0

「チームのロード」ボタンがあり、クリックすると、データベースにチームのポップアップリストが表示されます。私の質問は 3 つあります。

  1. 現在、ボタンをクリックするとポップアップする AlertDialog がありますが、データベースから AlertDialog の行を取り込む方法が見つかりません。(現在は静的リスト)
  2. この質問の 2 番目の部分は、リストに入力できるようになったら、次のように連結された 2 つの返された列を表示したいということです: "Team Name (City)" または "team_ID - Team Name (City)"。
  3. 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を使用しました。1つはListViewの文字列を格納し、もう1つは対応する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 に答える