getItemIdAtPosition() problem in Android?
I am using the getItemIdAtPosition() to get the Basecoulmns id of the record in Sqlite Database.
the code is below:
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = managedQuery(Constants.CONTENT_URI,
PROJECTION, BaseColumns._ID + "="
+ l.getItemIdAtPosition(position), null, null);
}
But its does not retrieves the id correctly. Is this method depends some thing at the time of setting adapter or creation of DB? I have no idea. why it shows the position of the listview. Any Idea?
Edit:
the code for getView mehod :
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.brutube_videos, null);
开发者_运维知识库holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.vdo_text1);
holder.text2 = (TextView) convertView
.findViewById(R.id.vdo_text2);
holder.text3 = (TextView) convertView
.findViewById(R.id.vdo_rate_text);
holder.icon = (ImageView) convertView
.findViewById(R.id.vdo_icon);
convertView.setTag(holder);
} else {
holder.text1 = (TextView) convertView
.findViewById(R.id.vdo_text1);
holder.text2 = (TextView) convertView
.findViewById(R.id.vdo_text2);
holder.text3 = (TextView) convertView
.findViewById(R.id.vdo_rate_text);
holder.icon = (ImageView) convertView
.findViewById(R.id.vdo_icon);
}
if (!mBusy) {
try {
Log.v("getview", "" + (position) + " - " + VAL3[position]);
holder.icon.setImageBitmap(BitmapFactory
.decodeStream(new URL(VAL3[position])
.openConnection().getInputStream()));
notifyDataSetChanged();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
holder.icon.setTag(null);
} else {
holder.icon.setImageResource(R.drawable.no_video_icon);
// Non-null tag means the view still needs to load it's data
holder.icon.setTag(this);
}
holder.text1.setText(VAL1[position]);
holder.text2.setText(VAL2[position]);
holder.text3.setText(VAL4[position] + " Ratings");
return convertView;
}
You could try setting your desired value of your list row views as a tag. In your list's viewbinder or adapter use setTag() on your rows view.
Then call (long) v.getTag(); on list item click
Edit to include sample code for a BaseAdapter
/* In Your BaseAdapter */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout view;
if(convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
view = (RelativeLayout)layoutInflator.inflate(R.layout.my_layout_row, null);
} else {
view = (RelativeLayout)convertView;
}
long myIdFromDB = // Do your database work
// Set Tag
view.setTag(myIdFromDB);
return view;
}
/* Your listItemClicked method */
protected void onListItemClick(ListView l, View v, int position, long id) {
// Get Tag
long baseColumnId = (long)v.getTag();
Cursor cursor = managedQuery(Constants.CONTENT_URI,
PROJECTION, BaseColumns._ID + "="
+ baseColumnId, null, null);
}
Don't know if this is helpful but you can check out how the standard Android CursorAdapter passes the id along and maybe do something similar in your implementation.
More specifically, check out getItemId and hasStableIds.
精彩评论