2012-09-03 141 views
1

我使用TextView和CheckBox自定义ListView布局。一切正常。 我想要的是,当我点击CheckBox或TextView(在ListView中的单个视图上)都应该像一个对象一样。 (我可以点击CheckBox,它不会影响TextView,TextView对CheckBox没有任何影响。)代码没有问题。使用OnClickListener自定义列表视图

我已经实现了所有可能的解决方案,但问题仍然存在。 (单击列表中的每个对象应该考虑完整行的一个完整点击。)我希望我解释得很好。

主要活动

package com.example.smsplanner; 

public class SMSPlanner extends ListActivity  { 

    ListView contactsListView; 

    private String TAG = "SMSPlanner"; CheckBox check; 
    int count; 

    List<ContactInfo> list = new ArrayList<ContactInfo>(); 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     ph = new String[3]; 
     phType = new String[3]; 
     LoadContactListFromPhone(); 
     ContactsAdapter contactadAdapter = new ContactsAdapter(this, list); 
     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     setListAdapter(contactadAdapter); 
    }  
    @Override 
    public void onListItemClick(ListView parent, View v, int position, long id) 
    { 
     TextView tx =(TextView)v.findViewById(R.id.firstname); 
     TextView ph =(TextView)v.findViewById(R.id.phone); 
     Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show(); 

    } 
    final class ContactHolder{ 
     TextView txtviewfirstname; 
     CheckBox chkselected; 
     TextView txtviewphone; 
    } 
    void LoadContactListFromPhone() 
    { 
     loadlistandreturns(); 
    } 

    void call() 
    { 
     Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG); 
     toast.show(); 
    } 
} 

定义适配器

public class ContactsAdapter extends ArrayAdapter<ContactInfo> 
{ 

    private final Activity context; 
    int resourceid; 

    List<ContactInfo> list = null; 
    public ContactsAdapter(Activity context, List<ContactInfo> list) { 
     super(context, R.layout.contactrow, list); 
     this.context = context; 
     this.list = list; 
    } 

    @Override 
    public View getView(int position, View convertview, ViewGroup viewgroup){ 
     View view = null; 
     if(convertview == null){ 
      LayoutInflater inflater = context.getLayoutInflater(); 
      view = inflater.inflate(R.layout.contactrow, null); 

      ContactHolder holder = new ContactHolder(); 

      holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname); 
      holder.txtviewphone = (TextView)view.findViewById(R.id.phone); 
      holder.chkselected = (CheckBox)view.findViewById(R.id.check); 
      view.setTag(holder); 
     } 
     else{ 
      view = convertview; 
     } 
     ContactHolder holder2 = (ContactHolder) view.getTag(); 
     holder2.txtviewfirstname.setText(list.get(position).firstname); 
     holder2.txtviewphone.setText(list.get(position).phonenumber); 
     holder2.chkselected.setChecked(list.get(position).selected); 
     return view; 
    } 


    final class ContactHolder{ 
     TextView txtviewfirstname; 
     CheckBox chkselected; 
     TextView txtviewphone; 
    } 
} 

布局

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="100" > 

    <RadioGroup 
     android:id="@+id/rgStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="15" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/firstname" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/phone" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 
    </RadioGroup> 

    <RadioGroup 
     android:id="@+id/rgStyle2" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="85" 
     android:orientation="vertical" > 

     <CheckBox 
      android:id="@+id/check" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:checked="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" > 

     </CheckBox> 
    </RadioGroup> 
</LinearLayout> 
+0

1 /为什么RadioGroup? – njzk2

回答

0
i think you should make adapter as 

public class ContactsAdapter extends BaseAdapter { 

    ArrayList<ContactInfo> mlist; 
    Context mcontext; 


public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {  
     mlist = mchtlist; 
     mcontext = context; 

    } 

    @Override 
    public int getCount() { 
     return mlist.size(); 
    } 

    @Override 
    public Object getItem(int postion) { 
     return mlist.get(postion); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
     public View getView(int position, View convertview, ViewGroup viewgroup){ 
      View view = null; 
      if(convertview == null){ 
       LayoutInflater inflater = context.getLayoutInflater(); 
       view = inflater.inflate(R.layout.contactrow, null); 

       ContactHolder holder = new ContactHolder(); 

       holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname); 
       holder.txtviewphone = (TextView)view.findViewById(R.id.phone); 
       holder.chkselected = (CheckBox)view.findViewById(R.id.check); 

       setOnClickListener(new OnClickListener() { 
     @Override 
      public void onClick(View arg0) { 
      // to open the selected file in resp 

        // do your work here 
       }}); 


    chkselected .setOnClickListener(new OnClickListener() { 
     @Override 
    public void onClick(View v) { 
    // Toast.makeText(context,// "checked is clicke="+pos, 12).show(); 
      if (chkselected.isChecked())   
         {    

         // do your work here 
      } else { 

    // do your work here        
      } 
     } 
}); 



      view.setTag(holder); 

     } 
      else{ 
       view = convertview; 
      } 
      ContactHolder holder2 = (ContactHolder) view.getTag(); 
      holder2.txtviewfirstname.setText(list.get(position).firstname); 
      holder2.txtviewphone.setText(list.get(position).phonenumber); 
      holder2.chkselected.setChecked(list.get(position).selected); 
      return view; 
     } 

} 
+0

hehe .i在这里执行,但后来我无法访问它在我的smsplanner活动。 – abidkhan303

+0

好的,你能改变你的ArrayAdapter 为BaseAdapter吗? –

+0

然后会发生什么,如果身份证这样做.... baseadapter不采取我的contactinfo对象..请引导我该怎么办 – abidkhan303