本文介绍了通过自定义ArrayAdapter膨胀listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Array
+一个Listview
+一个Listview items layout Xml
+一个Class
从ArrayAdapter
扩展而来,
I have one Array
+ one Listview
+ one Listview items layout Xml
+ one Class
extended from ArrayAdapter
,
现在,我想通过使用自定义的ArrayAdapter
在自定义的ListView
中显示我的Array
项目,而无需使用任何其他类或方法!
Now i want to show my Array
Items in Customized ListView
By using Customized ArrayAdapter
without any using of another class or method !
我现在可以使用CursorAdapter,但是我不能与ArrayAdapter一起使用
I now how i can use CursorAdapter But i can't work with ArrayAdapter
推荐答案
您可以尝试使用BaseAdapter.
You can try to use 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;
}
}
这篇关于通过自定义ArrayAdapter膨胀listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!