views:

141

answers:

2

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;
    }
+1  A: 

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);
}
disretrospect
ThanX for reply. Can you explain it with some sample code. I can not get you completely.
Praveen Chandrasekaran
Sure, can you explain a bit more about how you are populating your list? Are you populating it from a Cursor? If so, do you use a SimpleCursorAdapter?
disretrospect
no i just extend the base adapter. how to set for it?
Praveen Chandrasekaran
From the looks of your sample code you are already setting the tag of the convertView to a ViewHolder instance. You could add a property to ViewHolder that can hold the id, then when you do listItemClick you would do ViewHolder vh = (ViewHolder)v.getTag(); long baseColumnId = vh.getMyId();
disretrospect
+1  A: 

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.

alun