Monster
Monster

Reputation: 21

Custom listview error

I'm newbie, I created a custom list view using adapter but when I run my app. it gives error any helP? error in // legendImage.setImageDrawable(image);

Here is picture of logcat file error Here is class AdapterCustomlistView:

    public class AdapterCustomListview extends ArrayAdapter<MoveData> {
        private int resource;
        private LayoutInflater inflater;
    private Context context;
    public AdapterCustomListview(Context ctx, int resourceId, List<MoveData> objects) {
        super(ctx, resourceId, objects);
        resource = resourceId;
        inflater = LayoutInflater.from(ctx);
        context = ctx;
    }

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

        convertView = (LinearLayout) inflater.inflate(resource, null);
        MoveData Legend = getItem(position);
        TextView legendName = (TextView) convertView.findViewById(R.id.txtAppName);
        legendName.setText(Legend.getName());

        TextView legendBorn = (TextView) convertView.findViewById(R.id.txtCoin);
        legendBorn.setText(Legend.getCoin());

        ImageView legendImage = (ImageView) convertView.findViewById(R.id.imageView);
        String uri = "drawable/" + Legend.getImage();
        int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
        //noinspection deprecation
        Drawable image = context.getResources().getDrawable(imageResource);


        legendImage.setImageDrawable(image); **//This row show error**



        return convertView;

    }
}

this is HOme activity (MainActivity)

    ctx = this;
    List<MoveData> legendList = new ArrayList<MoveData>();
    legendList.add(new MoveData("icon_dollar", "icon_dollar", "icon_dollar"));
    legendList.add(new MoveData("icon_dollar", "icon_dollar", "icon_dollar"));
    legendList.add(new MoveData("icon_dollar", "icon_dollar", "icon_dollar"));
    legendList.add(new MoveData("icon_dollar", "icon_dollar", "icon_dollar"));
    legendList.add(new MoveData("icon_dollar", "icon_dollar", "icon_dollar"));
    legendList.add(new MoveData("icon_dollar", "icon_dollar", "icon_dollar"));

    listViewItem = (ListView) findViewById(R.id.lvItem);
    listViewItem.setAdapter(new AdapterCustomListview(ctx, R.layout.custom_layout_single, legendList));

    // Click event for single list row
    listViewItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MoveData o = (MoveData) parent.getItemAtPosition(position);
            Toast.makeText(Home.this, o.getName().toString(), Toast.LENGTH_SHORT).show();
        }
    });
}

Here is class move data

public class MoveData {


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCoin() {
    return coin;
}

public void setCoin(String coin) {
    this.coin = coin;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}
private String name;
private String coin;
private String image;

public MoveData(String AppName, String Coin, String image) {
    super();
    this.name = AppName;
    this.coin = Coin;
    this.image = image;
}
}

Upvotes: 0

Views: 110

Answers (1)

Jeffalee
Jeffalee

Reputation: 1085

instead of using Drawable image = context.getResources().getDrawable(imageResource); (which is deprecated), use: Drawable image = ContextCompat.getDrawable(context, imageResource);

Upvotes: 1

Related Questions