Editable ListView Items

I want to edit my items ListView

. eg; I have an item ListView

that I clicked on this item and add string value

using edit text

after that, I click on that item again and add a new one string value

next to the previous one string value

and again again again. When I click this item, I want to edit this list. How do I do it?

Java Source Package:

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

            }
        });
    }

}

      

+3


source to share


1 answer


Storing the values ​​of elements in the Collection order. Install this collection to the adapter. Add and remove these values ​​after the call notifyDataSetChanged

for the adapter.

Try it.

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 cannot verify this code. Therefore, I cannot say for sure that this is correct. Try different options and you will get it.

UPDATE In the clickListener dialog, 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);
}

      

+1


source







All Articles