在 android 中单击按钮时将列表视图中的选中项目转移到另一个列表视图
Posted
技术标签:
【中文标题】在 android 中单击按钮时将列表视图中的选中项目转移到另一个列表视图【英文标题】:transfer checked items in list-view to another list-view on button click in android 【发布时间】:2016-04-10 06:03:17 【问题描述】:我有一个listview
和使用customAdapter.class
的数据现在我想要的是在单击按钮时将listview
中的检查项目转移到secondActivity
...
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);*/
);
这是我在另一个列表视图中获取该数据的第二个活动
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);
【问题讨论】:
我想获取复选框的文本并将它们传输到 ResultActivity.java 并在另一个列表视图中显示它们 【参考方案1】:您将自定义列表发送到另一个活动的方法将不起作用。为了在活动之间传输您的自定义列表,您需要创建 Parcelable List 并通过 Intent 发送它。
Android Intents 不支持自定义列表。
自定义列表可以通过两种方式传递,Serialization 和 Parcelable。 但是 Parcelable 更高效且易于实现。
Refer this link to send custom list between activities through Parcelable
This link will give you much better idea to implement Parcelable.
更新的代码:更改您的模型代码,如下所示。
public class Model implements Parcelable
private String name;
private int selected;
public Model(String name)
this.name = name;
selected = 0;
public String getName()
return name;
public int isSelected()
return selected;
public void setSelected(boolean selected)
this.selected = selected;
@Override
public int describeContents()
// TODO Auto-generated method stub
return 0;
/**
* Storing the Student data to Parcel object
**/
@Override
public void writeToParcel(Parcel dest, int flags)
dest.writeString(name);
dest.writeInt(selected);
private Model (Parcel in)
this.name = in.readString();
this.selected = in.readInt();
public static final Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>()
@Override
public Student createFromParcel(Parcel source)
return new Student(source);
@Override
public Model[] newArray(int size)
return new Model[size];
;
然后在 MainActivity 中执行此操作..
Intent next = new Intent(MainActivity , ResultActivity.class);
next.putParcelableArrayListExtra("model_data", (ArrayList<? extends Parcelable>) selectedItems);
startActivity(next);
在 ResultActivity 中执行此操作。
ArrayList<Model> his = getIntent().getParcelableArrayListExtra("model_data");
试试上面的代码.. 祝你好运..!!
【讨论】:
如何传递关于edittext数据的listview数据 抱歉没能找到你, 你建议的链接即将从edittext传输数据我需要传输listview数据即ArrayList我通过单击按钮将检查的项目从 listview 保存到 sqlite 来解决。另一个按钮以打开新活动并以这种方式调用所选项目 sqlite... oncheckchange 在数组列表中添加和删除项目,并在 onbutton 点击中调用它,就像这样......
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;
这是添加到数据库的按钮编码
btest.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
handler.addSelected(adapter.selList, adapter.selID, adapter.selType);
这是如何插入到 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 + " ");
然后像这样从数据库返回
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 中单击按钮时将列表视图中的选中项目转移到另一个列表视图的主要内容,如果未能解决你的问题,请参考以下文章
Android:从自定义列表视图中单击的按钮获取列表视图项目