Reputation: 1216
I fill my Array list in DB by this:
db.addBookMark(new BookMark("Ravi", "9100000000"));
db.addBookMark(new BookMark("Srinivas", "9199999999"));
db.addBookMark(new BookMark("Tommy", "9522222222"));
db.addBookMark(new BookMark("Karthik", "9533333333"));
And read (all items from DB) by this:
// Reading all contacts
List<BookMark> bookmarks = db.getAllBookMarks();
and I create a xml layout listview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.wepars.webapp.activity.MsgActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
and also rows item row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I read this question before..But it works for a simple list. how can I publish my 2 diamond list in listview?
I try complete this code:
public class BookMarkActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
DatabaseBookMark db = new DatabaseBookMark(this);
Log.d("Insert: ", "Inserting ..");
db.addBookMark(new BookMark("Ravi", "9100000000"));
db.addBookMark(new BookMark("Srinivas", "9199999999"));
db.addBookMark(new BookMark("Tommy", "9522222222"));
db.addBookMark(new BookMark("Karthik", "9533333333"));
// Reading all contacts
List<BookMark> bookmarks = db.getAllBookMarks();
}
}
Upvotes: 0
Views: 114
Reputation: 3973
You have to extend an ArrayAdapter
public class BookmarkAdapter extends ArrayAdapter<BookMark> {
public BookmarkAdapter(Activity activity,
ArrayList<BookMark bookmark) {
super(activity, R.layout.row_item, bookmark);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final BookMark b = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent,
false);
viewHolder.message= (TextView) convertView.findViewById(R.id.message);
viewHolder.number= (TextView) convertView
.findViewById(R.id.number);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.message.setText(b.message);
viewHolder.number.setText(b.number);
return convertView;
}
private class ViewHolder {
TextView message, number;
}
}
and use it like this
public class BookMarkActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
DatabaseBookMark db = new DatabaseBookMark(this);
Log.d("Insert: ", "Inserting ..");
db.addBookMark(new BookMark("Ravi", "9100000000"));
db.addBookMark(new BookMark("Srinivas", "9199999999"));
db.addBookMark(new BookMark("Tommy", "9522222222"));
db.addBookMark(new BookMark("Karthik", "9533333333"));
// Reading all contacts
ArrayList<BookMark> bookmarks = db.getAllBookMarks();
// ADDED
ListView lv = (ListView) findViewById(R.id.list_view);
lv.setAdapter(new BookmarkAdapter(this,bookmarks));
}
}
Upvotes: 1