2012-06-11 39 views
1

我想在OnListItemClick()中的所有行的視圖 實際上,我想要改變textView中的一個textView的文本,當該行被點擊,因爲目前我有兩個textViews。我應該調用OnCreate()方法還是其他? 下面是代碼Android ListView,OnListItemClick查找行的所有視圖

public class List extends ListActivity { 

public ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list); 
    HashMap<String,String> temp = new HashMap<String,String>(); 
    temp.put("first","Strength"); 
    temp.put("second", strength); 

    list.add(temp); 

    setListAdapter(new SimpleAdapter(this,list,R.layout.row_view, new String [] {"first","second"}, new int [] {R.id.rowTextView1, R.id.rowTextView2})); 

} 


@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    HashMap<String,String> temp = new HashMap<String,String>(); 
    list.remove(0); 
    temp.put("first","Strength"); 
    temp.put("second", "low"); 

    list.add(temp); 


    Toast.makeText(this, "you have selected " + list.get(position).get("first"), Toast.LENGTH_SHORT).show(); 
} 

} 

,這裏是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/rowTextView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="20dp" 
    /> 

<TextView 
    android:id="@+id/rowTextView2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="10dp" 
    /> 

</LinearLayout> 

simmilarly我想在onActivityResult()來更改文本

感謝

回答

0

請嘗試以下row_view的XML:

protected void onListItemClick(ListView l, View v, int position, long id) { 
// TODO Auto-generated method stub 
super.onListItemClick(l, v, position, id); 
HashMap<String,String> temp = new HashMap<String,String>(); 
list.remove(0); 
temp.put("first","Strength"); 
temp.put("second", "low"); 

list.add(temp); 

TextView t1 = (TextView) v.findViewById(R.id.rowTextView1);//assuming rowTextView1 is the id of the textview you want to change 
t1.setText("New_Text"); 

Toast.makeText(this, "you have selected " + list.get(position).get("first"), Toast.LENGTH_SHORT).show(); 

}

+0

非常感謝很多人,我想要做的另一件事是做同樣的事情,但在onActivityResult()方法,意味着如果我啓動一些活動,在那裏寫一些文本,當它回來時,我想要文本將被設置 – Haris

+0

也許下面的鏈接可以解決您的問題: http://stackoverflow.com/questions/10990271/how-to-pass-data-to-the-activity-that-is-prior-to-the-之前的活動/ 10990309#10990309 –

+0

那不是解決方案。我通過保存該視圖,然後更新它onActivityResult() – Haris

相關問題