如何从列表视图中获取文本

时间:2017-03-17 15:54:15

标签: android listview

我的listview有两个textview。我想从其中一个文本视图中获取文本onItemclick。我尝试过使用下面的代码,但这就是我得到的" User @ 41f0a6b0"

 allusers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           // TextView textView = (TextView) view.findViewById(R.id.nameHolder);
            String name = adapterView.getItemAtPosition(i).toString();

我做错了什么?

我的getView()方法:

 public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.adapter_listing, null, false);
            holder = new ViewHolder();
holder.fullname = (TextView) convertView.findViewById(R.id.user_fullname);
  convertView.setTag(holder);
 } else {
            holder = (ViewHolder) convertView.getTag();
        }

5 个答案:

答案 0 :(得分:0)

这是正确的方法:

        allusers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    TextView textView = (TextView) view.findViewById(R.id.nameHolder);
                    String text = textView.getText().toString();
                    Toast.makeText(getApplicationContext(), "Selected item: " + text + " - " + position, Toast.LENGTH_SHORT).show();
                }
        }

答案 1 :(得分:0)

也许:

String name = adapterView.getItemAtPosition(i).getText.toString();

答案 2 :(得分:0)

您想要获取的项目类似于用户对象? 然后尝试类似的事情:

String name = adapterView.getItemAtPosition(i).getUserName();

如果对象中有函数getUserName

答案 3 :(得分:0)

@Kamesh 谢谢先生,你帮助我解决了这个问题。这是我绕过它的方式:

  allusers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        TextView textView = (TextView)   view.findViewById(R.id.nameHolder);
            String name = adapterView.getItemAtPosition(i).toString();

   User user= (User) allusers.getItemAtPosition(i);

Toast.makeText(getApplicationContext(),“Selected item:”+ user.getFirstname()+“ - ”+ i,Toast。                         LENGTH_SHORT).show();

答案 4 :(得分:0)

您应该创建要从中获取文本的TextView, 从setOnItemClickListener处获取的gettext之后

     alluser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            TextView yourgetTextview = (TextView) findViewById(R.id.yourgettextviewid);
            String yourtext = bookkname.getText().toString();
        }
    });