2012-06-25 16 views
2

我已經創建了一個自定義列表視圖。通過繼承arrayadapter,我使用自定義適配器添加了聯繫人詳細信息列表。如果我選擇列表中的特定聯繫人意味着我需要獲取所選的詳細信息。我怎麼能做到這一點。在這裏我的編碼,如何從android中的listView中選擇視圖?

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; 
    } 
} 
在我使用的列表視圖作爲馬寧活動

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); 

這裏聯繫人列表是具有許多對象的類。

我嘗試過這種方式,但沒有解決。請指導我。提前致謝。

回答

0

設置setOnItemClickListenersetOnItemSelectedListener列出,你會得到的通話項點擊並選擇...

setOnItemClickListener 

註冊一個回調在這個適配器視圖的項目已被點擊時調用。

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

..

setOnItemSelectedListener 

註冊一個回調,以便在這個適配器視圖中的項目已選定被調用。

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 

       } 
      }); 
0

首先,我將在用於顯示列表項的XML佈局中定義一個onclick監聽器。這是通過將屬性android:onClick =「methodName」添加到頂層佈局視圖來完成的。

然後在您的主要活動中,您需要實現在您的XML佈局中定義的方法(即methodName(View v)),當用戶敲擊列表項時會調用該方法。

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 

      } 
     }); 
+0

thakks它爲我工作 – Amarnath

相關問題