Reputation: 1209
I'm new in Android development, and I am confused on how to display data dynamically...
I have a RelativeLayout activity type, where are 3 Textviews, 3 buttons and a ImageView.
What I want to achieve is to dynamically fill these items with the SQLite database.
Suppose that I do a query and I got 3 rows, then it should have all the structure I mentioned above but repeated 3 times with their respective data.
As a custom list?
In the documentation I've seen, many do not take the data from a database, but a StringArray, others take the data from the database but displayed in a list ...
Visually the structure is something like this:
For now, the image will always be the blond woman... But I want to fill at least the TextViews. In the sense that if the query I got 2 rows, then you should see something like
They would be so kind to explain in what way I can accomplish this?
Upvotes: 0
Views: 1039
Reputation: 39718
There's a couple of ways to do this, but you probably want a ListView, with a CursorAdapter. The CursorAdaptertakes a Cursor as an input, and shows how an individual row looks. The CursorAdapterwould look something like this:
private class SpecialCursorAdapter extends CursorAdapter {
public SpecialCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
//Any other major setup needs to happen here.
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
//Take the view as given in newView, and populate it with the dat afrom cursor.
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LinearLayout row = (LinearLayout) mInflater.inflate(
R.layout.data_row, parent, false);
//Inflate the row
return row;
}
}
The Cursor is populated with a standard query, there's plenty of resources how to do that. You get the ListView pointer from the inflated XML code, and do this:
ListView listvew=...;
Cursor cursoer=...;
SpecialCursorAdapter adapter=new SpecialCursorAdapter(this,cursor,0);
listview.setAdpater(adapater);
I regularly use this for very large data sets like this, and it works quite well.
Upvotes: 1
Reputation: 8836
ListView is the best option for this situation, you just need to make your own custom adapter for listview.
Also make your data structure for data and put them in a list and send it to Adapter, then handle it in getView() method in adapter.
Create a list item layout which contains buttons, imageview and textviews and use it in custom adapter.
class MyData{
String someText;
String someText2;
String buttonText;
}
List<MyData> list = new ArrayList<MyData>();
public class MyAdapter extends BaseAdapter{
public MyAdapter(Context c, List<MyData> list){
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate your custom list item layout and put your data
}
}
A sample here
Upvotes: 1