可编辑的ListView项目

时间:2015-04-22 20:45:36

标签: java android android-listview

我想编辑我的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();

            }
        });
    }

}

1 个答案:

答案 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);
}