BaseAdapter如何在没有布局文件的情况下以编程方式编码?

时间:2015-04-10 13:47:18

标签: android android-layout android-arrayadapter baseadapter layout-inflater

有一个示例baseadapter,如下所示,它使用布局xml文件名adapter_layout。但我不想使用adapter_layout.xml。我想识别类中的所有组件(TextView,EditText ...)。如果没有adapter_layout.xml,BaseAdapter应该如何以编程方式编码?

public class LayoutAdapter extends BaseAdapter {

private List<String> objects = new ArrayList<String>();

private Context context;
private LayoutInflater layoutInflater;

public LayoutAdapter(Context context) {
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public String getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.adapter_layout, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
        viewHolder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
        viewHolder.editText1 = (EditText) convertView.findViewById(R.id.editText1);

        convertView.setTag(viewHolder);
    }
    return convertView;
}


protected class ViewHolder {
    private TextView textView2;
    private TextView textView1;
    private EditText editText1;
}

}

0 个答案:

没有答案