I’ve an app where i am displaying a record I’ve got from a webservice. The record has 5 fields, startTime, duration, status, and clientname. (clientname is actually made up of 2 fields first and last). I can get the fields from an arrayList into a String array and then pass that array to the adapter. The fields are displayed in the ListView. All good so far, apart from the same record is shown 5 time in the ListView. Why is this? I pass the array in once to the adapter.
public class GetRota extends Activity {
private static final String TAG = GetRota.class.getSimpleName();
ListView listView;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getrotalayout);
listView = (ListView)findViewById(R.id.rotalist);
intent = this.getIntent();
Bundle bundle = intent.getBundleExtra("rotaArrayBundle");
@SuppressWarnings("unchecked")
ArrayList array = (ArrayList) bundle.get("rotaArray");
for(int i = 0; i < array.size(); i++){
Log.e(TAG, "array pos " + i + " = " + array.get(i));
}
String record = array.get(0).toString();
String[] itemsInRecord = record.split(",");
String[] recordItem = new String[5];
for(int x = 0; x < itemsInRecord.length; x++){
Log.e(TAG, "token = " + itemsInRecord[x]);
recordItem[x] = itemsInRecord[x];
}
MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(this, recordItem);
listView.setAdapter(arrayAdapter);
}// end of onCreate
private class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rotarowlayout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rotarowlayout, parent, false);
TextView startTime = (TextView)rowView.findViewById(R.id.rowstarttime);
TextView duration = (TextView)rowView.findViewById(R.id.rowduration);
TextView status = (TextView)rowView.findViewById(R.id.rowstatus);
TextView name = (TextView)rowView.findViewById(R.id.rowclientname);
startTime.setText("Start Time: " + values[0]);
duration.setText("Duration:" + values[1]);
status.setText("Status:" + values[2]);
name.setText("Client:" + values[3] + values[4]);
return rowView;
}
}
}// end of GetRota
.
[update]
private class MySimpleArrayAdapter extends BaseAdapter {
private final Context context;
private final ArrayList array;
public MySimpleArrayAdapter(Context context, ArrayList array) {
this.context = context;
this.array = array;
}
public int getCount() {
return array.size();
}
public Object getItem(int position) {
return array.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String[] values = array.get(position).toString();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
rowView = new View(context);
// get layout from gridview_item.xml
rowView = inflater.inflate(R.layout.rotarowlayout, parent, false);
} else {
rowView = (View) convertView;
}
TextView startTime = (TextView)rowView.findViewById(R.id.rowstarttime);
TextView duration = (TextView)rowView.findViewById(R.id.rowduration);
TextView status = (TextView)rowView.findViewById(R.id.rowstatus);
TextView name = (TextView)rowView.findViewById(R.id.rowclientname);
startTime.setText("Start Time: " + values[position][0]);
duration.setText("Duration:" + values[position][1]);
status.setText("Status:" + values[position][2]);
name.setText("Client:" + values[position][3] + values[position][4]);
return rowView;
}
}
[update hitesh]
private class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String[]> list;
public MySimpleArrayAdapter(Context context,ArrayList<String[]> list)
{
super(context, R.layout.rotarowlayout);
Log.e(TAG, "inside adapter constructor");
this.context = context;
this.list= list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e(TAG, "inside getView");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rotarowlayout, parent, false);
TextView startTime = (TextView)rowView.findViewById(R.id.rowstarttime);
TextView duration = (TextView)rowView.findViewById(R.id.rowduration);
TextView status = (TextView)rowView.findViewById(R.id.rowstatus);
TextView name = (TextView)rowView.findViewById(R.id.rowclientname);
String[] values = list.get(position);
startTime.setText("Start Time: " + values[0]);
Log.e(TAG, "starttime = " + startTime);
duration.setText("Duration:" + values[1]);
status.setText("Status:" + values[2]);
name.setText("Client:" + values[3] + values[4]);
return rowView;
}
@Override
public int getCount() {
return this.list.size();
}
}// end of adapter class
actualy ur code is smwt wrong. use
ArrayList list of string arrays
Customize ur adapter also. MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(this, list);
listView.setAdapter(arrayAdapter);
In ur activity class, replace
with