简体   繁体   中英

Editable ListView Items

I want to Edit my ListView items. for example; I have a listView item i clicked this item and I add string value with edit text afterwards I again click this item and I add new string value alongside to previous string value , and again, again, again. When I click this item I want edit this listitem. How I do that?

Java sourcecode:

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();

            }
        });
    }

}

Save item values in ordering Collection. Set this collection in adapter. Add and remove this values and after call notifyDataSetChanged for adapter.

Try like this.

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

Sorry, but now I can't test this code. So I can not say for sure that it is correct. Try different variants and you'll get.

UPDATE From dialog clickListener you can try this:

@Override
public void onClick(DialogInterface dialog, int which) {
      ArrayAdapter adapter = (ArrayAdapter ) listView.getAdapter();
      String item = (String) listView.getSelectedItem();
      item += "YourText";

     adapter.insert(item, position);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM