如何在buttonclick上获取所有listview选中的项目?

时间:2016-08-23 06:54:31

标签: android arrays sqlite listview

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从数据库中填充listview?所以我没有获得位置,因为我从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数组。

2 个答案:

答案 0 :(得分:0)

如果您尝试使用复选框从列表中获取选定的值,请尝试使用此方法

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

答案 1 :(得分:0)

在适配器外部单击时,请使用以下代码。 &#34; ListAdapter&#34;应该是Adapter类名。

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

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

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

在适配器中进行以下功能。

  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);
        }
    }
}

上面的函数将检查列表视图的所有checkBox。玩得开心。小心。