0

So what i m trying to do is get a String value on click of an item. But through this code when i click on the item i get the position of the converter class or something like that,which i dont understand. "MainActivity$Converter@30e6eb1" in the toast. I just want the subject part to Appear in the Toast.

   @Override
    protected void onPostExecute(String result) {

        JSONObject jsonObject;
        JSONArray jsonArray;
        CustomAdapter customAdapter;
        ListView listview;

        listview = (ListView) findViewById(R.id.xlistview);
        customAdapter = new CustomAdapter(getApplicationContext(), R.layout.row_layout);
        listview.setAdapter(customAdapter);
                                                                                 //Log.d("json_raw_home",result);

        if (result != null) {                                                    //Log.d("post execute",result);
            try {

                jsonObject = new JSONObject(result);
                jsonArray = jsonObject.getJSONArray("data");
                int count = 0;
                String subject, description;
                while (count < jsonArray.length()) {
                    JSONObject JO = jsonArray.getJSONObject(count);
                    subject = JO.getString("subject");
                    description = JO.getString("description");

                    Converter converter = new Converter(subject, description);
                    customAdapter.add(converter);

                    count++;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = String.valueOf(parent.getItemAtPosition(position));
                Toast.makeText(getApplicationContext(),item, Toast.LENGTH_LONG).show();}
        });
    }
}

public class CustomAdapter extends ArrayAdapter {
    List list = new ArrayList();

    public CustomAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        row = convertView;
        ContactHolder contactHolder;

        if(row==null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            contactHolder=new ContactHolder();
            contactHolder.tv_subject=(TextView)row.findViewById(R.id.xsubject_tv);
            contactHolder.tv_description=(TextView)row.findViewById(R.id.xdescription_tv);
            row.setTag(contactHolder);
        }

        else
        {
            contactHolder=(ContactHolder)row.getTag();
        }

        Converter converter = (Converter) this.getItem(position);
        contactHolder.tv_subject.setText(converter.getSubject());
        contactHolder.tv_description.setText(converter.getDescription());
        return row;
    }

    public class ContactHolder{
        TextView tv_subject,tv_description;
    }


}

public class Converter {
    private String subject,description;

    public Converter(String subject, String description) {
        this.setSubject(subject);
        this.setDescription(description);
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

}

3 Answers 3

2

Change

String item = String.valueOf(parent.getItemAtPosition(position));

to

String item = ((Converter)parent.getAdapter().getItem(position)).getSubject() 
Sign up to request clarification or add additional context in comments.

4 Comments

You've been sent from heavens.. :D I need a Tutor can u help me out?
su kaho mota bhai?
SO is there to help you anytime. you can ask the question and you will surely get the answer.
i ve sent you a message on fb.
1

You pass Converter objects to CustomAdapter to list them. So if you call getItem you have to concat Object to Converter. So;

change

String item = String.valueOf(parent.getItemAtPosition(position));

to

String item = ((Converter)parent.getAdapter().getItem(position)).getSubject();

so final code:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = ((Converter)parent.getAdapter().getItem(position)).getSubject();
                Toast.makeText(getApplicationContext(),item, Toast.LENGTH_LONG).show();}
        });

Comments

0

parent.getItemAtPosition(position); return the object of at position whatever you return from getItem(position).

Converter item = (Converter )parent.getItemAtPosition(position);

Toast.makeText(getApplicationContext(),item.getSubject(), Toast.LENGTH_LONG).show();

Comments

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.