我想在单击listItem时更改listItem的值。我已经提供了以下代码。
public class MyListDialogExampleActivity extends Activity implements OnItemClickListener {
ListView myListView;
String itemString = null;
String [] listViewArray = new String[] {"ABC", "DEF", "GHI", "JKL"};
MyArrayAdapter listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView = (ListView) findViewById (R.id.myListView);
listAdapter = new MyArrayAdapter(this, R.layout.list_row, R.id.list_tv, listViewArray);
myListView.setAdapter(listAdapter);
myListView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("onItemClick", arg2+"");
String str = showListDialog();
// i want to change the selected list item with String str
}
public String showListDialog () {
final CharSequence[] items = {"1", "2", "3", "4"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a number");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
itemString = items[item].toString();
}
});
AlertDialog alert = builder.create();
alert.show();
return itemString;
}
}
答案 0 :(得分:1)
您无法在运行时更改数组
所以你必须使用List(http://developer.android.com/reference/java/util/List.html)
答案 1 :(得分:1)
你需要在onItemClick上使用发送给你的参数,这样你就可以在你使用的数组中取第i个元素,改变它的值,最后通知适配器使用notifyDatasetChanged更改了数据。