从TextView获取文本通过LongClick嵌套在ListView中

时间:2014-10-17 11:27:09

标签: android sqlite listview

我有一个列表视图,根据用户通过编辑文本输入来填充来自SQLite db的文本。我的代码是:

Cursor cursor = mDbHelper.searchDb((query != null ? query.toString() : "@@@@"));

    if (cursor == null)
    {
    }
    else 
    {
     String[] from = new String[] 
                {
                    COLUMN-1,
                    COLUMN-2,
                    COLUMN-3
                };   
        int[] to = new int[] 
                {     
                    R.id.textView1,
                    R.id.textView2,
                    R.id.textView3
                };

            SimpleCursorAdapter cursorAdapter1 = new      
                 simpleCursorAdapter(this,R.layout.searchResults, cursor, from, to);

            mListView.setAdapter(cursorAdapter1);
            registerForContextMenu(mListView);

和我的上下文菜单

@Override
public boolean onContextItemSelected(MenuItem item) 
{

  AdapterView.AdapterContextMenuInfo info =     
     (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
  int menuItemIndex = item.getItemId();
  String[] menuItems = 
    {
        "Option-1",
        "Option-2"
    };
  String menuItemName = menuItems[menuItemIndex];
  TextView textView4 = (TextView) findViewById(R.id.textView1);
  String text = textView4.getText();
  if(menuItemIndex == 0 )
  {
      Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();  
  }

  return true;
}

我正在尝试通过Toast中的字符串“text”长按项目文本(ListView中的TextView)。但它始终返回列表中第一个可查看项目的文本,而不管单击哪个项目?我无法弄清楚代码中哪里出错了?

3 个答案:

答案 0 :(得分:1)

尝试将lonfClick侦听器添加到listView,并单击该项目的内容。在你的情况下,像:

yourListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View view,
                    int pos, long id) {

               String text = ((TextView)view).getText().toString();

               Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();

                return true;
            }
        }); 

我希望这会有所帮助!!

答案 1 :(得分:0)

为列表视图实现长按侦听器。 请尝试以下代码。希望它对你有用。

 mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view,int arg2, long arg3) {
                 TextView textView4 = (TextView)view.findViewById(R.id.textView1);
                  String text = textView4.getText();
                return false;
            }
        });.

答案 2 :(得分:0)

你必须在getView方法中附加onClickListener。 例如:

 public View getView(int position, View convertView, ViewGroup parent) {

    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.yourTextView);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.anotherView);
    textView.setText("TEST TEXT");

    rowView.setOnLongClickListener(new OnLongClickListener(){
        public boolean onLongClick(    View v){
             // do whatever you want
             return true;
        }
    });
    return rowView;
  }