2015-09-30 33 views
0

請幫我將兩個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; 

    } 

} 
+0

在適配器構造函數中發送這兩個列表,並根據list1 + list2的大小循環getView或bindView並相應地膨脹視圖,這對您有所幫助。發佈你已經試過的代碼,以便更有幫助 –

+0

你爲什麼要編寫不同的方法來訪問'firstname'和'lastname'?任何特定目的? –

+0

就像我在我的問題中所說的那樣,我在編程方面並不擅長,所以我在檢索數據時遵循了基本原則。 –

回答

0

剛爲了清除事情,你應該做的是使用一個ListView並顯示firstNam e和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); 

問,如果你需要任何進一步的幫助..

+0

而不是姓氏,名字旁邊出現「true」 –

+0

沒有得到你嗎? –

+0

而不是來自姓氏的數據,在firstname的數據旁邊顯示「true」文本顯示,而不是來自我的數據庫的lastname數據。 –

0

更改您的代碼在活動類填充適配器如下:

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); 

} 
+0

姓氏顯示在firstName的底部。我的目標是在一個列表視圖中顯示它們,並且它們彼此對齊。 –

相關問題