I made a listview from db and I added onitemclicklistener. When I click on the item it will take me to the next activity and show full detail of the item. I also added a checkbox on the listview which checks the item. The problem is that when I click on the checkbox it wont work, but when I click on the item, it starts the next activity and after it, when I go back to the listview and try again to click on the checkbox, then this item checkbox works and the other one doesn't work. Also when I select a second item from the listview and go back then the checkbox works. Checkbox only works on that item with at least one time click. Does someone have an idea of what could be causing this problem? Here's my 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;
}
}
Thanks in advance