我想从prepopulate sqlite数据库的两个字段中检索数据。这些字段是组织名称(org_name)和联系号码(contact_no)。之后,我需要将org_name数据分配给一个大文本字段,并将contact_no分配给我的自定义列表视图中的小文本字段。
我只用一个字段试过这个。它工作正常。但是,当我试图检索两个字段时,它无法正常工作。这是我试过的。请帮我解决这个问题。
ContactView类
public class ContactView extends Activity {
private ListView listView;
private ListView listView1;
List<Organization> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes(getIntent().getStringExtra("ID_EXTRA"));
databaseAccess.close();
List<Organization> rowItem=new ArrayList<Organization>();
for(String quote:quotes){
Organization temp=new Organization(quote);
rowItem.add(temp);
}
listView = (ListView)findViewById(R.id.listView);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.single_row_item, rowItem);
listView.setAdapter(adapter);
}
数据库访问类
public class DBAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DBAccess instance;
String passedVar = null;
private ListView listView;
public DBAccess(Context context) {
this.openHelper = new HelloDatabase(context);
}
public static DBAccess getInstance(Context context) {
if (instance == null) {
instance = new DBAccess(context);
}
return instance;
}
public void open() {
this.database = openHelper.getWritableDatabase();
}
public void close() {
if (database != null) {
this.database.close();
}
}
public List<String> getQuotes(String id) {
List<String> list = new ArrayList<>();
Integer value;
if (id != null) {
Cursor cursor = database.rawQuery("SELECT org_name,contact_no FROM org_name WHERE category_id = \"" + id + "\"", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
list.add(cursor.getString(cursor.getColumnIndex("contact_no")));
cursor.moveToNext();
}
cursor.close();
}
return list;
}}
Bean类
public class Organization {
public String title;
public String telenum;
public Organization(String title,String telenum) {
this.title = title;
this.telenum=telenum;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTelenum(){
return telenum;
}
public void setTelenum(String telenum){
this.telenum=telenum;
}
}
CustomListViewAdapter类
public class CustomListViewAdapter extends ArrayAdapter<Organization> {
Context context;
public CustomListViewAdapter(Context context, int layout,
List<Organization> items) {
super(context, layout, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
TextView txtTele;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Organization rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.single_row_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.org_name);
holder.txtTele = (TextView) convertView.findViewById(R.id.tele_num);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
holder.txtTele.setText(rowItem.getTelenum());
return convertView;
}}
答案 0 :(得分:1)
注意:如果任何人面临同样的问题,请使用SimpleCursorAdapter
。它简单,易用,高效。找一个简单的示例here或在此帖子https://stackoverflow.com/a/37560755/5460053
由于DBAccess类的getQuotes()方法中的以下行,它不适用于两个字段:
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));//Adding org_name first. org_name is added at indexes : 0,2,4,...
list.add(cursor.getString(cursor.getColumnIndex("contact_no")));//Then adding contact_no. contact_no is added at indexes : 1,3,5,...
cursor.moveToNext();
}
然后在为适配器创建数据源时,在备用索引处添加org_name和contact_no
List<Organization> rowItem=new ArrayList<Organization>();
for(String quote:quotes){
Organization temp=new Organization(quote);//I wonder how this worked as there is only one contructor for Organization which is expecting 2 parameters
rowItem.add(temp);
}
将您的DBAccess类getQuotes()更改为:
public List<Organization> getQuotes(String id) {
List<Organization> list = new ArrayList<>();
if (id != null) {
Cursor cursor = database.rawQuery("SELECT org_name,contact_no FROM org_name WHERE category_id = \"" + id + "\"", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Organization org = new Organization(cursor.getString(0),
cursor.getString(cursor.getColumnIndex("contact_no")));
list.add(org);
cursor.moveToNext();
}
cursor.close();
}
return list;
}
将您的ContactView活动的onCreate()更改为:
public class ContactView extends Activity {
private ListView listView;
private ListView listView1;
List<Organization> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<Organization> rowItem = databaseAccess.getQuotes(getIntent().getStringExtra("ID_EXTRA"));
databaseAccess.close();
listView = (ListView)findViewById(R.id.listView);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.single_row_item, rowItem);
listView.setAdapter(adapter);
}