ListView不显示android sqlite项目

时间:2013-03-27 04:44:54

标签: android sqlite listview

我在使用ListView显示sqlite数据库中的数据项时遇到问题。 当我尝试在点击按钮后看到数据时,它会显示:

  

com.LocationReminder和Task @ 4053afb0

这很奇怪。

这是我的readReminder.java

import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class readReminder extends ListActivity {
private DBReminder operasiDB;
private ArrayList<Task> daftarTugas;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.read_task);
     operasiDB = new DBReminder(this);

     daftarTugas = operasiDB.viewAllTask();
     ArrayAdapter<Task> adapter = new ArrayAdapter<Task>(this,
         android.R.layout.simple_list_item_1, daftarTugas);

     setListAdapter(adapter);
 }
}

这是我的代码,用于显示数据库中的所有数据

public ArrayList<Task> viewAllTask() {
    openConn();
    ArrayList<Task> tugas = new ArrayList<Task>();
    Cursor curs = db.rawQuery("select * from " + TableName, null);
    if (curs.moveToFirst()) {
        do {
            Task task = new Task();
            task.setId(Integer.parseInt(curs.getString(curs.getColumnIndex(ID))));
    task.setLatitude(Integer.parseInt(curs.getString(curs.getColumnIndex(LATITUDE))));               
task.setLongitude(Integer.parseInt(curs.getString(curs.getColumnIndex(LONGITUDE)));
task.setRadius(Integer.parseInt(curs.getString(curs.getColumnIndex(RADIUS))));
task.setAlamat(curs.getString(curs.getColumnIndex(ALAMAT)));
task.setKonteks(curs.getString(curs.getColumnIndex(KONTEKS_TUGAS)));
tugas.add(task);
        } while (curs.moveToNext());
    }
    closeConn();
    return tugas;
}

在SQLite Manager Eclipse中,数据也随表一起出现。 请帮我。谢谢......

3 个答案:

答案 0 :(得分:0)

您必须通过扩展Adapter类来创建自己的ArrayAdapter

答案 1 :(得分:0)

如果要在列表视图中显示多个列,请使用SimpleCursorAdapter

public Cursor viewAllTask() {
//openConn(); open your database in the activity onCreate

return db.rawQuery("select * from " + TableName, null);

//closeConn(); // close your database in the activity onDestroy
} 

更改private ArrayList<Task> daftarTugas;Cursor daftarTugas;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.read_task);
 operasiDB = new DBReminder(this);
 operasiDB.open();

 Cursor daftarTugas = operasiDB.viewAllTask();
 String[] fromColumns = {DBReminder.RADIUS, 
                         DBReminder.ALAMAT,
                         DBReminder.KEY_KONTEKS_TUGAS};
int[] toViews = {R.id.textview_radius, 
                 R.id.textview_alamat,
                 R.id.textview_konteks_tugas};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                            R.layout.listview_items,
                            daftarTugas, fromColumns, toViews, 0);
        setListAdapter(adapter);
 }  

布局XML名称listview_items

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/textview_radius"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView 
    android:id="@+id/textview_alamat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView 
    android:id="@+id/textview_konteks_tugas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

答案 2 :(得分:0)

默认情况下,ArrayAdaptor类期望提供的资源ID引用单个TextView。您需要自定义它。

使用simplecursoradoptor的简单方法。