2 回答
TA贡献1864条经验 获得超6个赞
创建一个扩展 BaseAdapter 的自定义适配器类,然后实现方法,然后在 getView 方法中将布局 id 传递给视图,以便您可以访问布局文件中的 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;
}
}
这是您的列表视图活动
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贡献1735条经验 获得超5个赞
您需要先移动光标。参考下面的代码:
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);
}
添加回答
举报