我有一个SQLite数据库,其中包含一个表。我想使用ListActivity和ListView显示表的内容。问题是它只显示了
database.VesproModel@41814440VesproModel.java类只保存表的字段以及它们的getter和setter。我能够成功地将数据插入到表中,通过从设备中提取数据库并在计算机上查看来验证该表。
这就是我如何处理获取数据的方式:
public List<VesproModel> getAllComments() {
List<VesproModel> comments = new ArrayList<VesproModel>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_TPCS_VESSEL, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
VesproModel comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
cursor.close();
return comments;
}
private VesproModel cursorToComment(Cursor cursor) {
VesproModel comment = new VesproModel();
Log.d("cursorToComment", "Before if cursorToComment");
if( cursor != null && cursor.moveToFirst() ){
Log.d("cursorToComment", "inside cursorToComment");
comment.setId(cursor.getLong(0));
comment.setImo_number(cursor.getString(1));
...
...
...}
return comment;
}我在ListActivity中调用这个函数,如下所示:
public class CurrentList extends ListActivity {
private VesproDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currentlist);
datasource = new VesproDataSource(this);
datasource.open();
List<VesproModel> values = datasource.getAllComments();
ArrayAdapter<VesproModel> adapter = new ArrayAdapter<VesproModel>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}这是上面ListActivity的相应布局:
<?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" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>如何在屏幕上显示表格中的数据。请建议我应该使用什么样的布局,因为表中有相当多的列。谢谢。
编辑1 :
正如建议的那样,我创建了一个自定义类来扩展ArrayAdapter类以处理显示Vespromodel变量的问题:
public class ItemAdapter extends ArrayAdapter<VesproModel> {
private List<VesproModel> objects;
public ItemAdapter(Context context, int textViewResourceId, List<VesproModel> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
if (i != null) {
// I inserted some textviews to test the data out. For the final display , I would need many more textviews or a TableLayout
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
TextView mt = (TextView) v.findViewById(R.id.middletext);
TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
TextView btd = (TextView) v.findViewById(R.id.desctext);
if (tt != null){
tt.setText("Imo_number: ");
}
if (ttd != null){
ttd.setText(i.getImo_number());
}
if (mt != null){
mt.setText("Vessel Name : ");
}
if (mtd != null){
mtd.setText(i.getVessel_name());
}
if (bt != null){
bt.setText("Vessel_type : ");
}
if (btd != null){
btd.setText(i.getVessel_type());
}
}
return v;
}}
我是这样从ListActivity打来的:
List<VesproModel> values = dsrc.getAllComments(); // function is defined in the above section.
m_adapter = new ItemAdapter(this, R.layout.list_item, values);
setListAdapter(m_adapter);现在,该活动被困在getAllComments()方法中的无限循环中。
发布于 2013-06-25 10:49:54
ListView不知道如何显示来自VesproModel的值,这就是它显示VesproModel类名的原因。您需要的是实现您自己的适配器,它将知道如何从类的对象中获取数据。
或者,如果VesproModel类有您想要显示的字段--让我们称之为text --您可以使用ArrayAdapter<String>而不是ArrayAdapter<VesproModel>,然后:
List<VesproModel> values = datasource.getAllComments();
ArrayList<String> valuesToDisplay = new ArrayList<String>();
for(VesproModel vm : values) {
valuesToDisplay.add(vm.text);
}
ArrayAdapter<String> adapter = new ArrayAdapter<VesproModel>(this,
android.R.layout.simple_list_item_1, valuesToDisplay);
setListAdapter(adapter);这样,您就有了字符串列表,而ArrayAdapter确切地知道如何显示这些字符串。
如果你需要更多的信息,告诉我,所以我会给出更清楚的解释。
发布于 2013-06-25 11:09:58
您在ResourceId中使用的ArrayAdapter只接受String,因此values对象必须是String类型的Arraylist。
现在,您需要创建一个扩展ArrayAdapter<VesproModel>的适配器,然后创建自己的行,以在ArrayAdapter的getView方法中显示注释。
在创建了Adapter之后,可以通过以下方式调用它。
MyAdapter<VesproModel> adapter = new MyAdapter<VesproModel>(this,
R.layout.your_layout, values);其中values = new ArrayList<VesproModel>;
发布于 2013-06-25 11:04:12
莫基托斯的答案是正确的。另一种方法是在自定义类toString()中重写VesproModel方法。然后,它将打印在database.VesproModel@41814440中的任何内容,而不是获取toString()。例如,按照摩斯基托斯的方法,你可以这样做:
@Override
public String toString(){
return text;
}https://stackoverflow.com/questions/17295218
复制相似问题