Reputation: 1446
I have this SimpleCursorAdapter for my ListView, the view is set from NewView. I'm trying to make that everytime the row is clicked, it leads to a new activity carrying the row's attribute to the new activity.
Normally I do this with OnItemClickListener, but since I'm using SimpleCursorAdapter and NewView, the only option I got to make the row clickable is by using OnClickListener.
But I have difficulty in implementing the method I used to have on OnItemClickListener to OnClickListener. Especially I have no idea how I deal with 'id' and 'position' on OnCLick.
Here's the OnItemClickListener I used to have:
protected void onItemClick(AdapterView<?> l, View v, int position, long id) {
Intent listIntent = new Intent(ProjectsList.this, DetailsActivity.class);
listIntent.putExtra("spendino.de.ProjectDetail.position",position);
listIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath(
Provider.CONTENT_URI, Database.Project.NAME), Long
.toString(id)));
startActivity(listIntent);}
And here's my SimpleCursorAdapter Class:
class MySimpleCursorAdapter extends SimpleCursorAdapter {
Context context=null;
ImageLoader loader = null;
private LayoutInflater mInflater = null;
private int mProjectNameIndex = -1;
private int mOrgNameIndex = -1;
private int mDescIndex = -1;
private int mDonationIndex = -1;
private int mSmallImageIndex = -1;
private int mKeywordIndex = -1;
private int mPriceIndex = -1;
private int mShortcodeIndex = -1;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
loader = new ImageLoader(context);
this.context = context;
mInflater = getLayoutInflater();
mProjectNameIndex = c.getColumnIndexOrThrow(Database.Project.C_PROJECTTITLE);
mOrgNameIndex = c.getColumnIndexOrThrow(Database.Project.C_ORGANIZATIONTITLE);
mDescIndex = c.getColumnIndexOrThrow(Database.Project.C_PROJECTDESCRIPTION);
mDonationIndex = c.getColumnIndexOrThrow(Database.Project.C_DONATIONAMOUNT);
mSmallImageIndex = c.getColumnIndexOrThrow(Database.Project.C_SMALLIMAGE);
mKeywordIndex = c.getColumnIndexOrThrow(Database.Project.C_KEYWORD);
mPriceIndex = c.getColumnIndexOrThrow(Database.Project.C_PRICE);
mShortcodeIndex = c.getColumnIndexOrThrow(Database.Project.C_SHORTCODE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View convertView = mInflater.inflate(R.layout.listitems, null);
convertView.setClickable(true);
convertView.setFocusable(true);
convertView.setBackgroundResource(android.R.drawable.menuitem_background);
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
}
});
return convertView;
}
Here's the line which tells that everytime the row is clicked it has to go to a new activity carrying its attributes:
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
}
});
I'm a newbie in this one, I really need a solution to deal with the difficulty I'm facing
Upvotes: 0
Views: 605
Reputation: 10938
I am not aware of any issues with using onItemClickListener on a ListView that uses a SimpleCursorAdapter.
newView effectively does the same thing as another adapter's getView as far as the ListView is concerned - they both give Views that are used as rows in the list, and the ListView provides the onItemClickListener interface independently of the adapter.
However, manually setting the View to be click-able may intercept the touch event intended for onItemClick so try your old onItemClickListener and don't call
convertView.setClickable(true);
UPDATED: Change the line in your newView method to:
convertView.setOnClickListener(new ClickListener(context, cursor.getString(mProjectNameIndex), convertView.getId()));
which uses this class:
class ClickListener implements OnClickListener {
Intent listIntent;
public ClickListener(Context c, String name, long id) {
listIntent = new Intent(c, DetailsActivity.class);
listIntent.putExtra("spendino.de.ProjectDetail.name", name);
listIntent.setData(Uri.withAppendedPath(Uri.withAppendedPath(
Provider.CONTENT_URI, Database.Project.NAME), Long
.toString(id)));
}
public void onClick(View v) {
v.getContext().startActivity(listIntent);
}
}
Also override bindView in your adapter (this is because Views created in newView can be reused).
@Override
public void bindView (View view, Context context, Cursor cursor) {
if(view != null) {
view.setOnClickListener(new ClickListener(context, cursor.getString(mProjectNameIndex), view.getId()));
}
super.bindView(view, context, cursor);
}
Upvotes: 1
Reputation: 14600
You can use a View's getTag() and setTag() methods to assign custom values to a View. You can use these for your 'id' & 'position' - assign them in your newView() method and pull them out in the onClick method.
Upvotes: 0