简体   繁体   English

单击列表视图android上的项目

[英]clicking item on listview android

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; 这是我的代码包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 此代码将列表的elemnt号删除并删除它。但是我想单击并删除或更新。儿子的帮助将非常有用。谢谢

To remove the desired item from the list using the remove() method of your ArrayAdapter . 使用ArrayAdapter的remove()方法从列表中删除所需的项目。

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并在ArrayAdapter上调用notifyDataSetChanged()。

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 添加项目,您可以执行以下操作:单击按钮,从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);

Read official android docs here 1 and 2 for handling click of menu items ... 在此处阅读官方android文档12,以处理菜单项的单击...

What you want to do is set the itemClickListener() . 您要做的是设置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. 从该块,您只需要调用您的delete语句。 Alternatively, you could have your class use the AdapterView.OnItemClickListener interface, and put the routine there, then changing the command to list.setOnItemClickListner(this) ; 或者,您可以让您的类使用AdapterView.OnItemClickListener接口,并将例程放在此处,然后将命令更改为list.setOnItemClickListner(this)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM