无法将listView所选项目传递给textview

时间:2014-02-22 11:40:52

标签: java android eclipse listview

我创建了两个对话框,第一个打开第二个对话框,它将访问listview并且确实如此,第二个对话框选择listView中的特定项目并将该项目传输到第一个对话框中的textview ...我搞砸了我的逻辑,我怎么能这样做?

这是第一个对话框:

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_students_dialog);

            imb1=(ImageButton)findViewById(R.id.im1);
            imb2=(ImageButton)findViewById(R.id.im2);
            imb3=(ImageButton)findViewById(R.id.im3);

            text1=(TextView)findViewById(R.id.Text1);
            text2=(TextView)findViewById(R.id.Text2);
            text3=(TextView)findViewById(R.id.Text3);
            /* Get values from Intent */
            Intent intent = getIntent();

            String name  = intent.getStringExtra("Title1");
            text1.setText(name);


            ie1=(EditText)findViewById(R.id.edit1);
            ib=(Button)findViewById(R.id.dialog_button_cancel);
            ib1=(Button)findViewById(R.id.dialog_button_ok);

            ib.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent=new Intent(InsertStudent.this, StudentsActivity.class);
                    startActivity(intent);

                }
            });

            ib1.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String data1 = ie1.getText().toString();
                    String data2 = ie2.getText().toString();
                    mySQLiteAdapter.insertSchool(data1, data2);
                    updateList();
                    Intent myIntent = new Intent(InsertStudent.this, StudentsActivity.class);
                    startActivity(myIntent);

                }
            }); 
            imb1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent(InsertStudent.this, Add1.class);
                    myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(myIntent);
                }
            }); 
            imb2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent(InsertStudent.this, Add2.class);
                    startActivity(myIntent);
                }
            }); 
            imb3.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent(InsertStudent.this, Add3.class);
                    startActivity(myIntent);
                }
            }); 
      }

     private void updateList(){
            cursor.requery();
     }
}

这是第二个对话框:

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.a_add_class_students);
            listContent = (ListView)findViewById(R.id.listView1);
            mySQLiteAdapter = new Database(this);
            mySQLiteAdapter.openToWrite();

            cursor = mySQLiteAdapter.queueSchoolAll();

            button1 = (Button) findViewById(R.id.Button01);
            button2 = (Button) findViewById(R.id.dialog_button_ok);
            // Capture button clicks
            button1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) { 
                    Intent myIntent = new Intent(Add1.this, InsertSubject.class);
                    startActivity(myIntent);
                }
            }); 
            cursor.requery();
            String[] from = new String[]{Database.KEY_ID2, Database.KSCHOOL, Database.KSCHOOLCODE};
            int[] to = new int[]{R.id.rid, R.id.rt1, R.id.rt2};

            cursorAdapter =
                new SimpleCursorAdapter(this, R.layout.row_school, cursor, from, to);
            listContent.setAdapter(cursorAdapter);
            listContent.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
                    final int item_id = cursor.getInt(cursor.getColumnIndex(Database.KEY_ID2));
                    sdid.sdid(item_id);

                    Intent intent=new Intent(Add1.this,DetailsSchool.class);
                    startActivity(intent);  
                    }

                });
            cursor.requery();
                listContent.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    // TODO Auto-generated method stub

                    Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
                    final int item_id = cursor.getInt(cursor.getColumnIndex(Database.KSCHOOLCODE));

                    Intent intent  = new Intent(getApplicationContext(),InsertStudent.class);
                    intent.putExtra("Title1", item_id);
                    startActivity(intent);
                }
            });
                cursor.requery();
     }
     private void updateList(){
            cursor.requery();
     }

}

1 个答案:

答案 0 :(得分:0)

你在第二个对话框中为同一个列表视图调用了两次setOnItemClickListener