2017-03-17 96 views
-1

我正在開發一個項目,並且希望從列表視圖中獲取所選項目的複選框(複選框)。 有沒有辦法做到這一點? 目前爲止的代碼如下;在列表視圖中查找選中的複選框Android

主要活動

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.ListView; 
public class MainActivity extends Activity { 
    ListView lv; 
    Model[] modelItems; 
    CheckBox ONE,TWO,THREE; 
    Button MultiData; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     lv = (ListView) findViewById(R.id.listView1); 

     modelItems = new Model[5]; 
     modelItems[0] = new Model("pizza", 0); 
     modelItems[1] = new Model("burger", 0); 
     modelItems[2] = new Model("olives", 0); 
     modelItems[3] = new Model("orange", 0); 
     modelItems[4] = new Model("tomato", 0); 
     CustomAdapter adapter = new CustomAdapter(this, modelItems); 
     lv.setAdapter(adapter); 

    } 

} 

CustomAdapter

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class CustomAdapter extends ArrayAdapter { 
    Model[] modelItems = null; 
    Context context; 

    public CustomAdapter(Context context, Model[] resource) { 
     super(context, R.layout.row1, resource); 
     this.context = context; 
     this.modelItems = resource; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.row1, parent, false); 
     TextView name = (TextView) convertView.findViewById(R.id.textView1); 
     CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     name.setText(modelItems[position].getName()); 
     if (modelItems[position].getValue() == 1) 
      cb.setChecked(true); 
     else 
      cb.setChecked(false); 
     return convertView; 
    } 
} 

模型

public class Model{ 
    String name; 
    int value; /* 0 -> checkbox disable, 1 -> checkbox enable */ 

    Model(String name, int value){ 
     this.name = name; 
     this.value = value; 
    } 
    public String getName(){ 
     return this.name; 
    } 
    public int getValue(){ 
     return this.value; 
    } 

} 

main_xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 
</LinearLayout> 

row1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 
    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" /> 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 
</LinearLayout> 

Eidt1在主要活動

boolean modelItemBool[] = new boolean[modelItems.length]; 
     for (int h =0;h<modelItemBool.length;h++){ 
      modelItemBool[h] = false; 
     } 

Eidt 2

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.row1, parent, false); 
     TextView name = (TextView) convertView.findViewById(R.id.textView1); 
     CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     name.setText(modelItems[position].getName()); 

     cb.setOnCheckedChangeListener(new 
      CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if(isChecked){ 
         modelItems[position].getValue() == 1; 
        }else{ 
         modelItems[position].getValue() == 0; 
        } 
       } 
      }); 
     return convertView; 
    } 
+0

你必須記錄檢查位置,一旦他們得到遏制,在聽衆進行更新。您不能從ListView中獲取所有視圖,因爲它們正在被重用。 –

+2

可能重複的[使用列表視圖中的複選框獲取選定的項目](http://stackoverflow.com/questions/18162931/get-selected-item-using-checkbox-in-listview) –

+0

@VladMatvienko我怎樣才能'記錄檢查一旦他們被檢查了嗎? – user6750923

回答

0

試試這個

創建布爾數組與您modelItems大小和陣列中的1號店真假值

步驟:默認存儲的所有值陣列假

第2步:數組更新值複選框檢查狀態變化

+0

請添加代碼。 – user6750923

+0

我已經添加了這個'boolean modelItemBool [] = new boolean [modelItems.length]; for(int h = 0; h user6750923

+0

@Mahul Gajjar我該如何實現Step2? – user6750923

0
  • 新保持選定的項目清單:

    List<Model> selectedList = new ArrayList<>(); 
    
    private List getSelectedList(){ 
        if(!selectedList.isEmpty()) selectedList.clear(); 
        for (int i=0;i<array.length;i++){ 
         if(modelItems [i].getValue==1){ 
          selectedList.add(modelItems [i]); 
         } 
        } 
        return selectedList; 
    } 
    
  • getView設置onCheckedChangeListenerCheckBox

    cb.setOnCheckedChangeListener(new 
    
        CompoundButton.OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
          if(isChecked){ 
           modelItems[position].setValue(1); 
          }else{ 
           modelItems[position].setValue(0); 
          } 
         } 
        }); 
    
+0

我應該在哪裏添加代碼?在主要活動? – user6750923

+0

將第一步添加到MainActivity中,將第二步添加到Adapter中的getView方法。當你想要selectedList只是調用方法'getSelectedList()'。 – xiaoyuan

+0

我在'getView'中添加了step2,並將android的位置更改爲final。但是它給出'modelItems [position] .getValue()== 1;'行上的錯誤''請參閱編輯問題 – user6750923

0

回報convertView之前添加以下代碼;然後modelItems陣列將每一個複選框被選中時/未選中

final int posFinal = position; 
     cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
       modelItems[posFinal].setValue(b ? 1 : 0); 
      } 
     }); 
+0

'modelItems [posFinal] .getValue()'返回整數。〜〜 – xiaoyuan

+0

modelItems [posFinal] .setValue(b?1:0); –

+0

@ Tonteria24如何獲取MainActivty中的modelItems數組按鈕單擊?我在MainActivity中需要它。 – user6750923

0
// Replace your code with bellow, on the click of button it will show selected checkbox name in alert dialog. 
// MainActivity 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ListView; 

public class MainActivity extends Activity { 
    ListView lv; 
    Model[] modelItems; 
    Button btn; 
    String selectedCheckBoxNAme = ""; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_xml); 
     lv = (ListView) findViewById(R.id.listView1); 
     btn = (Button)findViewById(R.id.btn) ; 
     modelItems = new Model[5]; 
     modelItems[0] = new Model("pizza", 0); 
     modelItems[1] = new Model("burger", 0); 
     modelItems[2] = new Model("olives", 0); 
     modelItems[3] = new Model("orange", 0); 
     modelItems[4] = new Model("tomato", 0); 
     CustomAdapter adapter = new CustomAdapter(this, modelItems); 
     lv.setAdapter(adapter); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       selectedCheckBoxNAme = ""; 
       for (int i = 0; i < modelItems.length; i++) { 
        Model obj= modelItems [i]; 
        if(obj.getValue() == 1) 
        { 
         selectedCheckBoxNAme = selectedCheckBoxNAme +"\n" + obj.getName(); 
        } 
       } 
       if(!selectedCheckBoxNAme.isEmpty()) { 
        new AlertDialog.Builder(MainActivity4.this).setTitle("").setMessage(selectedCheckBoxNAme) 
          .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
                int which) { 
           } 
          }) 
          .setCancelable(false).create().show(); 
       } 
      } 
     }); 
    } 
} 
// CustomAdapter.java 
import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 

public class CustomAdapter extends ArrayAdapter { 
    Model[] modelItems = null; 
    Context context; 

    public CustomAdapter(Context context, Model[] resource) { 
     super(context, R.layout.row1, resource); 
     this.context = context; 
     this.modelItems = resource; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.row1, parent, false); 
     TextView name = (TextView) convertView.findViewById(R.id.textView1); 
     CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     name.setText(modelItems[position].getName()); 
     if (modelItems[position].getValue() == 1) 
      cb.setChecked(true); 
     else 
      cb.setChecked(false); 

     cb.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (((CheckBox) v).isChecked()) { 
      modelItems[position].setValue(1); 
      }else{ 
      modelItems[position].setValue(0); 
      } 
     } 
     }); 
     return convertView; 
    } 

} 
//main_xml.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="wrap_content" 
       > 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/btn"> 
    </ListView> 

    <Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Click Here" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true"/> 
</RelativeLayout> 

// Model 
public class Model { 
    String name; 
    int value; /* 0 -&gt; checkbox disable, 1 -&gt; checkbox enable */ 

    Model(String name, int value){ 
     this.name = name; 
     this.value = value; 
    } 
    public String getName(){ 
     return this.name; 
    } 
    public int getValue(){ 
     return this.value; 
    } 

    public void setValue(int value) 
    { 
     this.value = value; 
    } 
} 
相關問題