【问题标题】:Two ArrayList in one ListView in AndroidAndroid中的一个ListView中有两个ArrayList
【发布时间】: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 并相应地膨胀视图,这是给你的提示。发布您尝试过的代码,这样会更有帮助
  • 为什么要编写不同的方法来访问 firstnamelastname ?有什么具体目的吗?
  • 就像我在我的问题中所说的那样,我的编程不是那么好,所以我在检索数据时遵循了基础。

标签: java android listview arraylist android-listview


【解决方案1】:

只是为了清除问题,您应该做的是使用单个 ListView 并在其中显示 firstName 和 lastName,我将为此演示一些代码,我将添加基本部分以供您理解更好的方法,

在您访问 firstName 的方法中,您可以更新它以在单个查询中同时获取 firstNamelastName,如下所示 ->

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

询问您是否需要任何进一步的帮助..

【讨论】:

  • 名字旁边出现“true”而不是姓氏
  • 而不是来自 lastname 的数据,在我的数据库中 firstname 的数据而不是 lastname 的数据旁边显示“真实”文本。
  • 更新了我的答案 -> 将此添加到查询中 -> cursor.getString(cursor.getColumnIndex("lastName"))
  • 仍然不显示:(您所做更改的输出是第一个数据没有真实,而第二个数据有真实,而第三个没有,依此类推......
  • 查看更新,这是我的错误,已纠正,如果有帮助请告诉我
【解决方案2】:

更改代码以在活动类中填充适配器,如下所示:

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

}

【讨论】:

  • 姓氏显示在名字的底部。我的目标是在一个列表视图中显示它们并且它们彼此对齐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 2011-11-11
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多