Wednesday, March 18, 2015

Android : How to Increment a number, in listView when clicking on Button



I'm new to Android, and i'm developing an application where users can like or dislike a post, when the user dislikes the post likes number-- if the user likes number++.


the problem is that when I press the button it works, but it doesn't work for some other items, and when I scroll down or up I lose the info, and it will be mixed all over the other items


I'm using an adapter view for my list view which look like this :





public adapter(Context context, ArrayList<statisticitem> questionaires) {
super(context, 0, questionaires);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

final statisticitem c = getItem(position);


if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom, parent, false);

}

TextView text1 = (TextView) convertView.findViewById(R.id.textView1);
final TextView text2 = (TextView) convertView.findViewById(R.id.textView2);
TextView text3 = (TextView) convertView.findViewById(R.id.textView3);
ImageView like = (ImageView) convertView.findViewById(R.id.imageView1);


text1.setText(c.getIdQuestion());

like.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

likes++;

text2.setText("likes :"+likes);


}
});




return convertView;
}

}



the Main Activity where I have the Listview :



ListView list1 = (ListView) findViewById(R.id.listView1);
lists = new ArrayList<statisticitem>();
for (int i=0;i<10;i++) {
statisticitem s = new statisticitem();
s.setIdQuestion("Question"+i);
lists.add(s);
}

adapter adapter = new adapter(MainActivity.this, (ArrayList<statisticitem>) lists);
list1.setAdapter(adapter);


}

No comments:

Post a Comment