点击listview android上的项目

时间:2012-11-25 11:32:20

标签: android android-listview

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

}

此代码获取列表的元素号并删除它。但我想点击并删除或更新。小帮助将有用。谢谢你已经

2 个答案:

答案 0 :(得分:3)

使用ArrayAdapter的remove()方法从列表中删除所需的项目。

可能的方法是:

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,以便处理菜单项的点击...

答案 1 :(得分:0)

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

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

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