使用 ListView 显示 SQLite 数据库行的 ArrayList

我有一个 SQLite 数据库,我想使用 ListView 显示每个 ROW/Campsite。


DBHandler 方法返回 Campsites 的 ArrayList:


public ArrayList<Campsite> getAllCampsites() {

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CAMPSITES, null);

    ArrayList<Campsite> campsites = new ArrayList<>();

    Campsite campsite;

    if (cursor.getCount() > 0) {

        for (int i = 0; i < cursor.getCount(); i++) {

            cursor.moveToNext();

            campsite = new Campsite();

            campsite.setName(cursor.getString(1));

            campsite.setCity(cursor.getString(2));

            campsite.setFeature(cursor.getString(3));

            campsite.setFavorite(cursor.getString(4));

            campsite.setRating(cursor.getInt(5));

            campsite.setLatitude(cursor.getDouble(6));

            campsite.setLongitude(cursor.getDouble(7));


            campsites.add(campsite);

        }

    }

    cursor.close();

    db.close();

    return campsites;

}

我有一个名为“浏览”的活动,它在布局文件中包含 ListView。我还有一个 browser_row_layout ,它具有一行/露营地的布局。


因此,在 Browse.java 中,我需要创建适配器来显示每个露营地。注意:我仅在列表视图中显示 3 个特定列(名称、城市、功能)


因此,我有 Activity_browse 布局文件,其中包含实际的 ListView 元素和 browser_row_layout,它代表数据库的 1 行,并且仅包含其中 3 列的 TextView。


到目前为止我所拥有的:


import android.os.Bundle;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;


public class Browse extends AppCompatActivity {


ListView lvCampsites;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_browse);


    lvCampsites= findViewById(R.id.lvCampsites);


    DatabaseHandler db = new DatabaseHandler(this);

    ArrayList<Campsite> campsites = db.getAllCampsites();


}

}


TLDR:如何使用 ListView 创建一个适配器来显示露营地列表中的每个露营地,其中包含数据库的每一行。


杨__羊羊
浏览 79回答 2
2回答

慕尼黑的夜晚无繁华

创建一个扩展 BaseAdapter 的自定义适配器类,然后实现方法,然后在 getView 方法中将布局 id 传递给视图,以便您可以访问布局文件中的 textview。public class CampsitesListAdapter extends BaseAdapter {&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; private ArrayList<Campsites> list;&nbsp; &nbsp; public CampsitesListAdapter(Context mContext , List<Campsites> countries) {&nbsp; &nbsp; &nbsp; &nbsp; this.mContext = mContext;&nbsp; &nbsp; &nbsp; &nbsp; this.list = countries;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getCount() {&nbsp; &nbsp; &nbsp; &nbsp; return list.size();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object getItem(int position) {&nbsp; &nbsp; &nbsp; &nbsp; return list.get(position);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public long getItemId(int position) {&nbsp; &nbsp; &nbsp; &nbsp; return position;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; View view = View.inflate(mContext, R.layout.campsite_layout , null );&nbsp; &nbsp; &nbsp; &nbsp; TextView name = (TextView) view.findViewById(R.id.nametxt);&nbsp; &nbsp; &nbsp; &nbsp; Textview city = (Textview) view.findViewById(R.id.citytxt);&nbsp; &nbsp; &nbsp; &nbsp; Textview feature = (Textview) view.findViewById(R.id.featuretxt);&nbsp; &nbsp; &nbsp; &nbsp; name.setText(list.get(position).getname());&nbsp; &nbsp; &nbsp; &nbsp; city.setText(list.get(position).getcity());&nbsp; &nbsp; &nbsp; &nbsp; feature.setText(list.get(position).getfeature());&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }&nbsp;}这是您的列表视图活动import android.os.Bundle;import android.widget.ListView;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;public class Browse extends AppCompatActivity {ListView lvCampsites;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_browse);&nbsp; &nbsp; lvCampsites= findViewById(R.id.lvCampsites);&nbsp; &nbsp; DatabaseHandler db = new DatabaseHandler(this);&nbsp; &nbsp; ArrayList<Campsite> campsites = db.getAllCampsites();&nbsp; &nbsp; //Instance of custom adapter&nbsp;&nbsp; &nbsp; CampsitesListAdapter adapter = new CampsitesListAdapter(this ,&nbsp; &nbsp;campsites);&nbsp; &nbsp;//Setting adapter to listview&nbsp; &nbsp;lvCampsites.setAdapter(adapter);}我希望这可以帮助你。

喵喔喔

您需要先移动光标。参考下面的代码:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (cursor!=null && cursor.getCount()> 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.moveToFirst();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Campsite campsite = new Campsite();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsite.setName(cursor.getString(1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsite.setCity(cursor.getString(2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsite.setFeature(cursor.getString(3));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsite.setFavorite(cursor.getString(4));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsite.setRating(cursor.getInt(5));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsite.setLatitude(cursor.getDouble(6));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsite.setLongitude(cursor.getDouble(7));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; campsites.add(campsite);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }while(cursor.moveToNext);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java