将列表视图中的选中项目转移到按钮单击中的另一个列表视图中的android [英] transfer checked items in list-view to another list-view on button click in android

查看:73
本文介绍了将列表视图中的选中项目转移到按钮单击中的另一个列表视图中的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个listview,其中包含使用customAdapter.class的数据,现在我想要的是在按钮单击时将listview中的选中项目传输到secondActivity ...

I have a listview with data using customAdapter.class now what i want is that to transfer checked items in listview to secondActivity on button click...

 btest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SparseBooleanArray checked = listView.getCheckedItemPositions();
            ArrayList<Model> mylist = new ArrayList<Model>();
             for (int i = 0; i < checked.size(); i++) {
                    int position = checked.keyAt(i);
                    if (checked.valueAt(i))
                        // listView = new ArrayList<Model>();
                        mylist.add(String.valueOf(adapter.getItem(position)));

                }
                String[] output = new String[mylist.size()];
                for (int i = 0; i < mylist.size(); i++) {
                    output[i] = (mylist.get(i));

                }
                Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
                Bundle b = new Bundle();
                b.putStringArray("selectedItems", output);
                // b.putStringArrayList("SelectedItems: ",list);
                // b.putString("selectedItems", String.valueOf(output));
                intent.putExtras(b);
                startActivity(intent);*/
            }
    });

这是第二个活动,我正在另一个列表视图中获取数据

and this is the second activity where i am getting that data in another listview

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
    Bundle b = getIntent().getExtras();
    String[] result = b.getStringArray("selectedItems");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, result);
    lv.setAdapter(adapter);
}

推荐答案

我通过单击按钮将选中的项目从列表视图保存到sqlite来解决.另一个按钮可以打开新活动并以这种方式调用选定的项目... oncheckchange添加和删除数组列表中的项目,然后按这种方式在onbutton单击中调用此方法...

i solve by saving checked items from listview to sqlite on button click. another button to open new activity and call selected items sqlite this way... oncheckchange add and remove items in an arraylist and call this in onbutton click like this way...

  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder view = null;
    Support support = (Support) this.getItem(position);
    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.view_items, null);
        view = new ViewHolder();
        view.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
        view.cb = (CheckBox) convertView.findViewById(R.id.cb);
        convertView.setTag(view);

        view.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CheckBox cb = (CheckBox) buttonView;
                Support support = (Support) cb.getTag();
                support.setSelected(cb.isChecked());
                if (isChecked){
                    selList.add(support.status);
                    selID.add(support.id);
                    selType.add(support.type);
                  //  Toast.makeText(CustomAdapter.this, "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
                }else {
                    selList.remove(support.status);
                    selID.remove(support.id);
                    selType.remove(support.type);
                }
            }
        });
    }else{
        view = (ViewHolder) convertView.getTag();
        view.cb = view.getCb();
        view.tvInfo = view.getTvInfo();
    }
    view.cb.setTag(support);
    support = list.get(position);
    String id = support.getId();
    String status = support.getStatus();
    String type = support.getType();
    view.cb.setChecked(support.isSelected());
   // view.tvInfo.setText(id + "," + status + "," + type);
    view.tvInfo.setText(status);
    return convertView;
}

这是添加到数据库的按钮编码

this is button coding to add to db

 btest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           handler.addSelected(adapter.selList, adapter.selID, adapter.selType);

这是插入sqlite的方法.

and this is how to insert to sqlite..

public void addSelected(ArrayList<String> selList, ArrayList<String> selID, ArrayList<String> selType) {

    int size = selID.size();

    SQLiteDatabase db = getWritableDatabase();
    try{
       for (int i = 0; i < size ; i++){
            ContentValues cv = new ContentValues();
          //  cv.put(KEY_ID, selID.get(i).toString());
           cv.put(KEY_ID, selID.get(i));
             cv.put(KEY_STATUS, selList.get(i));
           cv.put(KEY_TYPE, selType.get(i));
            Log.d("Added ",""+ cv);
            db.insertOrThrow(TABLE_SELECTED, null, cv);
        }
        db.close();
    }catch (Exception e){
        Log.e("Problem", e + " ");
    }
}

像这样从db取回

 public ArrayList<String> getSelected() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<String> result = null;
    try{
        result = new ArrayList<String>();
      //  String query = "SELECT * FROM " + TABLE_SELECTED;
        String query = "SELECT " + KEY_ID + " FROM " + TABLE_SELECTED;

        Cursor c = db.rawQuery(query, null);
        if (!c.isLast()){
            if (c.moveToFirst()){
                do{
                    String sel_name = c.getString(c.getColumnIndex("_id"));
                    result.add(sel_name);
                    Log.d("Added ", sel_name);
                }while (c.moveToNext());
            }
        }
        c.close();
        db.close();
    }catch (Exception e){
        Log.e("Nothing is to show", e + " ");
    }
    return result;
}

这篇关于将列表视图中的选中项目转移到按钮单击中的另一个列表视图中的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆