Issue
I have a listview with a custom adapter. I am trying to set value to the listview using an adapter. But the values are not being set at run time, but it was working when implemented with a simpleadapter. My custom Adapter code is as follows.
public class dataListAdapter extends SimpleAdapter {
// HashMap<String, String> map = new HashMap<String, String>();
List<HashMap<String, String>> data = new ArrayList();
public dataListAdapter(Context context,
List<HashMap<String, String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
this.data = data;
}
@Override
public Object getItem(int arg0) {
if (null != data) {
try {
return data.get(arg0);
} catch (IndexOutOfBoundsException e) {
return null;
}
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater
.inflate(R.layout.listview_layout, parent, false);
}
Button vd = (Button) row.findViewById(R.id.viewDetails);
// Button gd = (Button) row.findViewById(R.id.getDirections);
Button ra = (Button) row.findViewById(R.id.reqAppointment);
final OnClickListener vdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
ListView listView = (ListView) parent;
dataListAdapter adapter = (dataListAdapter) listView
.getAdapter();
HashMap<String, String> item = (HashMap<String, String>) adapter
.getItem(position);
// listView.getAdapter().get
Toast.makeText(getApplicationContext(),
"vd clicked Id " + item,
Toast.LENGTH_LONG).show();
}
};
final OnClickListener gdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"gdclicked Id ", Toast.LENGTH_LONG)
.show();
}
};
final OnClickListener ravClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"req appclicked Id ", Toast.LENGTH_LONG)
.show();
}
};
vd.setOnClickListener(vdClickListener);
// gd.setOnClickListener(gdClickListener);
ra.setOnClickListener(ravClickListener);
return row;
}
}
This is how I set the values to the custom adapter.But the values are not set in the list view.
String[] from = { "flag", "txt", "cur", "vd", "gd",
"ra" };
// Ids of views in listview_layout
int[] to = { R.id.flag, R.id.name, R.id.spec,
R.id.vd, R.id.gd, R.id.ra};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
adapter = new dataListAdapter(getBaseContext(), aList,
R.layout.listview_layout, from, to);
// new CallToServer(adapter, aList).execute("");
// Getting a reference to listview of main.xml layout file
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("txt", "Name : " + "name");
hm.put("cur", "spec: " + "spec");
hm.put("flag", Integer.toString(0x7f020001));
hm.put("but1", "");
hm.put("but2", "");
hm.put("but3", "");
hm.put("ID", "100");
aList.add(hm);
System.out.println("Adapter object " + adapter);
adapter.notifyDataSetChanged();
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
// setContentView(listView);
Can somebody guide me where I am going wrong, because the values set to the custom adapter are not shown in the listview.
I have tried adding onClickListener to the buttons and printed the item, and the values for the item are shown, like the name ,Id etc which are added are in the HashMap are shown, so its an issue in the rendering only ? Also the values are shown when I hard coded the values in the xml. Can somebody explain what can be the issue.
Solution
I think the change needed was in the getView() method, I have added the following code in that, and it worked. This was the same code which I have used in the onClickListener()
TextView textView=(TextView)row.findViewById(R.id.specialization);
HashMap<String, String> item = (HashMap<String, String>) adapter.getItem(position);
textView.setText(item.get("txt"));
I will be doing the same for other fields like images etc . I think I can even use the viewholder also.
Answered By - dileepVikram
Answer Checked By - Candace Johnson (JavaFixing Volunteer)