-2

I am beginner of Android programming and need little help. I have an array of objects that should be shown in listview. Here is a code of class:

public class Car {
    public String img;
    public String thumbnail;
    public String manufacturer;
    public String model;
    public int price;
    public String description;
}

Thumbnail is a string variable that contains URL of thumbnail to be shown. I also have array of a few Car objects called "cars" declared in my DataStorage class.

I want only 2 things of every object to be shown in my customized listview: thumbnail (from internet) and model.

I created layout item.xml, which should represent one item of listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@android:color/holo_blue_dark">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="left"
        android:id="@+id/image_tmb"
        android:layout_gravity="left|center_vertical"/>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:paddingBottom="10dp"
            android:id="@+id/name"
            android:layout_weight="1"
            android:textSize="23dp"/>

    </LinearLayout>
</LinearLayout>

TextView should be filled with model and Imageview should be filled with thumbnail.

I also created class MyAdapter which should fill listview:

public class MyAdapter extends BaseAdapter {
    private Context myContext;
    public MyAdapter(Context context) {
        this.myContext = context;
    }

    @Override
    public int getCount() {
        return DataStorage.cars.length;
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
      //Here I should customize my view but don't know how.

    }

I created listview element in activity_main.xml and delcared it in MainActivity:

 ListView lv=(ListView) findViewById(R.id.lv);

So, how would I do my task?

3
  • Hello, Unfortunately your question is slightly 'bad' in that you are asking us to write your code for you or point you to a place to learn the material... NOT to 'fix a bug' (Which is what posts on here are suppose to be about, and your post should get flagged) vogella.com/tutorials/AndroidListView/article.html Here is one of Many links i found from googling 'android custom listview' and arbitrarily picked a link that looked like a tutorial. So in the future, if you just need tutorial/etc. then just google yourself.
    – mawalker
    Jan 4, 2016 at 16:15
  • you must define tumbnails as parameter in your adapter. and in your main activity pass it to adapter. and do not forget to initialize items inside getview method.
    – karimkhan
    Jan 4, 2016 at 16:16
  • BUT in the future, if you follow a tutorial or something, and don't understand something (either you get an error, or it works but you don't understand 'why') then feel free to ask another question about what the problem is.
    – mawalker
    Jan 4, 2016 at 16:16

1 Answer 1

2

A simple pattern would look something like this

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    // Inflate the view if it doesn't already exist
    if (view == null) {
        LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
    }

    // Get your current Car object
    Car c = getItem(i);

    // Initialize the views
    ImageView image = (ImageView) view.findViewById(R.id.image_tmb);
    TextView name = (TextView) view.findViewById(R.id.name);

    // Fill the views according to the car's properties

    /* NOTE: I'm not going to go in depth to explain how to set 
    an ImageView image from a URL, as it is an entirely different question,
    so I'll put a link to this library below */

    Picasso.with(myContext).load(c.thumbnail).into(image);
    name.setText(c.model);
}

You'll need to modify getItem to actually get the item, instead of returning null..

@Override
public Object getItem(int position) {
    return DataStorage.cars[position];
}

Then, set up your adapter in your Activity/Fragment/Whatever, like this

MyAdapter mAdapter = new MyAdapter(this);
lv.setAdapter(mAdapter);

If you're going to be doing a lot with images and don't want to write your own implementation, Picasso is a really easy library to use.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.