Karan Jtv Karan Jtv - 1 month ago 8
Android Question

Getting String out of custom List View

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


}

Answer

Change

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

to

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