2012-09-03 4 views
1

У меня есть собственный макет ListView с TextView и CheckBox. Все работает нормально. Я хочу, когда я нажимаю на CheckBox или TextView (в единственном представлении из ListView), оба должны вести себя как один объект. (Я могу нажать на CheckBox, и это не влияет на TextView, и TextView не влияет на CheckBox.) Код не вызывает проблем.Пользовательский просмотр списка с OnClickListener

Я реализовал все возможные решения, но проблема все еще существует. (Один щелчок по каждому объекту списка должен рассматривать ONE COMPLETE CLICK для полной строки.) Надеюсь, я объяснил очень хорошо.

ОСНОВНАЯ ДЕЯТЕЛЬНОСТЬ

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

CUSTOM ADAPTER

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/Почему радиогруппа? – 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

, тогда что произойдет, если id сделает это .... baseadapter не принимает мой объект contactinfo. Любезно посоветуйте мне, что я должен делать – abidkhan303

Смежные вопросы