Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

listview - Android : BaseAdapter how to?

Ok, I have been searching thick and thin, and I am having some issues implementing a BaseAdapter.

I have been able to implement a Simple Cursor Adapter http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List7.html as per the example above.

There is a pretty good BaseAdapter example here : List14 google example

I am wanting to create my own List Adapter using BaseAdapter to show a listView, with multiple items from a Database. I know this can be done using the Simple Cursor Adapter, but I am looking to handle rows differently, so I want to be able to draw each row by overriding getView. The data would be pulled from a cursor.

I know this code is ugly for getting to the cursor data, but assuming I have populated a cursor. What suggestions do you have on this if column 8 contains the image resource id. :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    cursor.moveToPosition(position);
    ImageView i = new ImageView(mContext);
    i.setImageResource(cursor.getShort(8));
    i.setAdjustViewBounds(true);
    i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    return i;
}

Do you have any good examples of a BaseAdapter being drawn using a cursor?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try calling notifyDataSetChanged() from a method inside the BaseAdapter itself.

See the methods in List8 of the API Demos as an example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...