我从db制作了一个listview,并添加了onitemclicklistener。当我点击该项目时,它将带我进入下一个活动并显示该项目的完整详细信息。我还在listview上添加了一个复选框来检查项目。问题是当我点击复选框时它不会工作,但是当我点击该项目时,它会启动下一个活动,之后,当我返回列表视图并再次尝试单击复选框时,则此项目复选框工作,另一个不起作用。此外,当我从列表视图中选择第二个项目并返回时,该复选框也可以。复选框仅适用于该项目,且至少有一次点击。有人知道可能导致这个问题的原因吗?这是我的Java:
public class DataListActivity extends Activity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
FoodDbHelper foodDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
ListDataAdapter dataAdapter = null;
Button button;
DataProvider dataProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.data_list_layout);
checkButtonClick();
listView = (ListView)findViewById(R.id.list_View);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdapter);
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getReadableDatabase();
cursor = foodDbHelper.getInformations(sqLiteDatabase);
if (cursor.moveToFirst())
{
do {
String name,quantity,calorie,fat,protein,sugar,vitamins;
boolean selected = false;
String names = null;
name = cursor.getString(0);
quantity = cursor.getString(1);
calorie = cursor.getString(2);
fat = cursor.getString(3);
protein = cursor.getString(4);
sugar = cursor.getString(5);
vitamins = cursor.getString(6);
DataProvider dataProvider = new DataProvider(name,quantity,calorie,fat,protein,sugar,vitamins,names,selected);
listDataAdapter.add(dataProvider);
}while (cursor.moveToNext());
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,int position, long id) {
String name = (String) ((TextView) view.findViewById(R.id.text_dish_name)).getText();
String quantity = (String) ((TextView) view.findViewById(R.id.text_dish_quantity)).getText();
String calorie = (String) ((TextView) view.findViewById(R.id.text_dish_calorie)).getText();
String fat = (String) ((TextView) view.findViewById(R.id.text_dish_fat)).getText();
String protein = (String) ((TextView) view.findViewById(R.id.text_dish_protein)).getText();
String sugar = (String) ((TextView) view.findViewById(R.id.text_dish_sugar)).getText();
String vitamins = (String) ((TextView) view.findViewById(R.id.text_dish_vitamins)).getText();
String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(),
"dish name is : " + name,
Toast.LENGTH_SHORT).show();
CheckBox names = (CheckBox) view.findViewById(R.id.checkBox1);
listView.setOnItemClickListener(this);
names.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
DataProvider dataProvider = (DataProvider) checkBox.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + dataProvider.getName() + checkBox.getText() +
" is " + checkBox.isChecked(),
Toast.LENGTH_SHORT).show();
dataProvider.setSelected(checkBox.isChecked());
}
});
Intent intent=new Intent(getApplicationContext(),Detail.class);
intent.putExtra("Dish name",name);
intent.putExtra("Dish quantity",quantity);
intent.putExtra("Dish calorie",calorie);
intent.putExtra("Dish fat",fat);
intent.putExtra("Dish protein",protein);
intent.putExtra("Dish sugar",sugar);
intent.putExtra("Dish vitamins",vitamins);
startActivity(intent);
}
});}
private void checkButtonClick() {
List list = new ArrayList();
listDataAdapter = new ListDataAdapter(this,
R.layout.row_layout,list);
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following dishes were selected...\n");
ArrayList<DataProvider> list = (ArrayList<DataProvider>) listDataAdapter.list;
for(int i=0;i<list.size();i++) {
DataProvider dataProvider = list.get(i);
if (dataProvider.isSelected()) {
responseText.append("\n" + dataProvider.getName()+" : " + dataProvider.getCalorie()+" kcal");
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
}
public class DataProvider {
private String name = null;
private String quantity;
private String calorie;
private String fat;
private String protein;
private String sugar;
private String vitamins;
private String names = null;
boolean selected = false;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getQuantity(){
return quantity;
}
public void setQuantity(String quantity){
this.quantity = quantity;
}
public String getCalorie(){
return calorie;
}
public void setCalorie(String calorie){
this.calorie = calorie;
}
public String getFat(){
return fat;
}
public void setFat(String fat){
this.fat = fat;
}
public String getProtein(){
return protein;
}
public void setProtein(String protein){
this.protein = protein;
}
public String getSugar() {return sugar; }
public void setSugar(String sugar) {this.sugar = sugar ;}
public String getVitamins() {return vitamins; }
public void setVitamins(String vitamins) {this.vitamins = vitamins ;}
public String getNames() {return names; }
public void setNames(String names) {this.names = names ;}
public boolean isSelected()
{return selected;}
public void setSelected(boolean selected) {this.selected = selected;}
public DataProvider(String name,String quantity,String calorie, String fat,String protein,String sugar,String vitamins,String names, boolean selected){
this.name = name;
this.quantity = quantity;
this.calorie = calorie;
this.fat = fat;
this.protein = protein;
this.sugar = sugar;
this.vitamins = vitamins;
this.names = names;
this.selected = selected;
}
public void add(List list) {
}
}
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
public ListDataAdapter(DataListActivity dataListActivity, int row_layout, List list) {
super(dataListActivity, row_layout, list);
}
static class LayoutHandler{
TextView name,quantity,calorie,fat,protein,sugar,vitamins;
CheckBox names;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if (row == null)
{
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.name = (TextView)row.findViewById(R.id.text_dish_name);
layoutHandler.quantity = (TextView)row.findViewById(R.id.text_dish_quantity);
layoutHandler.calorie = (TextView)row.findViewById(R.id.text_dish_calorie);
layoutHandler.fat = (TextView)row.findViewById(R.id.text_dish_fat);
layoutHandler.protein = (TextView)row.findViewById(R.id.text_dish_protein);
layoutHandler.sugar = (TextView)row.findViewById(R.id.text_dish_sugar);
layoutHandler.vitamins = (TextView)row.findViewById(R.id.text_dish_vitamins);
layoutHandler.names = (CheckBox) row.findViewById(R.id.checkBox1);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider)this.getItem(position);
layoutHandler.name.setText(dataProvider.getName());
layoutHandler.quantity.setText(dataProvider.getQuantity());
layoutHandler.calorie.setText(dataProvider.getCalorie());
layoutHandler.fat.setText(dataProvider.getFat());
layoutHandler.protein.setText(dataProvider.getProtein());
layoutHandler.sugar.setText(dataProvider.getSugar());
layoutHandler.vitamins.setText(dataProvider.getVitamins());
layoutHandler.names.setChecked(dataProvider.isSelected());
layoutHandler.names.setTag(dataProvider);
return row;
}
}
提前致谢
答案 0 :(得分:0)
你需要做一些事情:
第1步:
需要修改getView ..
此处只需要添加复选框
的操作示例:
layoutHandler.names.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
DataProvider dataProvider = (DataProvider) checkBox.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + dataProvider.getName() + checkBox.getText() +
" is " + checkBox.isChecked(),
Toast.LENGTH_SHORT).show();
dataProvider.setSelected(checkBox.isChecked());
}
});
第2步:
在listView行中添加checkBox后,单击导致问题的操作。 你可以解决这个问题,你需要从getView方法
触发itemclick动作示例:
//I am adding an extra widget
layoutHandler.layout = (LinearLayout)row.findViewById(R.id.linear_layout);
然后点击操作
layoutHandler.layout.setOnclick.....
在此触发器项下,使用performItemClick
layoutHandler.layout.performItemClick(...