我想获取对话框表单数据库中的项目列表。 我有关于Mainactivity的“mylist”。当我单击一个列表项时,我想要列表 对话框中的项目。
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
ArrayList<String> getdialoglist = new ArrayList<String>();
final ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getdialoglist);
ListView dialoglist = (ListView) findViewById(R.id.dialogListView);
dialoglist.setAdapter(adapter1);
String[] sColumns = {"_id", "title","description","date","location","trainer_id"};
final Cursor cursor = db.query("training", sColumns, null, null, null, null, null);
mylist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long _id) {
builder.setTitle("Title");
builder.setCursor(cursor, null, null);
builder.setAdapter(adapter1, null);
builder.create();
builder.show();
}
});
答案 0 :(得分:0)
我设法从预先填充的数据库中获取Items列表,并在onItemClick上显示对话框,其中包含有关这些项的更多信息。希望它可以帮助你:))
public class MainActivity extends Activity{
private ListView trainingListView;
private ListAdapter trainingListAdapter;
private ArrayList<Training> trainingArrayList;
//Lists that contain title of trainings
ArrayList<String> titleList = new ArrayList<String>();
DatabaseHelper helper = new DatabaseHelper(this);
//private static final String DB_PATH = "data/data/com.Areva.areva_attendance/databases/";
final Context context = this;
// Database Name
//private static String DB_NAME = "attendance.sqlite";
private Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
trainingListView = (ListView) findViewById(R.id.traininglistView);
trainingArrayList = new ArrayList<Training>();
// Create a list that contains only title of the training
trainingListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getTrainingsList());
trainingListView.setAdapter(trainingListAdapter);
trainingListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long id) {
// arg2 = the id of the item in our view (List/Grid) that we clicked
// arg3 = the id of the item that we have clicked
final Training data = trainingArrayList.get(arg2);
Toast.makeText(getApplicationContext(), "You clicked on position : " + arg2 + " and ID : " + id, Toast.LENGTH_LONG).show();
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle(data.getTitle());
TextView description = (TextView) dialog.findViewById(R.id.textView1);
description.setText("Description: "+ data.getDescription());
TextView date = (TextView) dialog.findViewById(R.id.textView2);
date.setText("Date: "+ data.getDate());
TextView location = (TextView) dialog.findViewById(R.id.textView3);
location.setText("Location: "+ data.getLocation());
Button back_btn = (Button) dialog.findViewById(R.id.button1);
back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
Button start_btn = (Button) dialog.findViewById(R.id.button2);
start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, TraineeActivity.class);
//intent.putExtra("data",trainingListView.getSelectedItem().toString());
intent.putExtra("trainingId", Training.getId());
intent.putExtra("title", data.getTitle().toString());
MainActivity.this.startActivity(intent);
}
});
dialog.show();
}
});
}
@SuppressWarnings("deprecation")
private ArrayList<String> getTrainingsList() {
//Open database helper Class
DatabaseHelper helper = new DatabaseHelper(this);
// Then we need to get a readable database
SQLiteDatabase db = helper.getReadableDatabase();
//Cursor cursor = db.rawQuery("SELECT t.* FROM training t JOIN attendance a ON t._id=a.training_id=?;",
// new String[] {Integer.toString(training_id)});
final String selectQuery = "SELECT * FROM training";
//Getting a cursor to fetch data from the database
final Cursor cursor = db.rawQuery(selectQuery, null);
startManagingCursor(cursor);
while(cursor.moveToNext()){
Training training = new Training();
training.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TRAINING_ID)));
training.setTitle(cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRAINING_TITLE)));
training.setDescription (cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRAINING_DESCRIPTION)));
training.setDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRAINING_DATE)));
training.setLocation(cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRAINING_LOCATION)));
//String Trainer_id = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRAINING_TRAINER_ID));
//Pass to the arraylist
trainingArrayList.add(training);
// But we need a List of String to display in the ListView also.
//That is why we create "titleList"
titleList.add(training.getTitle());
}
return titleList;
}
}