0

on my application i want to click the list item and make some changes like deleting or updating the item.but i could not implemented the other codes to my code.so little help will be useful. here is my code package com.example.todolist;

import java.util.ArrayList;
import java.util.Collection;

import android.os.Bundle;
import android.provider.Contacts;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity implements            OnClickListener,OnKeyListener,OnInitListener {
EditText txtitem;
ListView listitem;
TextToSpeech talker;
ArrayList<String> todolist;
ArrayAdapter<String> arrayadapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txtitem = (EditText) findViewById(R.id.txtitem);
    listitem = (ListView) findViewById(R.id.listitem);
    talker = new TextToSpeech(this, this);
    txtitem.setOnKeyListener(this);
    todolist = new ArrayList<String>();
    arrayadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,todolist);
    listitem.setAdapter(arrayadapter);


    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem toAdd = menu.add("AddItem");
    MenuItem toDelete = menu.add("DeleteItem");
    MenuItem toSave = menu.add("SaveItem");
    MenuItem toExit = menu.add("ExitItem");
    MenuItem toUpdate = menu.add("UpdateItem");
    return true;
}

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

    return false;
}

@Override
public void onClick(DialogInterface dialog, int which) {


}

public boolean onOptionsItemSelected(MenuItem item){
    super.onOptionsItemSelected(item);


        if(item.getTitle().equals("AddItem")){
            todolist.add(txtitem.getText().toString());
            arrayadapter.notifyDataSetChanged();
           txtitem.setText("");

        }
        if(item.getTitle().equals("DeleteItem")){
            String x = txtitem.getText().toString();
            int y = Integer.parseInt(x);
            todolist.remove(y-1);
            arrayadapter.notifyDataSetChanged();
           txtitem.setText("");

        }
        if(item.getTitle().equals("SaveItem")){
            say("Save Complete");
            arrayadapter.notifyDataSetChanged();
        }
        if (item.getTitle().equals("ExitItem")){


            talker.speak("Are you sure you want to close this activity?",TextToSpeech.QUEUE_FLUSH,null);    
            onBackPressed();


            }
        if(item.getTitle().equals("UpdateItem")){
            String x = txtitem.getText().toString();
            int y = Integer.parseInt(x);

           arrayadapter.notifyDataSetChanged();
           txtitem.setText(todolist.get(y-1));
           todolist.remove(y-1);

        }
    return true;

}

public void say(String text2say){
    talker.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
}

@Override
    public void onInit(int status) {

}
@Override
public void onDestroy() {
    if (talker != null) {
        talker.stop();
        talker.shutdown();
}

super.onDestroy();

}
    }
@Override
public void onBackPressed() 
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close this activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()

    {
        @Override
        public void onClick(DialogInterface dialog, int which) {

              finish();
              runOnUiThread(new Runnable() {

                  @Override
                  public void run() {
                      say("Bye");
                  }
              });
        }

    })
    .setNegativeButton("No", null)
    .show();

}

this code takes the list's elemnt number and delete's it.but i want to click and delete or update.son little help will be useful.thank you already

salihgueler
  • 2,619
  • 1
  • 18
  • 30

2 Answers2

3

To remove the desired item from the list using the remove() method of your ArrayAdapter.

A possible way to do that would be:

Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);

Another way would be to modify the ArrayList and call notifyDataSetChanged() on the ArrayAdapter.

arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();

To Add items you can do something like this : on a click of button take text from edittext and add it as an item on list

/** Reference to the button of the layout main.xml */
        Button btn = (Button) findViewById(R.id.btnAdd);

        /** Defining the ArrayAdapter to set items to ListView */
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

        /** Defining a click event listener for the button "Add" */
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(R.id.txtItem);
                list.add(edit.getText().toString());
                edit.setText("");
                adapter.notifyDataSetChanged();
            }
        };

        /** Setting the event listener for the add button */
        btn.setOnClickListener(listener);

Read official android docs here 1 and 2 for handling click of menu items ...

Shruti
  • 8,785
  • 12
  • 54
  • 93
  • but how can i bind it with my menu buttons :D – salihgueler Nov 25 '12 at 11:52
  • you can use `onOptionsItemSelected` to add new items in your list ... take a look at this link http://stackoverflow.com/questions/7479992/handling-a-menu-item-click-event-android – Shruti Nov 25 '12 at 12:51
  • bro i tried it already.but i couldn't do it.i mean i can not understand the logic of this.i mean how can i implement my options menu buttons to the on item click listener – salihgueler Nov 25 '12 at 13:37
  • i have added two links in my answer go thru those docs and u'll get the answers – Shruti Nov 25 '12 at 14:31
  • buddy that program crashes when i click the list item now.i mean i read all the material first than i ask you the question.but i couldn't implemented it okey – salihgueler Nov 25 '12 at 19:57
  • Post the logcat .. and tell what error you are getting ..may b i'll be able to help u in resolving it – Shruti Nov 26 '12 at 07:07
  • i changed the type of the code and solve the problem but thanks anyway for your help – salihgueler Nov 26 '12 at 09:34
  • Awesome...u saved my time... :) – Shrey Nov 26 '12 at 11:28
0

What you want to do is set the itemClickListener(). This is done by something like this:

list.setOnItemClickListner(new OnItemClickListener(){
    onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
      //Do stuff here.
    }
});

From that block, you just need to call your delete statement. Alternatively, you could have your class use the AdapterView.OnItemClickListener interface, and put the routine there, then changing the command to list.setOnItemClickListner(this);

PearsonArtPhoto
  • 37,474
  • 17
  • 109
  • 139