2012-08-17 73 views
3

這裏我創建自定義列表視圖與複選框/ RadioButton。我得到了這個,但我需要單一的選擇。自定義ListView與CheckBox單選

我嘗試使用這個lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);但它不是爲我工作。有沒有其他解決方案,請讓我知道。

main.java

private ImageAdapter adapter; 

private static String month[] = {"January","February","March","April","May", 
    "June","July","August","September", 
    "October","November","December"}; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ListView lstvw = (ListView) findViewById(R.id.listView); 

    adapter = new ImageAdapter(this, month); 
    lstvw.setAdapter(adapter); 
     lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    lstvw.setOnItemClickListener(this); 
} 

我不知道該如何添加代碼的複選框插入適配器class.please檢查適配器類,如下。

ImageAdapter.class

public class ImageAdapter extends BaseAdapter{ 

public String title[]; 
public String description[]; 
public Activity context; 
public LayoutInflater inflater; 

public ImageAdapter(Activity context,String[] title) { 
    super(); 

    this.context = context; 
    this.title = title; 

    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return title.length; 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public static class ViewHolder 
{ 

    TextView txtViewTitle; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    ViewHolder holder; 
    if(convertView==null) 
    { 
     holder = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.listitem, null); 

     holder.txtViewTitle = (TextView) convertView.findViewById(R.id.lstvw_textView); 
     convertView.setTag(holder); 
    } 
    else 
     holder=(ViewHolder)convertView.getTag(); 

    holder.txtViewTitle.setText(title[position]); 

    return convertView; 
} 

} 

編輯:

Listitem.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/lstvw_textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@+id/itemCheckBox" 
    android:padding="8dp" 
    android:text="hello world" /> 



<CheckBox 
    android:id="@+id/itemCheckBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:gravity="right" /> 

+0

使用RadioButton進行單選而不是ListView。 – 2012-08-17 07:05:28

+0

是否想通過單選按鈕或多選複選框在整個列表中選擇一個項目? – swiftBoy 2012-08-17 07:18:21

+0

@RDC是的,我只想單個項目選擇 – 2012-08-17 08:10:04

回答

6

這裏是如果我需要在一個時間只選擇單個項目我會做什麼。

Home.java(活動)

package com.lvcheck.activities; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

public class Home extends Activity 
{ 
    private ListView lvCheckBox; 
    private Button btnCheckAll, btnClearALl; 
    private String[] arr = {"One", "Two", "Three", "Four", "Five", "Six"}; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnCheckAll = (Button)findViewById(R.id.btnCheckAll); 
     btnClearALl = (Button)findViewById(R.id.btnClearAll); 

     lvCheckBox = (ListView)findViewById(R.id.lvCheckBox); 
     lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     lvCheckBox.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_multiple_choice, arr)); 


     btnCheckAll.setOnClickListener(new OnClickListener() 
     {   
      @Override 
      public void onClick(View arg0) 
      { 
       for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++) 
       { 
        lvCheckBox.setItemChecked(i, true);    
       } 
      } 
     }); 

     btnClearALl.setOnClickListener(new OnClickListener() 
     {   
      @Override 
      public void onClick(View v) 
      { 
       for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++) 
       { 
        lvCheckBox.setItemChecked(i, false);     
       } 
      } 
     }); 
    } 
} 

和我(main.xml中)的XML文件想這

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="center" 
     android:layout_height="wrap_content"> 

     <Button 
      android:layout_width="wrap_content" 
      android:text="Check All" 
      android:layout_marginRight="7dip" 
      android:id="@+id/btnCheckAll" 
      android:layout_height="wrap_content"> 
     </Button> 
     <Button 
      android:layout_width="wrap_content" 
      android:text="Clear All" 
      android:id="@+id/btnClearAll" 
      android:layout_height="wrap_content"> 
     </Button> 
    </LinearLayout> 

    <ListView 
     android:layout_width="fill_parent" 
     android:id="@+id/lvCheckBox" 
     android:fadingEdge="none" 
     android:cacheColorHint="@null" 
     android:layout_height="fill_parent"> 
    </ListView> 
</LinearLayout> 

所以輸出會是這樣的方式..

enter image description here

來源:here

讓我知道你是否對此有任何疑問。

編輯:檢查這個有用的鏈接:Custom Single choice ListView

+0

謝謝,我一直在使用 「新ArrayAdapter (這一點, android.R.layout.simple_list_item_multiple_choice,ARR)」 也做了,但我想這個使用自定義適配器類。 – 2012-08-17 10:39:03

+0

檢查[this](http://stackoverflow.com/questions/8337180/custom-single-choice-listview)和[this](http://tokudu.com/2010/android-checkable-linear-layout/)鏈接一定會幫助你 – swiftBoy 2012-08-17 10:50:22

2

您可以在適配器類使用CheckBox。嘗試這個。

@Override 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 
     // TODO Auto-generated method stub 

     if (convertView == null) { 
      inflater = LayoutInflater.from(adapterContext); 
      convertView = inflater.inflate(R.layout.view, null); 
      final ViewHolder viewHolder = new ViewHolder(); 

      viewHolder.name = (TextView) convertView.findViewById(R.id.txtName); 
      viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1); 

      convertView.setTag(viewHolder); 
     } 

     final ViewHolder holder = (ViewHolder) convertView.getTag(); 

     holder.name.setText(collectContactList.get(position).getName()); 

     holder.checkBox.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       CheckBox cb = (CheckBox) v; 
       if (cb.isChecked() == true) 
       { 
        // Here you will get list of checked item. 
       } 

       else 
       { 
       // Here you will get list of unchecked item.  
       } 

      } 
     }); 

希望這會幫助你。

+0

謝謝,但我想要單個項目選擇 – 2012-08-17 08:10:33

+0

你有關於單選擇複選框的任何想法嗎? – 2012-08-17 08:21:25

+0

裏面的if條件中你可以得到被選中的物品 – Akshay 2012-08-17 08:56:52

0

其簡單的和真正有用的。

int selected_position = -1 

public View getView(final int position, View convertView, ViewGroup parent) { 

    if (selected_position == position) { 
     CHECKBOX.setChecked(true); 

    } else { 
     CHECKBOX.setChecked(false); 

    } 
    CHECKBOX.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 


      if (((CheckBox) view).isChecked()) { 
       selected_position = position; 

      } else { 
       selected_position = -1; 
      } 


      notifyDataSetChanged(); 


     } 
    }); 
    return view; 
} 
相關問題