我想编辑我的ListView
项目。例如;我有一个listView
项目,我点击了这个项目,之后我添加了string value
edit text
我再次点击此项目,我将新的string value
添加到之前的string value
再一次又一次。当我点击这个项目时,我想编辑这个列表项目。我是怎么做到的?
Java源代码:
public class MainActivity extends Activity {
TextView tvDers;
EditText etDers, etDersSaati;
EditText etNot;
LinearLayout LayoutDers;
ArrayAdapter<String> adapter;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDersEkle = (Button) findViewById(R.id.btnDersEkle);
list = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
etDers = new EditText(MainActivity.this);
//Dialog
AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
build.setTitle("Ders Ekle");
build.setView(etDers);
build.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.add(etDers.getText().toString());
}
});
list.setAdapter(adapter);
final AlertDialog alertDers = build.create();
btnDersEkle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDers.show();
}
});
}
}
答案 0 :(得分:1)
在订购Collection中保存项目值。在适配器中设置此集合。添加和删除此值,并在调用notifyDataSetChanged
后调整适配器。
试试这样。
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String item = (String) parent.getItemAtPosition(int position);
item += "YourText";
ArrayAdapter adapter = (ArrayAdapter ) parent.getAdapter();
adapter.insert(item, position);
}
抱歉,但现在我无法测试此代码。所以我不能肯定地说它是正确的。尝试不同的变种,你就会得到。
<强>更新强> 在对话框clickListener中,您可以尝试:
@Override
public void onClick(DialogInterface dialog, int which) {
ArrayAdapter adapter = (ArrayAdapter ) listView.getAdapter();
String item = (String) listView.getSelectedItem();
item += "YourText";
adapter.insert(item, position);
}