如何从android中的listView获取选定视图?

时间:2012-06-25 04:14:27

标签: android listview android-arrayadapter

我创建了一个自定义列表视图。我通过继承arrayadapter使用自定义适配器添加了联系人详细信息列表。如果我选择列表中的特定联系人意味着我需要获取所选的详细信息。我怎样才能做到这一点。在这里我的编码,

public class ContactListAdapter extends ArrayAdapter<ContactList> {

    Context context;
    int layoutResourceId;
    ContactList objects[] = null;

    View row;

    public ContactListAdapter(Context context, int layoutResourceId, ContactList[] objects) {
        super(context, layoutResourceId, objects);
        // TODO Auto-generated constructor stub

        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        row = convertView;
        final ContactListHolder holder;

        if ( row == null ) {

            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ContactListHolder();
            holder.image    = (ImageView) row.findViewById(R.id.contactImage);
            holder.name     = (TextView) row.findViewById(R.id.contactName);
            holder.number   = (TextView) row.findViewById(R.id.contactNumber);
            holder.check    = (CheckBox) row.findViewById(R.id.selectedContact);
            holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub


                }
            });

            row.setTag(holder);
            holder.check.setTag(objects[position]);

        } else {

            holder = (ContactListHolder) row.getTag();
            holder.check.setTag(objects[position]);
        }

        ContactList contact = objects[position];
        if(contact.imageIcon != null) {

            Bitmap imgBitmap = BitmapFactory.decodeByteArray(contact.imageIcon, 0, contact.imageIcon.length);
            holder.image.setImageBitmap(imgBitmap);
        } else {

            holder.image.setImageResource(R.drawable.ic_launcher);
        }

        holder.name.setText(contact.name);
        holder.number.setText(contact.number);
        holder.check.setChecked(objects[position].isSelected());    

        return row;

    }

    static class ContactListHolder {

        ImageView image;
        TextView name;
        TextView number;
        CheckBox check;
    }
}

在manin活动中我使用列表视图,

ContactList contactList[] = new ContactList[MyTrackList.size()];

            for(int i=0;i<MyTrackList.size();i++) {

                MyContact contact = MyTrackList.get(i);
                contactList[i] = new ContactList(contact.getName(), contact.getNumber(), contact.getImage());

            }

            ContactListAdapter adapter = new ContactListAdapter(this, R.layout.manage_track_list_custom_view, contactList);

            trackList = (ListView) findViewById(R.id.manage_track_listView);
            trackList.setAdapter(adapter);

此处联系人列表是一个包含许多对象的类。

我试过这种方式,但它没有成功。请指导我。提前谢谢。

3 个答案:

答案 0 :(得分:0)

setOnItemClickListenersetOnItemSelectedListener设置为列表,您将在点击并选中项目时收到调用...

 setOnItemClickListener

注册单击此AdapterView中的项目时要调用的回调。

trackList.setOnItemClickListener(new OnItemClickListener() 
   {
   public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
   {
              contact =  contactList[position]          
   }
 });

...

setOnItemSelectedListener

注册在选择此AdapterView中的项目时调用的回调。

 trackList.setOnItemSelectedListener(new OnItemSelectedListener() 
            {
                public void onItemSelected(AdapterView<?> parent, View view, int position, long i) 
                {
                      // TODO Auto-generated method stub

                                   contact =  contactList[position] 
                                   //   or 
                                // Object obj= parent.getItemAtPsotion(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });

答案 1 :(得分:0)

首先,我将在XML布局中定义一个onclick侦听器,用于显示列表项。这是通过将属性 android:onClick =“methodName”添加到顶级布局视图来完成的。

然后在您的主要活动中,您需要实现XML布局中定义的方法(即methodName(View v)),当用户点击列表项时,将调用该方法。

答案 2 :(得分:0)

试试这个,

  1. 使用 onItemClickListener()
  2. 例如:

    ListView lv = (ListView)findViewById(R.id.myList);
    
    lv.setOnItemClickListener(new OnItemClickListener() {
    
    public void onItemSelected(AdapterView<?> parent, View view, int position, long i) 
                {
                      // Get the item here
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
    
                }
            });