从数据库填充AlertDialog,返回数据库值(不是位置)

时间:2012-08-03 01:06:45

标签: android listview android-sqlite alertdialog

我有一个按钮,“加载团队”,点击后,我想在数据库中有一个团队的弹出列表。我的问题有三个:

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

    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更好,请告诉我。

1 个答案:

答案 0 :(得分:0)

我的问题已经解决了。我在Dialog中使用了ListView。我使用了2个ArrayLists,一个存储了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();
}