【问题标题】:how to get all listview checked item on buttonclick?如何在按钮单击时获取所有列表视图检查项目?
【发布时间】:2016-12-29 20:57:37
【问题描述】:
setContentView(R.layout.activity_violation);
    db = new DatabaseHelper(this);
    sqLiteDatabase = db.getReadableDatabase();
    spinner = (Spinner) findViewById(R.id.spinner1);
    loadspinnerdata();
    txtTexts = (TextView) findViewById(R.id.texts);
    btnBack = (Button) findViewById(R.id.btnBack);
    btnSave = (Button) findViewById(R.id.btnSave);
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            listviewData();
        }


    });

    strSelectedItem = spinner.getSelectedItem().toString();


    listview = (ListView) findViewById(R.id.listView);
    LayoutInflater inflater = getLayoutInflater();
    ViewGroup header = (ViewGroup) inflater.inflate(R.layout.list_header, listview, false);
    listview.addHeaderView(header, null, false);
    listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    list_query = "select _id,section,offence,fine from tblSection";
    Cursor clistview = sqLiteDatabase.rawQuery(list_query, null);


    listadapter = new ListAdapter(this, clistview, 0);



    listview.setAdapter(listadapter);private void listviewData() {
    int cntChoice = listview.getCount();

    String checked = "";

    String unchecked = "";
    SparseBooleanArray sparseBooleanArray = listview.getCheckedItemPositions();

    for(int i = 0; i < cntChoice; i++)
    {

        if(sparseBooleanArray.get(i))
        {
            checked += listview.getItemAtPosition(i).toString() + "\n";
        }
        else  if(!sparseBooleanArray.get(i))
        {
            unchecked+= listview.getItemAtPosition(i).toString() + "\n";
        }

    }

我正在使用 cursorAdapter 从数据库中填充列表视图?所以我没有从 arrayAdapter 中的 getView 方法获得位置

public class ListAdapter extends CursorAdapter{

TextView tvSection,tvOffence,tvId,tvFine;
CheckBox chkBox;
ArrayAdapter<String> objects;
private boolean chkItem;
CompoundButton.OnCheckedChangeListener myCheckChangList;


public ListAdapter(Context context, Cursor c, int _id) {
   super(context, c,_id);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    return LayoutInflater.from(context).inflate(R.layout.listview_item,viewGroup,false);
}
public class ViewHolder{
    TextView tvSection,tvOffence,tvId,tvFine;
    CheckBox chkBox;
    boolean chkItem;
    boolean selected = false;
    public ViewHolder(boolean chkItem){
        super();
        this.chkItem=chkItem;
    }

    public ViewHolder() {

    }

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

@Override
public void bindView(View view, Context context, Cursor cursor) {
   ViewHolder holder=null;


            holder = new ViewHolder();
            holder.tvSection = (TextView) view.findViewById(R.id.txtSection);
            holder.tvOffence = (TextView) view.findViewById(R.id.txtOffence);
            holder.tvId = (TextView) view.findViewById(R.id.txtId);
            holder.tvFine = (TextView) view.findViewById(R.id.txtFine);
            holder.chkBox = (CheckBox) view.findViewById(R.id.checkBox);
            holder.chkBox.setOnCheckedChangeListener(myCheckChangList);
            view.setTag(holder);
            strFine=cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
            String strSection = cursor.getString(cursor.getColumnIndexOrThrow("section"));
            String strOffence = cursor.getString(cursor.getColumnIndexOrThrow("offence"));
            int strFine = cursor.getInt(cursor.getColumnIndexOrThrow("fine"));



            holder.tvSection.setText(strSection);
            holder.tvOffence.setText(strOffence);
            holder.tvFine.setText(String.valueOf(strFine));

    final ViewHolder finalHolder = holder;

}

我试过了,但这不起作用。我尝试了 checkbox.setOnclicklistener,它给了我选中的行值单个项目,但没有多个选中的项目。我也尝试了 sparseboolean 数组。

【问题讨论】:

    标签: android arrays sqlite listview


    【解决方案1】:

    如果您尝试使用复选框从列表中获取选定的值,请尝试这种方式它会起作用

    public class CardViewActivity extends AppCompatActivity {
    
     private Toolbar toolbar;
    
     private RecyclerView mRecyclerView;
     private RecyclerView.Adapter mAdapter;
     private RecyclerView.LayoutManager mLayoutManager;
    
     private List<Student> studentList;
    
     private Button btnSelection;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      btnSelection = (Button) findViewById(R.id.btnShow);
    
      studentList = new ArrayList<Student>();
    
      for (int i = 1; i <= 15; i++) {
       Student st = new Student("Student " + i, "androidstudent" + i
         + "@gmail.com", false);
    
       studentList.add(st);
      }
    
      if (toolbar != null) {
       setSupportActionBar(toolbar);
       getSupportActionBar().setTitle("Android Students");
    
      }
    
      mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
      // use this setting to improve performance if you know that changes
      // in content do not change the layout size of the RecyclerView
      mRecyclerView.setHasFixedSize(true);
    
      // use a linear layout manager
      mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    
      // create an Object for Adapter
      mAdapter = new CardViewDataAdapter(studentList);
    
      // set the adapter object to the Recyclerview
      mRecyclerView.setAdapter(mAdapter);
    
      btnSelection.setOnClickListener(new OnClickListener() {
    
       @Override
       public void onClick(View v) {
        String data = "";
        List<Student> stList = ((CardViewDataAdapter) mAdapter)
          .getStudentist();
    
        for (int i = 0; i < stList.size(); i++) {
         Student singleStudent = stList.get(i);
         if (singleStudent.isSelected() == true) {
    
          data = data + "\n" + singleStudent.getName().toString();
          /*
           * Toast.makeText( CardViewActivity.this, " " +
           * singleStudent.getName() + " " +
           * singleStudent.getEmailId() + " " +
           * singleStudent.isSelected(),
           * Toast.LENGTH_SHORT).show();
           */
         }
    
        }
    
        Toast.makeText(CardViewActivity.this,
          "Selected Students: \n" + data, Toast.LENGTH_LONG)
          .show();
       }
      });
    
     }
    
    }
    

    http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

    【讨论】:

      【解决方案2】:

      在适配器外部单击时,使用以下代码。 “ListAdapter”应该是适配器类名。

       ListAdapter.unCheck((ViewGroup)view.getParent());
      

      如果您的点击是在适配器内部,则忽略上面的代码并使用下面的代码。

       unCheck((ViewGroup)view.getParent());
      

      在 Adapter 中创建以下函数。

        public static void unCheck(ViewGroup vg) {
          for (int i = 0; i < vg.getChildCount(); i++) {
              View v = vg.getChildAt(i);
              if (v instanceof CheckBox) {
                  ((CheckBox) v).setChecked(true);
              } else if (v instanceof ViewGroup) {
                  unCheck((ViewGroup) v);
              }
          }
      }
      

      上述函数将检查列表视图的所有复选框。玩得开心。保重。

      【讨论】:

        猜你喜欢
        • 2013-12-30
        • 2021-12-15
        • 1970-01-01
        • 2020-07-26
        • 1970-01-01
        • 2013-06-20
        • 2011-08-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多