2

我创建了一个自定义列表视图。我通过子类化 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);

这里的联系人列表是一个有很多对象的类。

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

4

3 回答 3

0

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

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

于 2012-06-25T04:33:43.493 回答
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

                }
            });
于 2012-06-25T04:21:51.610 回答
0

尝试这个,

  1. 使用onItemClickListener()

例如:

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

            }
        });
于 2012-06-25T04:42:56.143 回答