Android ArrayAdapter 显示错误无法解决构造函数适配器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20605891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayAdapter showing error cannot resolve constructor adapter error
提问by buggydroid
My ArrayAdapter shows cannot resolve adapter. Please let me know what is wrong with the syntax.
我的 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 class looks like this.
我的订单类看起来像这样。
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;
}
}
And my ListAdapter looks like this.
我的 ListAdapter 看起来像这样。
public class ListAdapter extends ArrayAdapter {
公共类 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;
}
}
}
回答by Blackbelt
Generics have to be consistent in the declaration/initialization
泛型必须在声明/初始化中保持一致
change
改变
ArrayAdapter<Order> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
with
和
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this, android.R.layout.simple_list_item_1, list);
And also you make sure that this
refers to the activity context
而且你还要确保this
指的是活动上下文
回答by Fareed
Try this:
尝试这个:
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this.getActivity(),
android.R.layout.simple_list_item_1, list);
回答by Apoorv
Try using
尝试使用
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(HomeActivity.this, android.R.layout.simple_list_item_1, list);
in place of
代替
ArrayAdapter<Order> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
回答by Cropper
Try this but before that your order class should cantain some values:
试试这个,但在此之前,您的订单类应该包含一些值:
@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);
}
And your ListAdapter should be looks like this.
你的 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;
}
}
check this if doesn't work than comment :
检查这个如果不起作用而不是评论:
回答by Raghunandan
Use the below
使用以下
What you have is a Custom ArrayAdapter
您拥有的是自定义 ArrayAdapter
ListAdapter<Order> adapter = new ListAdapter<String>(this, android.R.layout.simple_list_item_1, list);
Your ListAdapter<Order>
is of type Order. But you have new ListAdapter<String>
which is wrong.
您 ListAdapter<Order>
的类型为 Order。但是你有new ListAdapter<String>
哪个是错误的。
Also you have
你还有
public class ListAdapter extends ArrayAdapter {
I suggest you rename ListAdapter to CustomAdapter and populate the list List<Order> list
我建议您将 ListAdapter 重命名为 CustomAdapter 并填充列表 List<Order> list
Example:
例子:
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);
Order class
订单类
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 which extends ArrayAdapter of type Order
扩展 Order 类型的 ArrayAdapter 的 CustomAdapter
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
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>
Snap
折断