1

i wanted to returned the selected value from the listview, i can do it if i using the default listview, but now i customize the listview, got imageview, ratingbarm and few textview. how can i return the value of title at the toast after user has selected? below is my code:

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

        }
    }
}
1
  • are you using design pattern Holder in getView method of your Adapter? Commented May 28, 2012 at 12:27

2 Answers 2

1

You should create model (bean) of all the data in one item means wrap the data in objects and create the array of that models and send that array to adapter..........

But for now there a few ways to acive you task :

1- you have the position of selected item which can be used as index in the strTitle to get the particular title.

String title = strTitle[position]; /// if think can Put the null and length check if to make it safe

2- you have the view of the list in callback public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) from that view using findViewbyid you can get your textview having title

String s =(String) ((TextView) view.findViewById(R.id.title)).getText(); //view is selected row view

or

TextView txt =(TextView)arg0.getChildAt(position-lv.firstVisiblePosition()).findViewById(R.id.mylistviewtextview); //arg0 is parent view String keyword = txt.getText().toString();

3-

Sign up to request clarification or add additional context in comments.

3 Comments

currently if i press the listview, it return an array, start from 0 for item number 1, 1 for item number 2.... etc i wanted to change that to my title's value
there are more alternative ways also mentioned above.. take a look over them also....
sure, will spend my time to know more about them :)
1

Try this

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

            }
        });

where "lv" is your listview and mylist is your arraylist

1 Comment

i having problem with .get(position);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.