0

我有一个“加载团队”按钮,单击该按钮时,我希望在数据库中弹出一个团队列表。我的问题有三个:

  1. 目前我有一个 AlertDialog 在单击按钮时弹出,但我找不到如何从数据库中填充 AlertDialog 的行。(目前为静态列表)
  2. 这个问题的第二部分是,一旦我可以填充列表,我想显示两个连接在一起的返回列,如下所示:“团队名称(城市)”或“团队 ID - 团队名称(城市)”。
  3. 第三,当他们单击该项目时,我希望它从查询中返回“team_id”列值,而不是列表位置或列表字符串值。

这可能吗?这是我用静态值填充列表的代码:

final CharSequence[] team_list = {"Team 1", "Team 2", "Team 3"};

final AlertDialog.Builder load_builder = new AlertDialog.Builder(this);
load_builder.setTitle("@string/pick_team");
load_builder.setItems(team_list, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), team_list[item], Toast.LENGTH_SHORT).show();
        // I will sent team_id as parameter to query, to get all data for that team
    }
});

我进行了广泛的搜索,但似乎找不到正确的答案。如果这作为弹出式 ListView 和/或带有自己的 layout.xml 更好,请告诉我。

4

1 回答 1

0

我的问题已经解决了。我在对话框中使用了 ListView。我使用了 2 个 ArrayList,一个存储 ListView 的字符串,另一个存储相应的 ID(具有匹配的数组索引)。

team_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>

ManageTeams.java:

ArrayList<String> team_results = new ArrayList<String>();
ArrayList<String> team_results_id = new ArrayList<String>();
ListView team_listView;
Dialog listDialog;
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.manage_teams);
...
    Button load_btn=(Button)findViewById(R.id.load_team_btn);
    load_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getTeams();
        }
    });
...
}

public void getTeam(String selected_team_id)
{
    Team team;

    DatabaseHelper db = new DatabaseHelper(this);
    team = db.getTeam(selected_team_id);
    ...
}

public void getTeams()
{
    final String colTeamID="team_id";
    final String colTeamName="team_name";
    final String colCity="city";
    final String colState="state";

    team_results.clear();
    team_results_id.clear();

    DatabaseHelper db = new DatabaseHelper(this);
    Cursor team_list = db.getTeams();

    if (team_list != null )
    {
        if (team_list.moveToFirst())
        {
            int counter = -1;
            do {
                counter = counter + 1;
                // assign strings to array for ListView
            } while (team_list.moveToNext());
        }
    }

    listDialog = new Dialog(this);
    listDialog.setTitle("Select Team");
    LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = li.inflate(R.layout.team_list, null, false);
    listDialog.setContentView(v);
    listDialog.setCancelable(true);

    team_listView = (ListView) listDialog.findViewById(R.id.listview);
    team_listView.setOnItemClickListener(this);
    team_listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, team_results));
    //now that the dialog is set up, it's time to show it
    listDialog.show();

    db.close();
}
...
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3)
{
    Log.i("ManageTeams", "onItemSelected");
    getTeam(team_results_id.get(pos));
    listDialog.dismiss();
}
于 2012-08-07T02:51:27.927 回答