【问题标题】:How to get data from my database and put it on a ListView that is clickable?如何从我的数据库中获取数据并将其放在可点击的 ListView 上?
【发布时间】:2012-02-27 14:10:58
【问题描述】:

我正在努力学习SQLite。我想将数据库中的信息(TextView)放入可点击的ListView(我仍然不知道可点击的ListView)。我可以就如何从我的数据库中获取数据并将其放在可点击的ListView 上获得一些帮助吗?这是我想在ListView 中拥有的教程网站的代码:

Button ViewBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);

    ViewBack=(Button) findViewById(R.id.bVBack);
    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    ViewBack.setOnClickListener(this);

    HotOrNot info = new HotOrNot(this);
    info.open();
    String data = info.geData(); 
    info.close();
    tv.setText(data);

}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    finish();

}

sqlview.xml:

<ScrollView
      android:layout_width="fill_parent"
      android:layout_height="match_parent" >      
<TextView
    android:id="@+id/tvSQLinfo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:text="get info from db" />
</ScrollView>

起初我试着把这个:

ListView lv = (ListView) findViewById(R.id.listMe);
ListAdapter listAD = new ListAdapter(this,R.id.listMe, tv);
 lv.setAdapter(listAD);

但我得到一个无法实例化数据类型ListAdapter的错误,所以我尝试在new ListAdapter(this,R.id.listMe,tv)上替换SimpleCursorAdapter

ListAdapter listAD = new SimpleCursorAdapter (this,R.id.listMe,tv, null, null);

但它又给了我一个错误,它建议我将演员表更改为Cursor 所以,我更改了所有内容,直到它没有给我任何错误。当我尝试运行它时,它不起作用。这是HotOrNotClass

public static final String KEY_ROWID= "_id"; 
public static final String KEY_NAME= "persons_name"; 
public static final String KEY_HOTNESS= "presons_hotness"; 

private static final String DATABASE_NAME= "HotOrNotdb";
public static final String DATABASE_TABLE= "peopleTable"; 
private static final int DATABASE_VERSION= 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_HOTNESS + " TEXT NOT NULL);"
        );  

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " +DATABASE_TABLE);
            onCreate(db);
    }

}
public HotOrNot(Context c){
    ourContext=c;
}

public HotOrNot open() throws SQLiteException{
ourHelper = new DbHelper(ourContext);
ourDatabase =ourHelper.getWritableDatabase();

return this;  }

public void close(){
    ourHelper.close();
}

public long createEntry(String name, String hotness) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_HOTNESS, hotness);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public String geData() {
    // TODO Auto-generated method stub
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
    Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result ="";
    int iRow=c.getColumnIndex(KEY_ROWID);
    int iName=c.getColumnIndex(KEY_NAME);
    int iHotness=c.getColumnIndex(KEY_HOTNESS);

    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
        result=result + c.getString(iRow)+" "+c.getString(iName)+" "+c.getString(iHotness)+"\n";
    }
    return result;

}
public String getName(long l) throws SQLiteException{
    // TODO Auto-generated method stub
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
    Cursor c= ourDatabase.query(DATABASE_TABLE, columns,  KEY_ROWID+"="+l, null, null, null, null);
    if (c!=null){
        c.moveToFirst();

        String name=c.getString(1);
        return name;
    }
    return null;
}

public String getHotness(long l)throws SQLiteException {
    // TODO Auto-generated method stub
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
    Cursor c= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+l, null, null, null, null);
    if (c!=null){
        c.moveToFirst();

        String hotness=c.getString(2);
        return hotness;
    }
    return null;
}

【问题讨论】:

  • 您能否显示您的 HotOrNot 类的源代码以及尝试运行它时遇到的错误?
  • 我把上面的代码放上去,放在这里看起来很乱
  • 你知道如何把它放到listview上吗?

标签: android sqlite android-listview


【解决方案1】:

您的getData() 方法会返回一个包含所有结果的大String。我认为这不是您想要的列表,因此请修改 getData() 方法,以便您可以返回代表您的数据库记录的 String 列表:

public ArrayList<String> geData() {
    // TODO Auto-generated method stub
    String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
    Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    ArrayList<String> result = new ArrayList<String>();
    int iRow=c.getColumnIndex(KEY_ROWID);
    int iName=c.getColumnIndex(KEY_NAME);
    int iHotness=c.getColumnIndex(KEY_HOTNESS);

    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
        result.add(c.getString(iRow)+" "+c.getString(iName)+" "+c.getString(iHotness));
    }
    return result;
}

为了显示一个列表,您必须在您的 Activity 布局中放置一个 ListView 元素(sqlview),这样您就可以像这样进行布局:

    <?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" >

    <Button
        android:id="@+id/bVBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Something" />

    <ListView
        android:id="@+id/listMe"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

然后在您的 onCreate() 方法中将 ListView 绑定到 Adapter

//...
ListView lv = (ListView) findViewById(R.id.listMe);
HotOrNot info = new HotOrNot(this);
info.open();
ArrayList<String> data = info.geData(); 
info.close();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item1, data));

【讨论】:

  • 当我第一次测试它时,它给了我一个错误“无法启动活动组件信息”所以我在网上搜索,据说我只需要清理我的项目。非常感谢你回答我的问题,我欠你一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多