我在同一版面上使用复选框和提交按钮有一个列表视图。
现在我只能点击复选框,它什么也没做。我阅读了很多关于显示项目的教程,但其中大多数都是作为toast消息发布的。 当我点击提交按钮时,我想要的是在新活动中显示的项目。
这是CheckBoxListAdapter
:
public class CheckboxListAdapter extends BaseAdapter implements OnClickListener {
private LayoutInflater inflator;
private List<SampleData> dataList;
public CheckboxListAdapter(LayoutInflater inflator) {
super();
this.inflator = inflator;
dataList = new ArrayList<SampleData>();
dataList.add(new SampleData("Teh Ais (130 cal)", false));
dataList.add(new SampleData("Teh O Ais (130 cal)", false));
dataList.add(new SampleData("Neslo Ais (170 cal)", false));
dataList.add(new SampleData("Kopi Ais (130 cal)", false));
dataList.add(new SampleData("Kopi O Ais (130 cal)", false));
dataList.add(new SampleData("Jus buah (130 cal)", false));
dataList.add(new SampleData("Limau ais (40 cal)", false));
dataList.add(new SampleData("Air berkarbonat (120 cal)", false));
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflator.inflate(R.layout.element_example, null);
view.findViewById(R.id.checkBox1).setOnClickListener(this);
}
SampleData data = (SampleData) getItem(position);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1);
cb.setChecked(data.isSelected());
cb.setTag(data);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText(data.getName());
return view;
}
@Override
public void onClick(View view) {
SampleData data = (SampleData) view.getTag();
data.setSelected(((CheckBox) view).isChecked());
}
}
CheckboxListActivity
的代码:
public class CheckboxListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
CheckboxListAdapter adapter = new CheckboxListAdapter(
getLayoutInflater());
getListView().setAdapter(adapter);
}
}
SampleData.java
:
public class SampleData {
private String name;
private boolean selected;
public SampleData(String name, boolean selected) {
super();
this.name = name;
this.selected = selected;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
listview的第一个XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="389dp"
android:layout_weight="0.79" >
</ListView>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_submit" />
</LinearLayout>
复选框的第二个XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:0)
在开始下一个活动时,将所选项目传递给Intent对象。从下一个Activity中的intent中检索所选项目并使用它。
实施例
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("Selected_Item", selectedItemData);
startActivity(intent)
在下一个活动中执行以下操作
Intent intent = getIntent();
String selectedItem = intent.getStringExtra("Selected_Item")