Reputation: 7
Here is my code, that I am trying to do since last two days. All I can see is the listView in mainActivity and when the listitem is clicked a alertdialog box appears without any datas from database in textView. I think Cursor cursor is not working, that is why I cannot reach to my goal. Could anyone suggest me some good solution for this problem? Thanks in advance.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating a main list view
final ArrayList<String> getTrainingsList = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getTrainingsList);
ListView mylist = (ListView) findViewById(R.id.traininglistView);
mylist.setAdapter(adapter);
String Path = DB_PATH + DB_NAME;
//opening an existing database
final SQLiteDatabase db = SQLiteDatabase.openDatabase(Path, null, 0);
final String selectQuery = "SELECT * FROM training";
//Getting a cursor to fetch data from the database
final Cursor c = db.rawQuery(selectQuery, null);
Log.d("TAG","cursor found");
c.moveToFirst();
Log.d("TAG","Cursor moved to first");
if(c !=null){
//If there are contents in the database,
//then c!=null, so using do-while loop access data in database
do{
String title = c.getString(c.getColumnIndex("title"));
//String description = c.getString(c.getColumnIndex("description"));
//String title_date = title+":"+date;
getTrainingsList.add(title);
c.moveToNext();
}while(!c.isAfterLast());
//update the list
adapter.notifyDataSetChanged();
//closing the database after use
//db.close();
}
//Below is the code to alert alert dialogue window on item clicked
final TextView view1 = (TextView)findViewById(R.id.dialogTextView);
Cursor cursor = db.query(TABLE_TRAINING, new String[] { TRAINING_ID,
TRAINING_TITLE, TRAINING_DESCRIPTION, TRAINING_DATE, TRAINING_LOCATION },
TRAINING_ID + "=?",
new String[] { String.valueOf(TRAINING_ID) }, null, null, null, null);
Log.d("TAG","cursor found again");
if(cursor.getCount() > 0) {
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex("title"));
String description = cursor.getString(cursor.getColumnIndex("description"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String location = cursor.getString(cursor.getColumnIndex("location"));
String allinfos = title+"\n"+description+"\n"+date+"\n"+location;
view1.setText(allinfos);
Log.d("TAG","Successfully set");
}
mylist.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long _id) {
AlertDialog.Builder b= new AlertDialog.Builder(MainActivity.this);
b.setTitle("Title");
b.setView(view1);
b.setPositiveButton("Proceed to training", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(MainActivity.this, TraineeActivity.class));
}
});
b.setNegativeButton("Back to List", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
}
});
}`
Upvotes: 0
Views: 1167
Reputation: 17401
Try to create a new textview instead of finding it in your activity layout. That textView is already added to your layout maybe because of that you are not able to add it to your dialog
Try this:
//Below is the code to alert alert dialogue window on item clicked
final TextView view1 = new TextView(this);
Cursor cursor = db.query(TABLE_TRAINING, new String[] { TRAINING_ID,
TRAINING_TITLE, TRAINING_DESCRIPTION, TRAINING_DATE, TRAINING_LOCATION },
TRAINING_ID + "=?",
new String[] { String.valueOf(TRAINING_ID) }, null, null, null, null);
Log.d("TAG","cursor found again");
if(cursor.getCount() > 0) {
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex("title"));
String description = cursor.getString(cursor.getColumnIndex("description"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String location = cursor.getString(cursor.getColumnIndex("location"));
String allinfos = title+"\n"+description+"\n"+date+"\n"+location;
view1.setText(allinfos);
Log.d("TAG","Successfully set");
}
mylist.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long _id) {
AlertDialog.Builder b= new AlertDialog.Builder(MainActivity.this);
b.setTitle("Title");
b.setView(view1);
b.setPositiveButton("Proceed to training", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(MainActivity.this, TraineeActivity.class));
}
});
b.setNegativeButton("Back to List", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
}
});
Upvotes: 1
Reputation: 23665
You have to set the text after the dialog has been created and after calling alertDialog.show().
Upvotes: 0