简体   繁体   中英

creating custom Adapter class to avoid for custom listview

I am building my own ListView based off of this tutorial.

this line: MessageDetails msg = _data.get(position); in his code is giving me a problem. I am obviously getting an type error with this line. I do not understand how his code was working like this. What should I be doing differently so that I can have it work the way his does.

EDIT

I am aware of the fact that the problem with this line is that it is trying to put two different types together MessageDetails and ArrayList<String> . His tutorial doesn't give me a good way of doing as I am getting the error and he apparently did not.

my adapter class

public class MenuItemAdapter extends BaseAdapter {
    private ArrayList<String> _data;
    Context _c;

    MenuItemAdapter(ArrayList<String> data, Context c){
        _data = data;
        _c = c;
    }
    public int getCount() {
        return _data.size();
    }
    public Object getItem(int position) {
        return _data.get(position);
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
           LayoutInflater vi = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           v = vi.inflate(R.layout.menuitemrow, null);
        }
        ImageView image = (ImageView) v.findViewById(R.id.MIR_itemImage);
        TextView itemName = (TextView)v.findViewById(R.id.MIR_itemImage);
        TextView itemDescription = (TextView)v.findViewById(R.id.MIR_itemDescription);
        TextView itemPrice = (TextView)v.findViewById(R.id.MIR_itemPrice);
        TextView itemOther = (TextView)v.findViewById(R.id.MIR_itemOther);

        // Create instance of MenuItemDetail and then assign
        MenuItemDetail mid = _data.get(position);
        image.setImageResource(mid.icon);
        itemName.setText(mid.itemName);
        itemDescription.setText(mid.itemDescription);
        itemPrice.setText(mid.itemPrice);
        itemOther.setText(mid.itemOther);
        return v;
    }

}

and this is my MenuItemDetail class which is very similar to his just with my fields instead.

public class MenuItemDetail {

    int icon;
    String itemName;
    String itemDescription;
    String itemPrice;
    String itemOther;



    public int getIcon() {
        return icon;
    }
    public void setIcon(int icon) {
        this.icon = icon;
    }
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public String getItemDescription() {
        return itemDescription;
    }
    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }
    public String getItemPrice() {
        return itemPrice;
    }
    public void setItemPrice(String itemPrice) {
        this.itemPrice = itemPrice;
    }
    public String getItemOther() {
        return itemOther;
    }
    public void setItemOther(String itemOther) {
        this.itemOther = itemOther;
    }


}

Any help and explanation would be appreciated.

I also had to fix the ArrayList issue as well where it needed the ArrayList<String> . Not sure if that change is where it went wrong.

You declared _data as a List of Strings ( ArrayList<String> ), perhaps you meant to use a List of MenuItemDetails? Since you cannot convert a String into a MenuItemDetail object. These classes are unrelated.

private ArrayList<MenuItemDetail> _data;
Context _c;

MenuItemAdapter(ArrayList<MenuItemDetail> data, Context c){
    _data = data;
    _c = c;
}

There was problem with the code for the example. This cannot be done. The work around is to ensure that you are passing it an ArrayList<YourType> of the type you are using. This however, causes a problem with trying to pass it through a bundle as only ArrayLists<String> can be passed as an extra in a bundle. So it is best and my solution was to make a converter method to convert the object to an ArrayyList<String> and then another constructor method to create that type from ArrayList<String> . This then requires an exception class to be created to catch an error in conversion from ArrayList<String> to ArrayList<YourType> unless of coarse you have made the system "error proof" which I highly doubt.

I was trying to avoid this route but it is the only feasable solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM