Inflate listview by Customized ArrayAdapter

2019-09-20 23:46发布

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条回答
老娘就宠你
2楼-- · 2019-09-21 00:33

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;
}
}
查看更多
登录 后发表回答