2014-09-24 73 views
0

我想在每次單擊列表視圖中的任何文本時在新活動中的TextView上調用setText。例如,如果我點擊包含「Android」的listView中的項目,我希望單詞「Android」出現在新的活動中。從選定的ListViewItem文本填充TextView

查找下面的代碼:

public class ThirdMainActivity extends ActionBarActivity { 

    ListView lvCourses; 
    String[] courses = { "Android", "HTML 5", "J2SE", "JavaScript", "MySQL", "PHP", "CSS", "JQuery", "C#"}; 

    String[] descr = { "Android Course", "HTML 5 Course", "J2SE Course", "JavaScript Course", "MySQL Course", "PHP Course", "CSS Course", "JQuery Course", "C# Course" }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_third_main); 

    lvCourses = (ListView) findViewById(R.id.lvCourses); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, Android.R.layout.simple_list_item_1, courses); 

    final TextView rr = (TextView)findViewById(R.id.setT); 
    lvCourses.setAdapter(adapter); 
    lvCourses.setOnItemClickListener(
     new OnItemClickListener() {  

      @Override 
      public void onItemClick(AdapterView<?> lv, View view, int position, long id) { 

       if(position == 0){ 
        rr.setText(""+descr[0]); 
        Intent e = new Intent(getApplicationContext(),TeMainActivity.class); 
        startActivity(e);       
       } 

       if(position == 1){ 
        rr.setText(""+descr[1]); 
        Intent e = new Intent(getApplicationContext(),TeMainActivity.class); 
        startActivity(e); 
       } 
      } 
     }); 
    } 
} 

回答

1

你並不需要檢查所有位置的狀態。如果你想,當我模擬它給我的錯誤,以這個數據傳遞給新的活動

rr.setText(descr[position]); 
Intent e = new Intent(getApplicationContext(),TeMainActivity.class); 
e.putExtra("desc", descr[position]) 
startActivity(e); 
+0

只是這樣做對onItemClick()

rr.setText(descr[position]); Intent e = new Intent(getApplicationContext(),TeMainActivity.class); startActivity(e); 

「不幸的是DA4停止」 – MKADAIM 2014-09-24 21:29:05