我的问题已经解决了。我在对话框中使用了 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();
}