I am trying to display information from the database to my list view. I can display it, but when i want to do something with it ,I am getting inaccurate position in the getView method. Can you guys give me some tips of what should I do?
public class ViewAdapter extends BaseAdapter {
LayoutInflater minflater;
@Override
public int getCount() {
return productsList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
public ViewAdapter() {
minflater = LayoutInflater.from(getBaseContext());
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = minflater.inflate(R.layout.you, null);
}
final TextView text11 = (TextView) convertView.findViewById(R.id.text11);
text11.setText(productsList.get(position).get_productname());
final TextView text22 = (TextView) convertView.findViewById(R.id.text22);
text22.setText(productsList.get(position).get_versionname());
final TextView text33 = (TextView) convertView.findViewById(R.id.text33);
text33.setText(productsList.get(position).get_date());
final Button update = (Button) convertView.findViewById(R.id.update);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// int a = dbhandler.checkDatabase(productsList.get(position).get_date());
final Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
StringBuffer buffer = new StringBuffer();
buffer.append(day + "-" + (month + 1) + "-" + year);
String date = DateFormat.getTimeInstance().format(new Date());
String b = "Date: " + buffer + "\n" + "Time: " + "" + date + "\n";
dbhandler = new mydbhandler(getBaseContext(), null,null,1);
dbhandler.updateProduct(position,b);
productsList = dbhandler.getFavList();
listview.setAdapter(new ViewAdapter());
}
});
return convertView;
}
}