我想在listview中获取已选中复选框的文本并将其转移到按钮单击时的第二个活动..我需要一些代码来执行此操作....这是我到目前为止所做的... 检查图片... ListView1和ListView2
这是activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btest"
android:layout_alignParentBottom="true"
android:text="submit"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_alignParentTop="true"
android:layout_above="@+id/btest"
android:clickable="true"
>
</ListView>
view_items.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" > <CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cbCategory"
android:text=" "
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="tvname"
android:layout_below="@+id/cbCategory"
/>
result.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/outputlist"
></ListView>
这是我的MainActivity.java
public class MainActivity extends Activity {
ListView listView;
ArrayAdapter<Model> adapter;
Button btest;
List<Model> list = new ArrayList<Model>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btest = (Button) findViewById(R.id.btest);
listView = (ListView) findViewById(R.id.list);
// adapter = new CustomAdapter(this,getModel());
adapter = new CustomAdapter(this,android.R.layout.simple_list_item_multiple_choice,getModel());
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, getModel());
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
final ArrayList<String> selectedStrings = new ArrayList<String>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tvcheck = (TextView) view.getTag(R.id.name);
CheckBox cb = (CheckBox) view.getTag(R.id.cbCategory);
if (cb.isChecked()) {
cb.setChecked(false);
}else {
cb.setChecked(true);
}
/*
selectedStrings.add(tvcheck.getText().toString());
Intent i = new Intent(getApplicationContext(), ResultActivity.class);
// Bundle b = new Bundle();
// b.putString("selectedStrings", selectedStrings);
i.putExtra("SelectedStrings", selectedStrings);
// i.putExtras(b);
startActivity(i);*/
Toast.makeText(view.getContext(), tvcheck.getText().toString()+ "" +isCheckedOrNot(cb), Toast.LENGTH_LONG).show();
}
});
btest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<Model> selectedItems = new ArrayList<>();
for (int i =0; i< checked.size(); i++){
int pos = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(pos));
}
String[] output = new String[selectedItems.size()];
for (int i = 0; i< selectedItems.size(); i++){
output[i] = String.valueOf(selectedItems.get(i));
}
Intent i = new Intent(getApplicationContext(), ResultActivity.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", output);
i.putExtras(b);
startActivity(i);
}
});
}
private String isCheckedOrNot(CheckBox checkBox){
if(checkBox.isChecked())
return "is checked";
else
return "is not checked";
}
private List<Model> getModel() {
list.add(new Model("pulses"));
list.add(new Model("tea"));
list.add(new Model("Milk"));
list.add(new Model("Sugar"));
list.add(new Model("Salt"));
list.add(new Model("Rice"));
list.add(new Model("Bread"));
list.add(new Model("Charger"));
list.add(new Model("Mouse"));
list.add(new Model("Mobile"));
list.add(new Model("Laptop"));
list.add(new Model("Bulb"));
return list;
}
}
这是我的CustomAdapter类
public class CustomAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public CustomAdapter(Activity context ,int res,List<Model> list){
super(context, R.layout.view_item,res,list);
this.context = context;
this.list = list;
}
static class ViewHolder{
protected TextView tvcheck;
protected CheckBox cb;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.view_item, null);
viewHolder = new ViewHolder();
viewHolder.tvcheck = (TextView) convertView.findViewById(R.id.name);
viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cbCategory);
// final ViewHolder finalViewHolder = viewHolder;
viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer)buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
viewHolder.cb.setTag(list.get(position));
convertView.setTag(R.id.name, viewHolder.tvcheck);
convertView.setTag(R.id.cbCategory, viewHolder.cb);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.cb.setTag(position);
viewHolder.tvcheck.setText(list.get(position).getName());
viewHolder.cb.setChecked(list.get(position).isSelected());
return convertView;
}
}
这是ResultActivity.class
public class ResultActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
ListView lv = (ListView) findViewById(R.id.outputlist);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,resultArr );
lv.setAdapter(adapter);
}
}
这是MOdel.class
public class Model {
private String name;
private boolean selected;
public Model(String name){
this.name = name;
selected = false;
}
public String getName(){
return name;
}
public boolean isSelected(){
return selected;
}
public void setSelected(boolean selected){
this.selected = selected;
}
}