我在DB中填充我的数组列表:
db.addBookMark(new BookMark("Ravi", "9100000000"));
db.addBookMark(new BookMark("Srinivas", "9199999999"));
db.addBookMark(new BookMark("Tommy", "9522222222"));
db.addBookMark(new BookMark("Karthik", "9533333333"));
然后阅读(来自DB的所有项目):
// Reading all contacts
List<BookMark> bookmarks = db.getAllBookMarks();
我创建了一个xml布局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>
以及行项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>
之前我读过this question ..但它适用于一个简单的列表。如何在listview中发布我的2钻石列表?
我尝试完成此代码:
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();
}
}
答案 0 :(得分:1)
您必须扩展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;
}
}
并像这样使用
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));
}
}
答案 1 :(得分:0)