简体   繁体   English

按钮单击方法如何找出在ListView中选择了哪个项目?

[英]How can a button click method find out which item is selected in a ListView?

I have a single screen with a bank of buttons below a ListView. 我在ListView下面有一个带有一排按钮的单一屏幕。 Entries on the ListView light up in orange when I scroll so I assume that are selected. 当我滚动时,ListView上的条目会以橙色点亮,因此我假设已选中。 When I then press the "Delete" button I want the onClickListener to remove the currently selected entry. 然后,当我按下“删除”按钮时,我希望onClickListener删除当前选定的条目。 But getSelectedItemPosition() always gives me -1. 但是getSelectedItemPosition()总是给我-1。 If I can't hope to use the GUI controls in this way, please give me another way of getting the same result. 如果我不希望以这种方式使用GUI控件,请给我另一种获得相同结果的方法。

I have even tried setting the onClickListener of the List View to store the index before the button is pressed (in case pressing the button unselects the entry) but even that is always -1 it seems. 我什至尝试设置List View的onClickListener来存储在按下按钮之前的索引(以防按下按钮取消选择条目),但看起来总是-1。

Here's the code (without the modification which didn't work) 这是代码(没有生效的修改)

package com.bayley;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;

/**
 *
 * @author p0074564
 */
public class September extends Activity {

 /** Called when the activity is first created. */


 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  final ListView myListView = (ListView) findViewById(R.id.myListView);
  Button addButton = (Button) findViewById(R.id.AddButton);
  Button deleteButton = (Button) findViewById(R.id.DeleteButton);
  final EditText editText = (EditText) findViewById(R.id.myEditText);

  final ArrayList<String> todoItems = new ArrayList<String>();
  todoItems.add("Monday");
  todoItems.add("Tuesday");
  todoItems.add("Wednesday");

  final ArrayAdapter<String> aa =
   new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, todoItems);
  myListView.setAdapter(aa);

  addButton.setOnClickListener(new 
    Button.OnClickListener() {
   public void onClick(View v) {
    todoItems.add(editText.getText().toString());
    aa.notifyDataSetChanged();
   }
  });


  deleteButton.setOnClickListener(new 
    Button.OnClickListener() {
   public void onClick(View v) {
    // always returns -1 unfortunately ie nothing is ever selected
    int index = myListView.getSelectedItemPosition();
    if (index >= 0) {
     todoItems.remove(index);
    }
    aa.notifyDataSetChanged();
   }
  });



 }
}

As I already mentioned in my comment I don't know if you can attach a OnFocusChangedListener to the items of a list, but I pretty sure that is possible some how, although it won't really help you. 正如我在评论中已经提到的那样,我不知道您是否可以将OnFocusChangedListener附加到列表的项目上,但是我可以肯定这是可行的,尽管它并不能真正帮助您。

But perhaps the both option below will might be interesting for you. 但是下面的两个选项可能对您来说很有趣。

  1. Implement a item context menu which appears when you long click an item. 实现一个项目上下文菜单,该菜单在您长按一个项目时出现。 In this context menu you can provide a delete action. 在此上下文菜单中,您可以提供删除操作。 You will see this behavior on many different Android apps which handle some sort of lists. 您会在许多处理某种列表的Android应用程序上看到此行为。 Have a look at this blog post . 看一下这篇博客文章

  2. Make you list to be capable of selecting multiple items. 使您能够选择多个项目的列表。 See this question for more infos. 有关更多信息,请参见此问题 By this way you can delete multiple items at once. 这样,您可以一次删除多个项目。

i some what modified ur code.. its working 我一些修改了您的代码..其工作

public class Selectlistitem extends Activity {
    int pos;

 /** Called when the activity is first created. */


 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  final ListView myListView = (ListView) findViewById(R.id.widget34);
  Button addButton = (Button) findViewById(R.id.btnadd);
  Button deleteButton = (Button) findViewById(R.id.btnremove);
  final EditText editText = (EditText) findViewById(R.id.txt1);

  final ArrayList<String> todoItems = new ArrayList<String>();
  todoItems.add("Monday");
  todoItems.add("Tuesday");
  todoItems.add("Wednesday");

  final ArrayAdapter<String> aa =
   new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, todoItems);
  myListView.setAdapter(aa);

  addButton.setOnClickListener(new
    Button.OnClickListener() {
   public void onClick(View v) {
    todoItems.add(editText.getText().toString());
    aa.notifyDataSetChanged();
   }
  });

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
        aa.getItem(pos);
        editText.setText(""+pos);
    }
});
  deleteButton.setOnClickListener(new
    Button.OnClickListener() {
   public void onClick(View v) {
    // always returns -1 unfortunately ie nothing is ever selected
//    int index = myListView.getCheckedItemPosition();
      int index=pos;
    if (index >= 0) {
     todoItems.remove(index);
    }
    editText.setText(""+index);
    aa.notifyDataSetChanged();
   }
  });



 }
}

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

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