如何在项目单击时在对话框内设置textview

时间:2013-12-01 17:31:04

标签: android dialog textview sqlite

这是我的代码,我在过去两天试图做。我只能看到mainActivity中的listView,当点击listitem时,会出现一个alertdialog框,而没有来自textView中数据库的任何数据。我认为Cursor光标不起作用,这就是为什么我无法达到我的目标。有人能建议我解决这个问题吗?提前谢谢。

@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();
        }
    });

}`

2 个答案:

答案 0 :(得分:1)

尝试创建新的textview,而不是在活动布局中找到它。 textView已添加到您的布局中,可能是因为您无法将其添加到对话框

试试这个:

 //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();
        }
    });

答案 1 :(得分:0)

您必须在创建对话框之后设置文本,在之后设置调用alertDialog.show()。