0

在我的应用程序上,我想单击列表项并进行一些更改,例如删除或更新项。但我无法在我的代码中实现其他代码。所以很少有帮助会很有用。这是我的代码包 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();

}

此代码采用列表的元素编号并删除它。但我想单击并删除或更新。小帮助将是有用的。谢谢你

4

2 回答 2

3

使用您的ArrayAdapter.

一种可能的方法是:

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

另一种方法是修改 ArrayList 并在 ArrayAdapter 上调用 notifyDataSetChanged()。

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

添加项目,您可以执行以下操作:单击按钮从 edittext 获取文本并将其添加为列表中的项目

/** 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);

在此处阅读官方 android 文档12以处理单击菜单项...

于 2012-11-25T11:38:56.430 回答
0

您要做的是设置itemClickListener()。这是通过以下方式完成的:

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

从该块中,您只需要调用您的删除语句。或者,您可以让您的类使用 AdapterView.OnItemClickListener 接口,并将例程放在那里,然后将命令更改为list.setOnItemClickListner(this);

于 2012-11-25T11:39:35.327 回答