Inflate listview by Customized ArrayAdapter

夙愿已清 提交于 2020-01-26 04:17:05

问题


I have one Array + one Listview + one Listview items layout Xml + one Class extended from ArrayAdapter,

Now i want to show my Array Items in Customized ListView By using Customized ArrayAdapter without any using of another class or method !

I now how i can use CursorAdapter But i can't work with ArrayAdapter


回答1:


You can try to use BaseAdapter.

Adapter class:

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;
}
}


来源:https://stackoverflow.com/questions/37783622/inflate-listview-by-customized-arrayadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!