2011-09-14 12 views
1

私はListViewについて助けが必要です。私はアンドロイドで新しく、ListView multiple_choiceを作成する必要があります(私は右のチェックボックスを意味します)、左側にアイコンがあります。私は項目を選択するまで問題はない、リストを作る。しかし、私が項目を選択したときチェックボックスはチェックされません。私はアイコンを使用してリストを作成し、imageViewとCheckedTextViewを使用するためにアダプタクラスを使用します。ここに私のコードMultiple_choice ListView with icon

public class EfficientAdapter extends BaseAdapter { 
private LayoutInflater mInflater; 
private Bitmap online; 
private Bitmap offline; 
private ArrayList<String> mCurrencyNames; 
public ViewHolder holder; 

public EfficientAdapter(Context context,ArrayList<String> mCurrencyNames) { // Cache the LayoutInflate to avoid asking for a new one each time. 

    mInflater = LayoutInflater.from(context); 
    this.mCurrencyNames = mCurrencyNames; 
    // Icon bound to the row. 
    online = BitmapFactory.decodeResource(context.getResources(), R.drawable.markeronline); 
    offline = BitmapFactory.decodeResource(context.getResources(), R.drawable.markeroffline); 
} 

    /** * The number of items in the list is determined by the number of speeches * in our array. * * @see android.widget.ListAdapter#getCount() */ 
public int getCount() { 
    // return DATA.length; 
    return mCurrencyNames.size(); 
} 

    /** * Since the data comes from an array, just returning the index is * sufficent to get at the data. If we were using a more complex data * structure, we would return whatever object represents one row in the * list. * * @see android.widget.ListAdapter#getItem(int) */ 
public Object getItem(int position) { 
    return position; 
} 


    /** * Use the array index as a unique id. * * @see android.widget.ListAdapter#getItemId(int) */ 
public long getItemId(int position) { 
    return position; 
} 

    /** * Make a view to hold each row. * * @see android.widget.ListAdapter#getView(int, android.view.View, * android.view.ViewGroup) */ 
public View getView(int position, View convertView, ViewGroup parent) { 
    // A ViewHolder keeps references to children views to avoid unneccessary calls 
    // to findViewById() on each row. 

    //When convertView is not null, we can reuse it directly, there is no need 
    // to reinflate it. We only inflate a new View when the convertView supplied 
    // by ListView is null. 
    if (convertView == null) { 
     convertView = mInflater.inflate (R.layout.menu_row, null); 

     // Creates a ViewHolder and store references to the two children views 
     // we want to bind data to. 
     holder = new ViewHolder(); 
     holder.text = (CheckedTextView) convertView.findViewById (R.id.text); 
     holder.icon = (ImageView) convertView.findViewById (R.id.icon); 

     convertView.setTag(holder); 
    } 
    else { 
     // Get the ViewHolder back to get fast access to the TextView 
     // and the ImageView. 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    // Bind the data efficiently with the holder. 
    holder.text.setText(mCurrencyNames.get(position)); 

    String status = Friends.nodelist.get(position).getNamedItem("status").getNodeValue(); 
    if(status.equals("1")){ 
     holder.icon.setImageBitmap(online); 
    } 
    else 
     holder.icon.setImageBitmap(offline); 

    return convertView; 
} 

public void refresh(ArrayList<String> list){ 
    mCurrencyNames = list; 
} 

static class ViewHolder { 
    public CheckedTextView text; 
    public ImageView icon; 
} 

} 

これは私のアダプタのコードであると私はそれが第一の項目をチェックします第四項目を選択するとき、私は、ソリューション

list.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
       RelativeLayout itemLayout = (RelativeLayout)list.getChildAt(position); 
       CheckedTextView ctv = (CheckedTextView)itemLayout.findViewById(R.id.text); 
       ctv.toggle(); 

     } 
    }); 

のためではなく、そのコードの後に​​そのコードを使用します。助けてくれてありがとう。

+0

動作しましたか?、... – Ronnie

答えて

0

onClickListenerCheckedTextViewに設定します。それが動作します。

OR

はfalseにCheckedTextViewのクリック可能なプロパティを設定します。