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
  • 3,284
  • 2
  • 22
  • 33

1 Answers1

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
  • 38,970
  • 17
  • 111
  • 142