在android中处理定制的listview?

在android中处理定制的listview?,android,android-listview,custom-lists,Android,Android Listview,Custom Lists,我正在开发一个应用程序,我正在使用定制的listview。首先,我将使用代码,我将问我的问题(在底部) java(使用适配器类将数据设置为listview) } custom.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" androi

我正在开发一个应用程序,我正在使用定制的listview。首先,我将使用代码,我将问我的问题(在底部)

java(使用适配器类将数据设置为listview)

}

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>

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;


 }
    }
公共类Myadapter扩展了BaseAdapter{
public int getCount(){
//TODO自动生成的方法存根
返回startarr.size();
}
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回位置;
}
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图v=null;
int i;
LayoutInflater LayoutInflater=getLayoutInflater();
v=布局更平坦。充气(R.布局。自定义,空);
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);
//按钮btn=(按钮)v.findViewById(R.id.button1);
最后一个字符串arlstdate[]=startarr.get(position.split)(“~”);
对于(i=0;i
在这里,我将数据附加到TextView,下面代码的输出是。。。 \

到目前为止,一切都很顺利。但我的问题是

当我点击自定义列表视图时,值需要显示在单独的toast消息中。正如我对列表视图有一些想法一样,单击listener,但在这里,如果使用该代码,它将获取最后出现的数据详细信息。例如,如果我单击第一个(在图像中),它将显示16:40,而不是显示12:0。我想要的是,我需要用不同的toast消息来显示其中的两个


有人能帮我解决这个问题吗?…

你自己因为没有实施一个合适的模型而生活得很艰难。您应该在适配器外部解析Json,并利用这些信息生成Java类。然后将列表或数组作为数据传递给适配器,同时在
getItem(position)
中返回模型对象! 通过这种方式,您可以完美地实现一个
监听器
,并收集您想要的任何数据

您还可以完全忽略
convertView
参数。对于性能优化,如果该视图不为空,则应采用该视图,而不是膨胀新视图

为此,必须在listview上实现OnItemClickListener事件。
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.
OnItemClickListener(新的OnItemClickListener(){ @凌驾 public void onItemClick(AdapterView父级、视图、, 内部位置,长id){ //从选定的ListItem获取值 } }); 现在,您有了listview的位置,然后在toast中显示相应值所在的列表位置。
share custom.xml(布局)也只需为每个列表项维护一个对象,并在ListView的
onItemClick()
position click()中获取该对象即可。。不要附加字符串数据。。为此,您必须创建一个自定义的
列表
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.