Web サービスから取得したレコードを表示するアプリがあります。このレコードには、startTime、duration、status、clientname の 5 つのフィールドがあります。(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;
}
}
【ヒテ更新】
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