I have implemented one custom adapter for my list view having checkbox and text item. I can get the position through my overridden parameter. but how to get the id of my list row ?
following is code for custom adapter –
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.reminder_row, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.reminderRowTextId);
viewHolder.reminderCheckBox = (CheckBox) rowView.findViewById(R.id.CheckBoxId);
rowView.setTag(viewHolder);
}
final int pos = position;
final ViewHolder holder = (ViewHolder) rowView.getTag();
int idRow = holder.text.getId();
Log.i(TAG, "id of item selected-->" + idRow); <<<<<<------- IT IS GIVING SOME VALUE LIKE 2030771-------->>>
String s = names[position];
holder.text.setText(s);
holder.reminderCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (holder.reminderCheckBox.isChecked()) {
Toast.makeText(getContext(), "pos-->chkd" + pos, Toast.LENGTH_SHORT).show();
long longPos = (long)pos;
dbHelper.completeTask(longPos + 1);
} else {
Toast.makeText(getContext(), "pos-->un--chkd" + pos, Toast.LENGTH_SHORT).show();
}
}
});
holder.text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "TEXT CLICKED" + pos , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,ReminderModificationActivity.class);
long longPos = (long)pos;
intent.putExtra(TasksDBAdapter.KEY_ROWID, longPos + 1);
Log.i(TAG, "row clickd --> " + longPos);
((Activity) context).startActivityForResult(intent, ACTIVITY_EDIT);
}
});
return rowView;
}
EDIT 1.1
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_DATE_TIME = "reminder_date_time";
public static final String KEY_ROWID = "_id";
public static final String KEY_IS_COMPLETE = "is_complete";
private static final String TAG = "TasksDBAdapter";
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement, "
+ KEY_IS_COMPLETE + " boolean default 'false', "
+ KEY_TITLE + " text not null, "
+ KEY_BODY + " text , "
+ KEY_DATE_TIME + " text);";
Please have a look at marked line. Can anyone please help me with how to get the id. Please be detailed as I m learning android.
Thanks in advance,
Ray
You’re calling
getId()on aTextView, which according to the docs returns the value associated with theandroid:idattribute. This value is auto generated, and is probably the same asR.id.reminderRowTextId.Basically, you’re not actually setting a meaningful ID in your
ViewHolderclass. Just add another member variable, and store whatever you need.To elaborate, an adapter is used to describe the size of a backing list/array, and provide a View for each element. To do this properly, you implement
getView()andgetItem().getItem()takes a position, which is a parameter togetView(). Pass inpositiontogetItem(), which will give you your item from SQLite. Get that object’s ID, and set a variable inViewHolder.