請幫我將兩個ArrayList放在一個ListView中。我只使用兩個listview顯示數據庫中的兩列。我不擅長編程,所以我需要你的幫助。在此先感謝Android中的一個ListView中的兩個ArrayList
這是我的主要活動
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);
}
THIS IS MY DATADB
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;
}
}
在適配器構造函數中發送這兩個列表,並根據list1 + list2的大小循環getView或bindView並相應地膨脹視圖,這對您有所幫助。發佈你已經試過的代碼,以便更有幫助 –
你爲什麼要編寫不同的方法來訪問'firstname'和'lastname'?任何特定目的? –
就像我在我的問題中所說的那樣,我在編程方面並不擅長,所以我在檢索數據時遵循了基本原則。 –