开发者

Using adapters for non-list views

I am using a SimpleCursorAdapter to populate a ListView with items from my database. When a user clicks on an item it takes them to the detail Activity for that item. What is the best practice for displaying that page; just write a display function that grabs all the TextViews I want to set the text for and populate them from the database or to use another SimpleCursorAdapter to set the TextViews? If the answer is to use 开发者_JAVA技巧an adapter, do I have to shove the detail page in a ListView?


I believe the best method is extracting some id in the list activity and pass that id (in the intent) to the detail Activity.

If it's not that much data, you could send everything in the intent's extras. The problem is that intents weren't designed to send complex data (neither big amounts of data) from one activity to another.

In the detail activity, you shoul get the data from the database (example: select * from people where id = 123). Now you could map the columns of that table to a simple object.

For example:

public class Person {
    String name;
    Integer age;
    String address;
}

As a final step, to show the data to your user, grab some textviews from your layout and set their text with the values you have in the person you got from the database.

TextView ageTv = (TextView)findViewById(R.id.age);
ageTv.setText(person.age + " years old");
//etc

There's no need to use a ListView in the detail activity


Jason

You have no need to use an adapter in this instance, it is more complex and resource intensive than necessary. ListViews bring a lot of side effect along with them including but not limited to:

  • Listeners for scroll and touch events
  • Many rows of small view objects, taking away available memory
  • If you are not recycling those rows, then are being read from disk and re-inflated repeatedly
  • Several repeated calls to the children's onMeasure() when drawing the view hierarchy and estimating the listview's size

Basically you are wasting memory and cpu time by using the ListView and Adapter paradigm inappropriately. All you need is a Cursor for your data, TextViews to display it, a ScrollView and (LinearLayout || RelativeLayout) to hold the TextViews that will scroll if they are too big to fit on screen at once.


Few ways of doing this:

  • Simply attach item listener to the ListView and inside, invoke a new intent, and pass it some data (small amounts of data preferably).
  • Or you could create another adapter in the detailed activity list view, that would also use a simple cursor adapter to pull details from your sqlite db. (more data)

It really depends on problem you are trying to solve. Isn't SimpleCursorAdapter to and from array logic, a neat solution to all that set, get stuff?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜