1. Are you ready for the Galaxy S20? Here is everything we know so far!

Help required to create a ListView

Discussion in 'Android Development' started by abcd2000, Sep 11, 2011.

  1. abcd2000

    abcd2000 Lurker
    Thread Starter

    Hello,
    I am struggling for creating a list view which should show one icon and a text in each row. The examples given in developer.android.com site for ListView is very simple and has only String to list in the ListView.

    I am not sure how to define the layout for my requirement and how to build the Adapter class to list the icon and text at the same time.

    Please help me to understand the ListView because the Book I have "Professional Android 2 Application Development" is really a useless book. Also the above site is also not very helpful.

    Thanks in advance.
    Cheers
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?

    abcd2000 likes this.
  3. alostpacket

    alostpacket Over Macho Grande?

    Also, welcome to AF! :)
     
    abcd2000 likes this.
  4. abcd2000

    abcd2000 Lurker
    Thread Starter

  5. abcd2000

    abcd2000 Lurker
    Thread Starter

    I am trying to display a text and an icon in a ListView. Below is my code. When I run, I am not getting anything displayed in simulator. Can somebody please loot at the code below and tell me what is wrong?

    I tried to make this code as suggested above.
    ------------------------------------------------------
    listview.xml:
    ------------------------------------------------------
    <?xml version="1.0" encoding="utf-8"?>
    <ListView android:id="@android:id/list"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
    android:id="@+id/textView"
    android:padding="3dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"/>
    </ListView>

    ------------------------------------------------------
    FileActivity.java:
    ------------------------------------------------------
    package com.vsb.file;

    import android.app.ListActivity;
    import android.os.Bundle;

    public class FileActivity extends ListActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.listview);
    FileAdapter adapter = new FileAdapter(this);
    adapter.addFile(new FileView(this, null, "One", R.drawable.folder));
    adapter.addFile(new FileView(this, null, "Two", R.drawable.file));
    setListAdapter(adapter);
    }
    }

    ------------------------------------------------------
    FileView.java:
    ------------------------------------------------------
    package com.vsb.file;

    import java.io.File;

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    public class FileView extends LinearLayout {

    private File file;
    private ImageView image;
    private TextView text;
    private int resId;

    public File getFile() {
    return file;
    }

    public void setFile(File file) {
    this.file = file;
    }

    public ImageView getImage() {
    return image;
    }

    public void setImage(int resId) {
    this.image.setImageResource(resId);
    this.resId = resId;
    }

    public TextView getText() {
    return text;
    }

    public void setText(CharSequence text) {
    this.text.setText(text);
    }

    public FileView(Context context) {
    super(context);
    }

    public FileView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public FileView(Context context, File file, CharSequence lable, int resId) {
    super(context);
    this.file = file;
    text = new TextView(context);
    text.setText(lable);
    image = new ImageView(context);
    this.resId = resId;
    image.setImageResource(resId);
    }

    public int getResId() {
    return resId;
    }

    public void setResId(int resId) {
    this.resId = resId;
    }

    }

    ------------------------------------------------------
    FileAdapter.java:
    ------------------------------------------------------
    package com.vsb.file;

    import java.util.ArrayList;
    import java.util.List;

    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;

    public class FileAdapter extends BaseAdapter {

    private List<FileView> list;
    private Context context;

    public FileAdapter(Context context) {
    this.context = context;
    list = new ArrayList<FileView>();
    }

    public void addFile(FileView fv) {
    list.add(fv);
    }
    @Override
    public int getCount() {
    return list.size();
    }

    @Override
    public Object getItem(int position) {
    return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    FileView fv;
    if(convertView == null) {
    fv = new FileView(context, null, list.get(position).getText().getText(), list.get(position).getResId());
    } else {
    fv = (FileView)convertView;
    fv.setText(list.get(position).getText().getText());
    fv.setImage(list.get(position).getResId());
    }
    return fv;
    }

    }
     
Loading...
Similar Threads - Help required create
  1. Oshhshwwn
    Replies:
    0
    Views:
    27
  2. Jhon534
    Replies:
    2
    Views:
    101
  3. SplashyBTW
    Replies:
    4
    Views:
    263
  4. nichosi
    Replies:
    3
    Views:
    266
  5. Tejana Mama84
    Replies:
    2
    Views:
    362
  6. Unforgiven
    Replies:
    3
    Views:
    148
  7. brotherfromtaytay
    Replies:
    0
    Views:
    96
  8. jmd494
    Replies:
    3
    Views:
    786
  9. upnorthisbest
    Replies:
    4
    Views:
    453
  10. Dehaat Store
    Replies:
    0
    Views:
    315

Share This Page

Loading...