我有一个Array
+一个Listview
+一个Listview items layout Xml
+ Class
从ArrayAdapter
延伸,
现在,我想在自定义Array
中显示我的ListView
项目,使用自定义ArrayAdapter
而不使用其他类或方法!
我现在如何使用CursorAdapter但是我无法使用ArrayAdapter
答案 0 :(得分:1)
您可以尝试使用BaseAdapter。
适配器类:
public class YourAdapter extends BaseAdapter {
private Context mContext;
private String yourArray[];
public YourAdapter (Context context, String data[]) {
mContext = context;
yourArray= data;
}
@Override
public int getCount() {
return yourArray.length;
}
@Override
public Object getItem(int position) {
return yourArray[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
//new item
convertView = LayoutInflater.from(mContext).inflate(R.layout.days_list_item, null);
holder = new ViewHolder();
holder.numberOfDays = (TextView) convertView.findViewById(R.id.eventDays);
holder.sinceOrUntil = (TextView) convertView.findViewById(R.id.eventType);
holder.eventTitle = (TextView) convertView.findViewById(R.id.eventTitle);
holder.daysText = (TextView) convertView.findViewById(R.id.DaysText);
convertView.setTag(holder);
} else {
//reusing existing item
holder = (ViewHolder) convertView.getTag();
}
/*
* Do you preparatory steps here, then assign result to below
*/
holder.numberOfDays.setText("text1");
holder.sinceOrUntil.setText("text2");
holder.eventTitle.setText("text3");
return convertView;
}
private static class ViewHolder {
TextView numberOfDays;
TextView daysText;
TextView sinceOrUntil;
TextView eventTitle;
}
}