1

我想从列表视图中返回选定的值,如果我使用默认列表视图,我可以做到,但现在我自定义列表视图,获得了图像视图、评分栏和一些文本视图。用户选择后如何在 toast 中返回 title 的值?下面是我的代码:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appslist);

        list=(ListView)findViewById(R.id.lvApps);        


        SoapObject Request = new SoapObject (NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        AndroidHttpTransport aht = new AndroidHttpTransport(URL);

        try
        {
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapObject resultString = (SoapObject) soapEnvelope.getResponse();          

            String[] strTitle = new String[resultString.getPropertyCount()];
            String[] strDeveloper = new String[resultString.getPropertyCount()];
            String[] strRating = new String[resultString.getPropertyCount()];
            String[] strLogo = new String[resultString.getPropertyCount()];         

            for(int i =0; i<resultString.getPropertyCount(); i++)
            {
                SoapObject array = (SoapObject) resultString .getProperty(i);
                strTitle[i] = array.getProperty(1).toString();  //get title  
                strDeveloper[i] = array.getProperty(3).toString(); //get developer
                strRating[i] = array.getProperty(4).toString(); //get rating
                strLogo[i] = array.getProperty(5).toString();   //get photo             
            }
            adapter=new AppsListAdapter(this, strTitle, strDeveloper, strRating, strLogo);
            list.setAdapter(adapter);

            list.setOnItemClickListener(new OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    Object GetLabel = list.getItemAtPosition(arg2);
                          //Toast here
                    Toast.makeText(getApplicationContext(), GetLabel.toString(), Toast.LENGTH_SHORT).show();                        

            });
        }

        catch(Exception e)
        {

        }
    }
}
4

2 回答 2

1

您应该在一个项目中创建所有数据的模型(bean)意味着将数据包装在对象中并创建该模型的数组并将该数组发送到适配器............

但是现在有几种方法可以让你完成任务:

1-您拥有所选项目的位置,该位置可用作 strTitle 中的索引以获取特定标题。

字符串标题 = strTitle[位置]; /// if think can put null and length check if 让它安全

2-您可以使用 findViewbyid 从该视图中查看回调 public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) 中的列表视图,您可以获得具有标题的文本视图

String s =(String) ((TextView) view.findViewById(R.id.title)).getText(); //view 被选中的行视图

或者

TextView txt =(TextView)arg0.getChildAt(position-lv.firstVisiblePosition()).findViewById(R.id.mylistviewtextview); //arg0 是父视图 String keyword = txt.getText().toString();

3-

于 2012-05-28T12:27:58.687 回答
1

试试这个

lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                HashMap<String, String> o = (HashMap<String, String>) mylist
                        .get(position);
                Log.v("o", o + "n");
                String d = o.get("the string value you want to retrieve");
                Log.v("Tapped ", d);

            }
        });

其中“lv”是您的列表视图,mylist 是您的数组列表

于 2012-05-28T12:32:32.670 回答