从数组中获取字符串

时间:2012-05-28 12:21:44

标签: android arrays listview

我想从列表视图中返回所选值,如果我使用默认列表视图,我可以这样做,但现在我自定义列表视图,获得了imageview,ratingbarm和少量textview。用户选择后,如何在吐司上返回标题值?下面是我的代码:

    @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)
        {

        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

1-你有所选项目的位置,可以在strTitle中用作索引来获得特定的标题。

String title = strTitle [position]; ///如果认为可以将空值和长度检查是否使其安全

2-你可以使用findViewbyid从该视图中获得callback public void onItemClick(AdapterView arg0,View arg1,int arg2,long arg3)中列表的视图,你可以获得具有标题的textview

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

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

3 -

答案 1 :(得分: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是你的arraylist