我在同一布局上有一个使用复选框和提交按钮的列表视图。
现在我只能点击复选框,它什么都不做。我阅读了很多关于显示项目的教程,但其中大多数都是作为祝酒词出现的。我想要的是当我单击提交按钮时项目显示在新活动中。
这是代码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;
}
}
列表视图的第一个 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>