从setOnItemClickListener一个列表视图项得到一个字符串 [英] getting a string from a listview item in setOnItemClickListener

查看:125
本文介绍了从setOnItemClickListener一个列表视图项得到一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我怎样才能从点击的ListView的项目的字符串。比如我在这个格式的条目。

# - 约翰史密斯

john@gmail.com

3451234532

纽约

我想约翰·史密斯。我可以在该位置上的项目,但我不知道了。

我的codeS:

 包com.example.deneme;
进口android.app.Activity;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.SimpleCursorAdapter;
进口android.widget.TextView;
进口android.widget.Toast;
公共类活性2延伸活动{私人SQLiteAdapter mySQLiteAdapter;
 ListView控件的listContent;
 SimpleCursorAdapter的CursorAdapter;
 光标光标;
 按钮buttonDeleteAll,buttonDeleteRow;公共无效的onCreate(捆绑savedInstanceState){super.onCreate(savedInstanceState);
的setContentView(R.layout.activity2);
buttonDeleteAll =(按钮)findViewById(R.id.deleteall);
buttonDeleteRow =(按钮)findViewById(R.id.deleterow);
=的listContent(ListView控件)findViewById(R.id.contentlist);
listContent.setClickable(真);
buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener);
buttonDeleteRow.setOnClickListener(buttonDeleteRowOnClickListener);
mySQLiteAdapter =新SQLiteAdapter(本);
mySQLiteAdapter.openToWrite();光标= mySQLiteAdapter.queueAll();
的String [] =由新的String [] {SQLiteAdapter.COLUMN_NAME,SQLiteAdapter.COLUMN_EMAIL,SQLiteAdapter.COLUMN_PHONE,SQLiteAdapter.COLUMN_ADDRESS};INT []为= INT新[] {R.id.text1,R.id.text2,R.id.text3,R.id.text4};CursorAdapter的=新SimpleCursorAdapter(这一点,R.layout.row,光标,从,到);
listContent.setAdapter(CursorAdapter的);listContent.setOnItemClickListener(新OnItemClickListener(){    公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){        字符串str =((的TextView)ARG1).getText()的toString()。
        Toast.makeText(Activity2.this,STR,Toast.LENGTH_SHORT).show();
    }
});}
   Button.OnClickListener buttonDeleteRowOnClickListener =新Button.OnClickListener(){       公共无效的onClick(查看为arg0){     //字符串n = inputContent1.getText()的toString()。
     // mySQLiteAdapter.deleteRow(N);
           updateList();
           Toast.makeText(Activity2.this项中删除,Toast.LENGTH_SHORT).show();
       }
        };        Button.OnClickListener buttonDeleteAllOnClickListener
        =新Button.OnClickListener(){
       公共无效的onClick(查看为arg0){        mySQLiteAdapter.deleteAll();
        updateList();
        Toast.makeText(Activity2.this,所有参赛作品已删除,Toast.LENGTH_SHORT).show();       }
        };        私人无效updateList(){
              cursor.requery();
               }
}

用自己的方式后,我得到了HESE错误:

 七月七日至27日:52:15.726:E / AndroidRuntime(856):致命异常:主要
7月7日至27日:52:15.726:E / AndroidRuntime(856):java.lang.ClassCastException:android.widget.LinearLayout
7月7日至27日:52:15.726:E / AndroidRuntime(856):在com.example.deneme.Activity2 $ 3.onItemClick(Activity2.java:58)
7月7日至27日:52:15.726:E / AndroidRuntime(856):在android.widget.AdapterView.performItemClick(AdapterView.java:284)
7月7日至27日:52:15.726:E / AndroidRuntime(856):在android.widget.ListView.performItemClick(ListView.java:3513)
7月7日至27日:52:15.726:E / AndroidRuntime(856):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1812)


解决方案

嗯,这取决于您填充你的列表视图使用。如果这些都是TextView中的那么所有你需要做的是这样的:

 ((的TextView)ARG1).getText()。的toString()

基本上查看ARG1 是点击查看。既然你已经填充的列表,你知道的看法是什么,因为你是谁创造并返回它的人(在你的适配器):

 公共查看getView(INT位置,查看convertView,父母的ViewGroup)

因此​​,无论是景观的类型,检查它的文档来了解如何获取文本出来的

编辑:

好吧,你正在使用的回报为每一行的LinearLayout光标适配器。它有几个TextViews,一个用于选定的每一列。要获得第一列你这样做:

 的LinearLayout行=(的LinearLayout)((的LinearLayout)ARG1).getChildAt(0);
TextView的柱=(TextView的)row.getChildAt(0);
字符串文本= column.getText()的toString()。

如果你想要一个不同的列只需更改参数 getChildAt(我)

I was wondering how I can get a string from a clicked listview's item. For example I have an entry in this format.

# - John Smith

john@gmail.com

3451234532

New York

I want to get John Smith. I can get the item on that position but I don't know the rest.

My codes:

package com.example.deneme;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class Activity2 extends Activity {

private SQLiteAdapter mySQLiteAdapter;
 ListView listContent;
 SimpleCursorAdapter cursorAdapter;
 Cursor cursor;
 Button buttonDeleteAll,buttonDeleteRow;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);


buttonDeleteAll = (Button)findViewById(R.id.deleteall);
buttonDeleteRow = (Button)findViewById(R.id.deleterow); 
listContent = (ListView)findViewById(R.id.contentlist);
listContent.setClickable(true);
buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener);
buttonDeleteRow.setOnClickListener(buttonDeleteRowOnClickListener);


mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();



cursor = mySQLiteAdapter.queueAll();
String[] from = new String[]{ SQLiteAdapter.COLUMN_NAME, SQLiteAdapter.COLUMN_EMAIL,SQLiteAdapter.COLUMN_PHONE, SQLiteAdapter.COLUMN_ADDRESS};

int[] to = new int[]{R.id.text1, R.id.text2,R.id.text3, R.id.text4};

cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);



listContent.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

        String str = ((TextView)arg1).getText().toString();
        Toast.makeText(Activity2.this, str, Toast.LENGTH_SHORT).show();
    }
});



}


   Button.OnClickListener buttonDeleteRowOnClickListener = new Button.OnClickListener(){

       public void onClick(View arg0) {

     //    String n = inputContent1.getText().toString();
     //    mySQLiteAdapter.deleteRow(n);
           updateList();
           Toast.makeText(Activity2.this, "Entry Deleted", Toast.LENGTH_SHORT).show();      


       }
        };

        Button.OnClickListener buttonDeleteAllOnClickListener
        = new Button.OnClickListener(){
       public void onClick(View arg0) {

        mySQLiteAdapter.deleteAll();
        updateList();
        Toast.makeText(Activity2.this, "All Entries Deleted", Toast.LENGTH_SHORT).show();      

       }
        };

        private void updateList(){
              cursor.requery();
               }
}

After using your way, I got hese errors:

07-27 07:52:15.726: E/AndroidRuntime(856): FATAL EXCEPTION: main
07-27 07:52:15.726: E/AndroidRuntime(856): java.lang.ClassCastException:              android.widget.LinearLayout
07-27 07:52:15.726: E/AndroidRuntime(856):  at  com.example.deneme.Activity2$3.onItemClick(Activity2.java:58)
07-27 07:52:15.726: E/AndroidRuntime(856):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-27 07:52:15.726: E/AndroidRuntime(856):  at android.widget.ListView.performItemClick(ListView.java:3513)
07-27 07:52:15.726: E/AndroidRuntime(856):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)

解决方案

Well, it depends on what you've populated your listview with. If those are TextView's then all you have to do is this:

((TextView)arg1).getText().toString()

Essentially View arg1 is the clicked View. Given that you've populated that list, you know what that view is, because you're the one who created and returned it in(in your adapter):

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

Therefore, whatever the type of that View, check its the documentation to see how to get the text out of it

Edit:

Well, the cursor adapter you're using returns a LinearLayout for each row. It has several TextViews, one for each column that's selected. To get the first column you do this:

LinearLayout row = (LinearLayout)((LinearLayout)arg1).getChildAt(0);
TextView column = (TextView)row.getChildAt(0);
String text = column.getText().toString();

If you want a different column just change the argument to getChildAt(i)

这篇关于从setOnItemClickListener一个列表视图项得到一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆