2016-01-22 36 views
1

聽說我使用自定義ListViewTextViewCheckBox。 但我想,單選擇在複選框在時間 一個CheckBox選中,那麼另一種是取消使用BaseAdapter 但是這個代碼不正常工作.. 請給我建議..thnks使用帶有TextView和CheckBox的Custome ListView單選CheckBox

@Override 
public View getView(final int position, View view, ViewGroup parent) { 
    Integer selected_position = -1; 
    holder = new ViewHolder(); 
    final Items itm = rowItem.get(position); 
    LayoutInflater layoutInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    if (view == null) { 

     view = layoutInflater.inflate(R.layout.activity_custom_list, 
       parent, false); 

     holder.tvItemName = (TextView) view.findViewById(R.id.textView1); 
     holder.check = (CheckBox) view.findViewById(R.id.checkBox1); 
     view.setTag(holder); 

    } else { 
     holder = (ViewHolder) view.getTag(); 
    } 

    holder.tvItemName.setText(itm.getItems()); 

    if (position == selected_position) 
     holder.check.setChecked(true); 
    else 
     holder.check.setChecked(false); 

    holder.check.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if (holder.check.isChecked()) { 
       selected_position = position; 
      } else { 
       selected_position = -1; 
      } 
      notifyDataSetChanged(); 

     } 
    }); 

    return view; 
}} 
+0

嘗試使用條件,使您的複選框的onClick使其他複選框假例如:otherCheckBox.setEnabled(假); – AndroidNewBee

+0

不會選擇單行列表視圖..不想在列表視圖中選擇單個複選框 –

+0

你想選擇一個複選框,如果你選擇另一個複選框,第一個應該自動被「取消選擇」? – 0X0nosugar

回答

1

使用自定義列表視圖與文本查看和複選框與單選框複選框 這麼多的嘗試,然後終於我得到了解決方案,我希望它是有用的代碼給你所有... 此代碼幫助你創建自定義列表視圖與文本視圖和複選框,然後選擇一個複選框,如果你選擇一個又一個第一應該自動被取消.....謝謝你......

activity_main .XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.listviewdemo2.MainActivity" > 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" /> 
</RelativeLayout> 

activity_custom_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.listviewdemo2.CustomListActivity" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/darker_gray" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="4dp" 
     android:layout_weight="0.39" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="150dp" 
     android:layout_marginTop="4dp"/> 
</LinearLayout> </LinearLayout> 

String.xml

<string-array name="name"> 
    <item>Laptop</item> 
    <item>Mobile</item> 
    <item>Desktop</item> 
    <item>TV</item> 
    <item>Pendrive</item> 
    <item>Router</item> 
    <item>Notebook</item> 
    <item>Tablet</item> 
    <item>I-pad</item> 
    <item>Bluetooth</item> 
    <item>HomeTheator</item> 
</string-array> 

MainActivity.java

String[] ItemName; 
List<Items> rowItem; 
ListView list; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    rowItem = new ArrayList<Items>(); 
    ItemName = getResources().getStringArray(R.array.name); 

    for(int i = 0 ; i < ItemName.length ; i++) 
    { 
     Items itm = new Items(ItemName[i]); 
     rowItem.add(itm); 
    } 

    list = (ListView) findViewById(R.id.listView1); 
    CustomListActivity adapter = new CustomListActivity(this, rowItem); 
    list.setAdapter(adapter); 
} 

Items.java

公共類項目{

private String items; 
private boolean selected; 


public Items(String items) { 

    this.items = items; 

} 

public String getItems() { 

    return items; 
} 

public void setItemName(String name) { 

    this.items = name; 
} 
public boolean getSelected() { 
    return selected; 
} 

public boolean setSelected(Boolean selected) { 
    return this.selected = selected; 
}} 

CustomListActivity.java

public class CustomListActivity extends BaseAdapter { 

Context context; 
List<Items> rowItem; 
View listView; 
boolean checkState[]; 

ViewHolder holder; 

public CustomListActivity(Context context, List<Items> rowItem) { 

    this.context = context; 
    this.rowItem = rowItem; 
    checkState = new boolean[rowItem.size()]; 

} 

@Override 
public int getCount() { 

    return rowItem.size(); 
} 

@Override 
public Object getItem(int position) { 

    return rowItem.get(position); 

} 

@Override 
public long getItemId(int position) { 

    return rowItem.indexOf(getItem(position)); 

} 

public class ViewHolder { 
    TextView tvItemName; 
    CheckBox check; 
} 

@Override 
public View getView(final int position, View view, ViewGroup parent) { 

    holder = new ViewHolder(); 
    final Items itm = rowItem.get(position); 
    LayoutInflater layoutInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

    if (view == null) { 

     listView = new View(context); 
     listView = layoutInflater.inflate(R.layout.activity_custom_list, 
       parent, false); 

     holder.tvItemName = (TextView) listView 
       .findViewById(R.id.textView1); 
     holder.check = (CheckBox) listView.findViewById(R.id.checkBox1); 
     listView.setTag(holder); 

    } else { 
     listView = (View) view; 
     holder = (ViewHolder) listView.getTag(); 
    } 

    holder.tvItemName.setText(itm.getItems()); 

    holder.check.setChecked(checkState[position]); 

    holder.check.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      for(int i=0;i<checkState.length;i++) 
      { 
       if(i==position) 
       { 
        checkState[i]=true; 
       } 
       else 
       { 
        checkState[i]=false; 
       } 
      } 
      notifyDataSetChanged(); 

     } 
    }); 
    return listView; 
}} 

顯示輸出: -

enter image description here

0

如果您想自定義ListView單選擇CheckBox你可以使用這樣的事情:

https://stackoverflow.com/a/12003125/5778152

+0

但我沒有想要使用按鈕像CkeckAll或ClearAll ..我希望選中一個複選框一次..例如一個複選框選中,然後我檢查另一個然後以前是取消選中,我使用BaseAdapter –