[英]Two ArrayList in one ListView in Android
請幫助我將兩個arraylist放在一個listview中。 我僅使用兩個listview顯示數據庫中的兩列。 我的編程能力不太好,所以我需要您的幫助。 提前致謝
這是我的主要活動
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;
}
}
更改您的代碼以在活動類中填充適配器,如下所示:
public class MainActivity extends Activity {
DataDB data = new DataDB();
ListView list;
ArrayAdapter<String> listAdapter;
public MainActivity() {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
ArrayList<String> finalList = new ArrayList<String>();
// set data
ArrayList<String> firstName = new ArrayList<String>();
try {
firstName = data.getFNameDB(this);
} catch (SQLException e) {
e.printStackTrace();
}
ArrayList<String> lastName = new ArrayList<String>();
try {
lastName = data.getLNameDB(this);
} catch (SQLException e) {
e.printStackTrace();
}
finalList.addAll(firstName);
finalList.addAll(lastName);
listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, finalList);
// set the adapter
list.setAdapter(listAdapter);
}
只是為了清楚起見,您應該做的是使用單個ListView
並在其中顯示firstName和lastName,為此我將演示一些代碼,還將添加一些必要的部分供您更好地理解事物,
在您訪問firstName
的方法中,可以更新該方法以在單個查詢中同時獲取firstName
和lastName
,如下所示->
public ArrayList<String> getNameDB(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,lastName FROM tbl_doctor", (String[]) null); cursor.moveToNext(); this.firstName.add(cursor.getString(0)+" "+cursor.getString(1)) {
}
this.con.close();
return this.firstName;
}
}
現在,您可以按以下方式在適配器中使用此列表,
ArrayList<String> names = new ArrayList<String>();
try {
names = data.getNameDB(this);
} catch (SQLException e) {
e.printStackTrace();
}
listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, names);
// set the adapter
list.setAdapter(listAdapter);
詢問是否需要任何進一步的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.