2 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
創(chuàng)建一個(gè)擴(kuò)展 BaseAdapter 的自定義適配器類,然后實(shí)現(xiàn)方法,然后在 getView 方法中將布局 id 傳遞給視圖,以便您可以訪問(wèn)布局文件中的 textview。
public class CampsitesListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Campsites> list;
public CampsitesListAdapter(Context mContext , List<Campsites> countries) {
this.mContext = mContext;
this.list = countries;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(mContext, R.layout.campsite_layout , null );
TextView name = (TextView) view.findViewById(R.id.nametxt);
Textview city = (Textview) view.findViewById(R.id.citytxt);
Textview feature = (Textview) view.findViewById(R.id.featuretxt);
name.setText(list.get(position).getname());
city.setText(list.get(position).getcity());
feature.setText(list.get(position).getfeature());
return view;
}
}
這是您的列表視圖活動(dòng)
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();
//Instance of custom adapter
CampsitesListAdapter adapter = new CampsitesListAdapter(this , campsites);
//Setting adapter to listview
lvCampsites.setAdapter(adapter);
}
我希望這可以幫助你。

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
您需要先移動(dòng)光標(biāo)。參考下面的代碼:
if (cursor!=null && cursor.getCount()> 0){
cursor.moveToFirst();
do{
Campsite 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);
}while(cursor.moveToNext);
}
添加回答
舉報(bào)