我正在开发一个使用自定义列表视图的应用程序。首先,我将使用代码,我会问我的问题(在底部)。
Main.java(使用适配器类将数据设置为列表视图)
Myadapter adapter = new Myadapter();
listview.setAdapter(adapter);
}
自定义.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>
我的适配器类:
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;
}
}
在这里,我将数据附加到 textviews 并且以下代码的输出是... \
到这里为止一切正常。但是我的问题是......
当我单击自定义列表视图时,值需要在单独的 toast 消息中显示。因为我对列表视图单击侦听器有一些想法,但在这里如果使用该代码,它将获取最后附加的数据详细信息。例如,如果我单击第一个(在图像中)它显示 16:40.not 显示 12:0。我想要的是我需要用不同的 toast 消息以不同的方式显示其中两个。
谁能帮我这个....