Je développe une application où j'utilise listview personnalisé. D'abord j'irai avec le code et je poserai ma question (au fond).Manipulation liste personnalisée sur Android?
Main.java (en utilisant la classe de l'adaptateur pour définir des données sur listview)
Myadapter adapter = new Myadapter();
listview.setAdapter(adapter);
}
custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:background="#adadad"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
classe Myadapter:
public class Myadapter extends BaseAdapter{
public int getCount() {
// TODO Auto-generated method stub
return startarr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
int i;
LayoutInflater layoutinflater = getLayoutInflater();
v = layoutinflater.inflate(R.layout.custom,null);
TextView aptdate = (TextView)v.findViewById(R.id.textView1);
TextView apttime = (TextView)v.findViewById(R.id.textView2);
TextView aptname = (TextView)v.findViewById(R.id.textView3);
TextView aptid = (TextView)v.findViewById(R.id.textView4);
//Button btn = (Button)v.findViewById(R.id.button1);
final String arlstdate[] = startarr.get(position).split("~");
for (i = 0; i < arlstdate.length; i++) {
aptdate.setText(arlstdate[i]);
try {
// Getting Array of Contacts
JSONObject json = new JSONObject(response);
contacts = json.getJSONArray("schedule");
// looping through All Contacts
for(int j = 0; j < contacts.length(); j++){
JSONObject c = contacts.getJSONObject(j);
// Storing each json item in variable
final String id = c.getString("scheduleId");
String startTime = c.getString("startDateTime");
String endTime = c.getString("endDateTime");
String type = c.getString("scheduleType");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startedTime = sdf.parse(startTime);
Date endedTime = sdf.parse(endTime);
int getstartdate = startedTime.getDate();
int getstartmonth = startedTime.getMonth();
int getstartyear = startedTime.getYear();
int getday = startedTime.getDay();
final int getstartingtime = startedTime.getHours();
final int getstartingmin = startedTime.getMinutes();
long diff = endedTime.getTime() - startedTime.getTime();
int hours = (int)(diff/(60*60*1000));
testselectID = String.valueOf(hours);
hoursarray.add(testselectID);
starttimearray.add(String.valueOf(getstartingtime+":"+getstartingmin));
calendar = Calendar.getInstance();
calendar.setTime(startedTime);
System.out.println((calendar.get(Calendar.YEAR))+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DAY_OF_MONTH)));
if(arlstdate[i].equals((calendar.get(Calendar.YEAR))+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DAY_OF_MONTH))))
{
aptid.append(id+"\n");
apttime.append(testselectID+"Hrs"+"\n");
aptname.append((String.valueOf(getstartingtime+":"+getstartingmin))+"\n");
}
}
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(), "schedule error is " + e, Toast.LENGTH_SHORT).show();
}
}
return v;
}
}
Ici, j'apposent les données à textviews et la sortie du code suivant est ... \
ici Upto tout fonctionne bien. Mais quelle est ma question ...
Lorsque je clique sur la liste personnalisée, les valeurs doivent s'afficher dans un message de toast séparé. Comme j'ai une idée concernant l'affichage de la liste, cliquez sur l'écouteur, mais ici si vous utilisez ce code, il prend les derniers détails de données apposés. Par exemple, si je clique sur le premier (dans l'image), il affiche 16: 40. ne pas déployer 12: 0. Ce que je veux, c'est que je doive en afficher deux différemment avec un message différent.
Quelqu'un peut-il me aider ....
part custom.xml (mise en page) et – Adil
qu'entretenir ** Un objet ** par élément de la liste et 'onItemClick de ListView()' obtenir ce Objet avec clic .. Ne pas ajouter les données de chaîne. Pour cela, vous devez faire une «liste