简体   繁体   English

如何在列表视图中获取所选项目的索引和文本视图?

[英]how to get the index and textview of the selected item in listview?

i have a listview from webservice and i want to get the index and textview of the selected item .my list view is 我有一个来自webservice的listview,我想获取所选项目的索引和textview。我的列表视图是

         company                  symbol

         android                   an
         iphone                    ip
         blackderry                bb
            .                       .
            .                       .
            .                       .

so tell me how to get the index and textview of symbol in the listview from webservice.i used two methods 所以告诉我如何从webservice.list中获取列表视图中符号的索引和textview.i使用了两种方法

firstmethod: 第一种方法:

protected void onListItemClick(ListView l, View v, int position,long id) 
{
        super.onListItemClick( l, v, position, id);

        ID = id;

    int selectedPosition = l.getSelectedItemPosition();
        index=String.valueOf(selectedPosition);
         Log.e("index value","index"+index);

   TextView txt=(TextView)findViewById(R.id.symboltext);
        TextView temp=(TextView)v;
        txt.setText(temp.getText());
        Log.e("text",txt.toString());

} }

in above code the logcat shows index is -1 and could not shows the txt. 在上面的代码中,logcat显示的索引为-1,无法显示txt。

Second method: 第二种方法:

    listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
   {
    public void onItemSelected(AdapterView parentView, View childView, int position, long id) 
    {
      String text = ((TextView)childView.getText()).toString();
      //The above text variable has the text value of selected item
      // position will reflect the index of selected item
    }
   public void onNothingSelected(AdapterView parentView) 
   {
   }
});

in above method the listview didnt get onclick action. 在上面的方法中,listview没有执行onclick操作。 so please tell me how to get the index and textview of the selected item in listview. 所以请告诉我如何在列表视图中获取所选项目的索引和文本视图。

Best Regards 最好的祝福

Thanks in advance 提前致谢

Try this code. 试试这个代码。 And Check your logcat. 并检查您的logcat。 It should get you the desired result. 它应该为您带来预期的结果。

protected void onListItemClick(ListView l, View v, int position,long id) 
{
        super.onListItemClick( l, v, position, id);

        ID = id;

       Log.e("index value","index"+position);
       TextView txt = (TextView) v.findViewById(R.id.symboltext);
       Log.e("text",txt.toString());

}

you can use a hashmap to put your string to a listview 您可以使用哈希图将您的字符串放入列表视图

this link will help you to understand how hashmap works and how the strings are saved and retrived from it 该链接将帮助您了解哈希图如何工作以及如何从中保存和检索字符串

I was trying something like this, on a Custom ListView : 我在Custom ListView上尝试过这样的事情:

在此处输入图片说明

My scenario was: 我的情况是:

To fetch three Objects from Server with JSONArrayRequest named as: 要使用名为JSONArrayRequest服务器从服务器中获取三个对象,请执行以下操作:

ID (tvLvDocID)
Title (tvLvDocTitle)
Sub Title (tvLvDocSub Title)

ID is actually the ID of the column of item in MySQL database. ID实际上是MySQL数据库中项目列的ID

What I did was: 我所做的是:

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

                                    int myPosition = position;

                                    String id_doc_from_lv = listViewDoc.getItemAtPosition(myPosition).toString();
                                    Toast.makeText(getApplicationContext(), "ID is : " + id_doc_from_lv, Toast.LENGTH_LONG).show();

                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

But My problem was that it was returning Title of the Custom ListView . 但是我的问题是它正在返回Custom ListView Title And I wanted only and only the ID , which I was also showing in ListView above Title. 我只需要ID ,我也在标题上方的ListView显示了ID (Its another story I will later apply INVISIBLE property to that ID ) . (这是另一个故事,我稍后将在该ID应用INVISIBLE属性)

As I wanted only ID . 因为我只想要ID So I did toke help from this answer : 所以我确实从这个答案中获得了帮助:

When I add these lines in my code: 当我在代码中添加这些行时:

String tv_ID_Str= ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString();

And call this tv_ID_Str as: 并将此tv_ID_Str称为:

Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show();

Aha! 啊哈! Now I can successfully show tvLvDocID and I can retrieve all the information from database on this ID . 现在,我可以成功显示tvLvDocID并且可以从数据库中检索有关该ID所有信息。

Final code looks like this: 最终代码如下所示:

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

                                    int myPosition = position;

                                    String id_doc_from_lv = listViewDoc.getItemAtPosition(myPosition).toString();
                                    // selected item
                                    String tv_ID_Str = ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString();

                                    Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show();

                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

Purpose of my answer is people like me, who spend lot of time to do this. 我回答的目的是像我这样的人,他们花很多时间来做到这一点。 I hope this will help someone. 我希望这会对某人有所帮助。

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

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