我有一个应用程序,我在其中显示我从网络服务获得的记录。该记录有 5 个字段,startTime、duration、status 和 clientname。(clientname 实际上由 first 和 last 2 个字段组成)。我可以将 arrayList 中的字段放入 String 数组,然后将该数组传递给适配器。字段显示在 ListView 中。到目前为止一切都很好,除了相同的记录在 ListView 中显示了 5 次。为什么是这样?我将数组传递给适配器一次。
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
. [更新]
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;
}
}
[更新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