【发布时间】:2015-12-27 19:39:00
【问题描述】:
请帮我把两个数组列表放在一个列表视图中。我只使用两个列表视图显示数据库中的两列。我在编程方面不是那么好,所以我需要你们的帮助。提前致谢
这是我的主要活动
public class MainActivity extends Activity {
DataDB data = new DataDB();
ListView list;
ListView list2;
ArrayAdapter<String> listAdapter;
public MainActivity() {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
list2 = (ListView) findViewById(R.id.listView2);
// set data
ArrayList<String> firstName = new ArrayList<String>();
try {
firstName = data.getFNameDB(this);
} catch (SQLException e) {
e.printStackTrace();
}
listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, firstName);
// set the adapter
list.setAdapter(listAdapter);
ArrayList<String> lastName = new ArrayList<String>();
try {
lastName = data.getLNameDB(this);
} catch (SQLException e) {
e.printStackTrace();
}
listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, lastName);
// set the adapter
list2.setAdapter(listAdapter);
}
这是我的数据库
public class DataDB {
DatabaseHelper con;
ArrayList<String> firstName = new ArrayList<String>();
ArrayList<String> lastName = new ArrayList<String>();
public DataDB() {
}
public ArrayList<String> getFNameDB(Context context) throws SQLException {
this.con = new DatabaseHelper(context);
try {
this.con.createDataBase();
} catch (IOException e) {
;
}
if (!this.con.checkDataBase()) {
return null;
} else {
this.con.openDataBase();
SQLiteDatabase db = this.con.getWritableDatabase();
for (Cursor cursor = db.rawQuery("SELECT firstName FROM tbl_doctor", (String[]) null); cursor.moveToNext(); this.firstName.add(cursor.getString(0))) {
}
this.con.close();
return this.firstName;
}
}
public ArrayList<String> getLNameDB(Context context) throws SQLException {
this.con = new DatabaseHelper(context);
try {
this.con.createDataBase();
} catch (IOException e) {
;
}
if (!this.con.checkDataBase()) {
return null;
} else {
this.con.openDataBase();
SQLiteDatabase db = this.con.getWritableDatabase();
for (Cursor cursor = db.rawQuery("SELECT lastName FROM tbl_doctor", (String[]) null); cursor.moveToNext(); this.lastName.add(cursor.getString(0))) {
}
this.con.close();
return this.lastName;
}
}
【问题讨论】:
-
在适配器构造函数中发送两个列表并循环 getView 或 bindView on size for list1+list2 并相应地膨胀视图,这是给你的提示。发布您尝试过的代码,这样会更有帮助
-
为什么要编写不同的方法来访问
firstname和lastname?有什么具体目的吗? -
就像我在我的问题中所说的那样,我的编程不是那么好,所以我在检索数据时遵循了基础。
标签: java android listview arraylist android-listview