3

My ArrayAdapter shows cannot resolve adapter. Please let me know what is wrong with the syntax.

 @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.

public class ListAdapter extends 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;
}

}

2
  • your ArrayAdapter constructor is wrong..change it. Commented Dec 16, 2013 at 7:58
  • Can u show you order class and I think this method will not work change it Commented Dec 16, 2013 at 8:10

5 Answers 5

9

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

Sign up to request clarification or add additional context in comments.

2 Comments

what does didnt work mean? You get the same compile time error?
I think it's because there's something wrong with my adapter.
4

Try this:

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

android.R.layout.simple_list_item_1, list);    

Comments

1

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);

Comments

0

Use the below

What you have is a Custom 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.

Also you have

public class ListAdapter extends ArrayAdapter {

I suggest you rename ListAdapter to CustomAdapter and populate the list 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

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>

Snap

enter image description here

7 Comments

@androidbug post Order class. Also how is list populated? and not working means you get the same error?
@androidbug also what is the param List<Order> Items. if you don't populate list with items it will be empty. Also what is the error that you are getting now after the change?. I suggest you post more relevant parts of the code.
@androidbug where do you add order items to list. show us that part
@androidbug you need to populate list then pass the list to the constructor adapter class
@androidbug i will post an example. change it according. also show us where you get all those list items values?
|
0

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.

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 :

1 Comment

modified but I haven't check so check and if doesn't work than comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.