显示错误的ArrayAdapter无法解析构造函数适配器错误

时间:2013-12-16 07:55:58

标签: android list android-layout listview

我的ArrayAdapter显示无法解析适配器。请让我知道语法有什么问题。

 @Override
    public void onRequestSuccess(List<Order> Items) {
        Toast.makeText(HomeActivity.this, "Success", Toast.LENGTH_SHORT).show();
         List<Order> list = new ArrayList<Order>();

        ListView listView = (ListView) findViewById(R.id.listview);

        ListAdapter<Order> adapter = new ListAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);

    }                 

MY Order课程如下所示。

public class Order {
public String createdOn;
public String id;
public String lastModifiedOn;
public String trackingNumber;
public String tripId;
public OrderEntry orderEntry;
public String tripOrderStatusValue;
public String notificationStatus;
public String remark;
public String createdBy;
public String orderId;
public String deliveryReasonCode;
public Boolean isTripStarted;
public Boolean isTripCompleted;
public Boolean isOutScanned;
public String shipmentType;
public String deliveryTime;

}

我的ListAdapter看起来像这样。

公共类ListAdapter扩展了ArrayAdapter {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub
}

private List<Order> items;

public ListAdapter(Context context, int resource, List<Order> items) {

    super(context, resource, items);

    this.items = items;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.list_row_myschedule, null);

    }

    Order p = items.get(position);

    if (p != null) {

        TextView tt = (TextView) v.findViewById(R.id.textview1);
        TextView tt1 = (TextView) v.findViewById(R.id.textview2);
        TextView tt3 = (TextView) v.findViewById(R.id.textview3);

        if (tt != null) {
            tt.setText(p.orderStatusValue);
        }
        if (tt1 != null) {

            tt1.setText(p.reasonCode);
        }
        if (tt3 != null) {

            tt3.setText(p.trackingNumber);
        }
    }

    return v;
}

}

5 个答案:

答案 0 :(得分:9)

泛型必须在声明/初始化

中保持一致

变化

ArrayAdapter<Order> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this, android.R.layout.simple_list_item_1, list);

此外,您还要确保this引用活动上下文

答案 1 :(得分:4)

试试这个:

ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this.getActivity(), 

android.R.layout.simple_list_item_1, list);    

答案 2 :(得分:1)

尝试使用

 ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(HomeActivity.this, android.R.layout.simple_list_item_1, list);

取代

 ArrayAdapter<Order> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

答案 3 :(得分:0)

使用以下

您拥有的是Custom ArrayAdapter

 ListAdapter<Order> adapter = new ListAdapter<String>(this, android.R.layout.simple_list_item_1, list);

您的ListAdapter<Order>是订单类型。但你有new ListAdapter<String>这是错误的。

你也有

public class ListAdapter extends ArrayAdapter {

我建议您将ListAdapter重命名为CustomAdapter并填充列表List<Order> list

示例:

    ListView lv = (ListView) findViewById(R.id.listView1);
    for(int i=0;i<10;i++)
    {
         list.add(new Order("status","1","2"));  
         // 10 items in lsitview with 3 items in each row.
         // Now all you need to do is populate your own data to list instead of the above 
    }
    CustomAdapter cus = new CustomAdapter(MainActivity.this,list);
    lv.setAdapter(cus);

订单类

public class Order {
public String tripOrderStatusValue;
public String deliveryReasonCode;
public String trackingNumber;
public Order(String tripOrderStatusValue, String deliveryReasonCode,String trackingNumber)
{
         this.tripOrderStatusValue =tripOrderStatusValue;
         this.deliveryReasonCode = deliveryReasonCode;
         this.trackingNumber = trackingNumber;
}
}

CustomAdapter,它扩展了Order

类型的ArrayAdapter
public class CustomAdapter extends ArrayAdapter<Order> {

    ArrayList<Order> list;
    LayoutInflater mInflater;
    public CustomAdapter(Context context, ArrayList<Order> list) {
        // TODO Auto-generated constructor stub
        super(context, 0,list);
        mInflater = LayoutInflater.from(context);
        this.list =list;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if(convertView ==null)
        {
            convertView = mInflater.inflate(R.layout.list_item,parent,false);
            holder = new ViewHolder();
            holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
            holder.tv3 = (TextView) convertView.findViewById(R.id.textView3);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag(); 
        }
        holder.tv1.setText(list.get(position).tripOrderStatusValue);
        holder.tv2.setText(list.get(position).deliveryReasonCode);
        holder.tv3.setText(list.get(position).trackingNumber);
        return convertView;
    }



    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return super.getItemId(position);
    }
static class ViewHolder
{
    TextView tv1,tv2,tv3;
}
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="24dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="26dp"
        android:text="TextView" />

</RelativeLayout>

对齐

enter image description here

答案 4 :(得分:0)

尝试此操作但在此之前您的订单类应包含一些值:

@Override
public void onRequestSuccess(List<Order> Items) {
    Toast.makeText(HomeActivity.this, "Success", Toast.LENGTH_SHORT).show();
       List<Order> list = new ArrayList<Order>();
       list =Items;
    ListView listView = (ListView) findViewById(R.id.listview);

    ListAdapter adapter = new ListAdapter(this, list );
    listView.setAdapter(adapter);

}       

你的ListAdapter应该是这样的。

public class ListAdapter extends BaseAdapter {


private List<Order> items;
Context mContext;

public ListAdapter(Context context, List<Order> items) {

super(context, items);

this.items = items;
this.mContext = context;

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

    @Override
    public Object getItem(int position) {
        return position;
    }

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


 @Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;

if (v == null) {

    LayoutInflater vi;
    vi = LayoutInflater.from(mContext );
    v = vi.inflate(R.layout.list_row_myschedule, null);

}

Order p = items.get(position);

    TextView tt = (TextView) v.findViewById(R.id.textview1);
    TextView tt1 = (TextView) v.findViewById(R.id.textview2);
    TextView tt3 = (TextView) v.findViewById(R.id.textview3);

        tt.setText(p.tripOrderStatusValue);
        tt1.setText(p.deliveryReasonCode);
        tt3.setText(p.trackingNumber);

return v;
}

}

检查这是否不起评论: