使用一个自定义适配器的多个列表视图

时间:2017-05-13 06:01:59

标签: android listview

实际上我正在创建多个listviews使用单个自定义适配器使用for循环将数组列表传递到适配器之后的问题是将最后发送的数组列表加载到所有列表视图后面这里是我的代码,请帮助我作为我我是新手。

for (int iqs=0; iqs< invoiceList.size();iqs++){
    invoice1 = invoiceList.get(iqs);
    itemsList1.clear();
    loadListViews(invoice1, listView1);//here am getting itemlist1 arraylist
    adapter=  new CustomAdapter(PendingOrdersActitvity.this, itemsList1);

    listView1 = new ListView(this);
    listView1.setLayoutParams(new RelativeLayout.LayoutParams(headerWidth, listHeight));
    //  listView1.setLayoutParams(list1Params);
    listView1.setDivider(null);
    listView1.setBackgroundColor(Color.parseColor("#defbff"));
    listView1.setAdapter(adapter);
    linear1.addView(listView1);
}

这是我的适配器类

public class CustomAdapter extends BaseAdapter {

private final LayoutInflater inflator;
private ArrayList<ItemsBean> newList = null;
private Context ctx;

DbHelperCozyPos dbHelper;
ArrayList<ItemsBean> disablelist = new ArrayList<ItemsBean>();
String parentobjid=null;

public CustomAdapter(Context ctx,int i,ArrayList<ItemsBean> invoiceDataList) {
    // TODO Auto-generated constructor stub

    this.ctx = ctx;
    this.newList = invoiceDataList;
    this.inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return newList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return newList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public  class ViewHolder {
    TextView qty, name;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    final ViewHolder holder;
    convertView = null;

    if (convertView == null) {
        convertView = inflator.inflate(R.layout.invoicelistadapter, null);
        holder = new ViewHolder();
        holder.qty = (TextView) convertView.findViewById(R.id.qty);
        holder.name = (TextView) convertView.findViewById(R.id.item);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.name.setText(newList.get(position).getItemnNameDisplay());
    holder.qty.setText(String.valueOf(newList.get(position).getQuantityDisplay()));
    holder.name.setTypeface(Typeface.DEFAULT_BOLD);
    holder.name.setTextSize(Constants.listbody);
    holder.qty.setTypeface(Typeface.DEFAULT_BOLD);
    holder.name.setTextColor(Color.parseColor("#0e1644"));
    holder.qty.setTextColor(Color.parseColor("#0e1644"));
}

新列表点击列表代码;

convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(ctx, ""+ "hii", Toast.LENGTH_SHORT).show();
        Dialog dialog = new Dialog(ctx);
                            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                            dialog.setContentView(R.layout.itembumping);
                            dialog.setCancelable(false);
                            dialog.show();
            ListView    list1=(ListView)dialog.findViewById(R.id.list1);
            ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(ctx,newList);
                    list1.setAdapter(adapter2);

        }
    }); 

2 个答案:

答案 0 :(得分:0)

您必须向适配器发送类型,以证明哪个布局必须膨胀并使进场布局膨胀。

答案 1 :(得分:0)

用这个代替你的代码。

public class CustomAdapter extends BaseAdapter {

private final LayoutInflater inflator;
private ArrayList<ItemsBean> newList = null;
private Context ctx;

DbHelperCozyPos dbHelper;
ArrayList<ItemsBean> disablelist = new ArrayList<ItemsBean>();
String parentobjid=null;

public CustomAdapter(Context ctx,int i,ArrayList<ItemsBean> invoiceDataList) {
    // TODO Auto-generated constructor stub

    this.ctx = ctx;
    this.newList = new ArrayList<>(invoiceDataList);

    this.inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return newList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return newList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public  class ViewHolder {
    TextView qty, name;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    final ViewHolder holder;
    convertView = null;

    if (convertView == null) {
        convertView = inflator.inflate(R.layout.invoicelistadapter, null);
        holder = new ViewHolder();
        holder.qty = (TextView) convertView.findViewById(R.id.qty);
        holder.name = (TextView) convertView.findViewById(R.id.item);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.name.setText(newList.get(position).getItemnNameDisplay());
    holder.qty.setText(String.valueOf(newList.get(position).getQuantityDisplay()));
    holder.name.setTypeface(Typeface.DEFAULT_BOLD);
    holder.name.setTextSize(Constants.listbody);
    holder.qty.setTypeface(Typeface.DEFAULT_BOLD);
    holder.name.setTextColor(Color.parseColor("#0e1644"));
    holder.qty.setTextColor(Color.parseColor("#0e1644"));
}