2

我正在开发一个使用自定义列表视图的应用程序。首先,我将使用代码,我会问我的问题(在底部)。

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 消息以不同的方式显示其中两个。

谁能帮我这个....

4

2 回答 2

2

如果没有实施适当的模型,您自己会过得很艰难。您应该在适配器之外解析您的 Json,并根据这些信息制作 Java 类。然后将列表或数组作为数据传递给您的适配器,同时返回模型对象getItem(position)!通过这种方式,您可以完美地实现OnItemClickListener并收集您想要的任何数据。

您也只是完全忽略了convertView参数。如果它不为空以进行性能优化,您应该使用此视图而不是膨胀一个新视图!

于 2012-11-06T09:39:25.787 回答
0
for that you have to implement the OnItemClickListener event on listview .

listview.OnItemClickListener(new OnItemClickListener(){
    @Override
        public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    // getting values from selected ListItem

       }
   });
 Now you have position of your listview then Whatever the list position that corresponding value is diplays in toast.
于 2012-11-06T11:09:53.297 回答