0
我有ListView.I也有按鈕。它們在RelativeLayout中。我希望能夠通過onClick(View v)按鈕來更改所選ListView項目的TexView文本。 ListView的佈局:通過按鈕編輯ListView裏面的ListView文本
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
主要活動:
public class MainActivity extends Activity {
private TextView mTextView;
private ListView my_list;
private MyArrayAdapter adapt;
Button mybutton;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] mContacts = {
"Jacob Anderson dsfd sdfdf sdfsd sdfds", "Emily Duncan", "Michael Fuller",
"Emma Greenman", "Joshua Harrison", "Madison Johnson",
"Matthew Cotman", "Olivia Lawson", "Andrew Chapman",
"Daniel Honeyman", "Isabella Jackson", "William Patterson",
"Joseph Godwin", "Samantha Bush", "Christopher Gatema55"};
setContentView(R.layout.activity_main);
my_list=(ListView)findViewById(R.id.listView1);
adapt=new MyArrayAdapter(this, mContacts, 0);
my_list.setAdapter(adapt);
mybutton = (Button)findViewById(R.id.button1);
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
,如果我想編輯的TextView這樣我應該怎麼做:
SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
TextViewOfSelectedItem.setText(text, BufferType.SPANNABLE);
我怎樣才能得到的TextViewOfSelectedItem或TextView的ListView項目通過項目位置?
您可以更改字符串數組中的字符串(單擊按鈕後)並重新加載適配器。 – iTurki 2012-08-09 17:40:22
我已經更新了這個問題。我想訪問一個TextView進行編輯 – Andrew 2012-08-09 18:04:04
使用listview項目(這是view)的'getChildAt(int index)'方法。通常你會在調試器中看到這些項目。 – 2012-08-09 18:12:22