如何获取listview中所选项目的索引和textview?

时间:2010-12-30 05:56:08

标签: android web-services listview

我有来自webservice的列表视图,我想获取所选项目的索引和textview。我的列表视图是

         company                  symbol

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

请告诉我如何从webservice.i中使用两种方法获取listview中符号的索引和textview

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());

}

在上面的代码中,logcat显示索引为-1,无法显示txt。

第二种方法:

    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) 
   {
   }
});

在上面的方法中,listview没有得到onclick动作。所以请告诉我如何获取listview中所选项目的索引和textview。

最好的问候

提前致谢

3 个答案:

答案 0 :(得分:1)

试试这段代码。并检查您的logcat。它应该会得到你想要的结果。

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());

}

答案 1 :(得分:0)

您可以使用hashmap将字符串放入listview

this link将帮助您了解hashmap的工作原理以及如何保存和从中恢复字符串

答案 2 :(得分:0)

我在Custom ListView

上尝试这样的事情

enter image description here

我的情景是:

从服务器获取三个对象,JSONArrayRequest命名为:

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

ID实际上是MySQL数据库中项目列的ID

我做的是:

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();
                        }

但我的问题是它正在返回Title的{​​{1}}。 我只想要Custom ListView,我也在ID以上的标题中显示。 (另一个故事我稍后会将ListView属性应用于INVISIBLE

我只想ID。所以我从this answer获得了帮助:

当我在代码中添加这些行时:

ID

并将此String tv_ID_Str= ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString(); 称为:

tv_ID_Str

啊哈!现在我可以成功显示Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show(); ,我可以在此tvLvDocID上检索数据库中的所有信息。

最终代码如下所示:

ID

我的答案的目的是像我这样的人,他们花了很多时间来做这件事。我希望这会对某人有所帮助。

相关问题