1. Download our Official Android App: Forums for Android!

Apps ListView not displaying anything.

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

  1. abcd2000

    abcd2000 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    5
    0
    5
    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 look at the code below and tell me what is wrong?

    I tried to make this code as suggested in thread http://androidforums.com/application-development/412041-help-required-create-listview.html

    Code (Text):
    1.  
    2.  ------------------------------------------------------
    3.  listview.xml:
    4.  ------------------------------------------------------
    5.  <?xml version="1.0" encoding="utf-8"?>
    6.  <ListView android:id="@android:id/list"
    7.        xmlns:android="http://schemas.android.com/apk/res/android"
    8.        android:layout_width="wrap_content"
    9.        android:layout_height="wrap_content">
    10.        <TextView
    11.              android:id="@+id/textView"
    12.              android:padding="3dip"
    13.              android:layout_width="wrap_content"
    14.              android:layout_height="wrap_content" />
    15.          <ImageView
    16.              android:id="@+id/imageView"
    17.              android:layout_width="wrap_content"
    18.              android:layout_height="wrap_content"
    19.              android:gravity="right"/>
    20.  </ListView>
    21.  
    22.  ------------------------------------------------------
    23.  FileActivity.java:
    24.  ------------------------------------------------------
    25.  package com.vsb.file;
    26.  
    27.  import android.app.ListActivity;
    28.  import android.os.Bundle;
    29.  
    30.  public class FileActivity extends ListActivity {
    31.  
    32.      /** Called when the activity is first created. */
    33.      @Override
    34.      public void onCreate(Bundle savedInstanceState) {
    35.          super.onCreate(savedInstanceState);
    36.          //setContentView(R.layout.listview);
    37.          FileAdapter adapter = new FileAdapter(this);
    38.          adapter.addFile(new FileView(this, null, "One", R.drawable.folder));
    39.          adapter.addFile(new FileView(this, null, "Two", R.drawable.file));
    40.          setListAdapter(adapter);
    41.      }
    42.  }
    43.  
    44.  ------------------------------------------------------
    45.   FileView.java:
    46.   ------------------------------------------------------
    47.  package com.vsb.file;
    48.  
    49.  import java.io.File;
    50.  
    51.  import android.content.Context;
    52.  import android.util.AttributeSet;
    53.  import android.widget.ImageView;
    54.  import android.widget.LinearLayout;
    55.  import android.widget.TextView;
    56.  
    57.  public class FileView extends LinearLayout {
    58.  
    59.      private File file;
    60.      private ImageView image;
    61.      private TextView text;
    62.      private int resId;
    63.      
    64.      public File getFile() {
    65.          return file;
    66.      }
    67.  
    68.      public void setFile(File file) {
    69.          this.file = file;
    70.      }
    71.  
    72.      public ImageView getImage() {
    73.          return image;
    74.      }
    75.  
    76.      public void setImage(int resId) {
    77.          this.image.setImageResource(resId);
    78.          this.resId = resId;
    79.      }
    80.  
    81.      public TextView getText() {
    82.          return text;
    83.      }
    84.  
    85.      public void setText(CharSequence text) {
    86.          this.text.setText(text);
    87.      }
    88.  
    89.      public FileView(Context context) {
    90.          super(context);
    91.      }
    92.      
    93.      public FileView(Context context, AttributeSet attrs) {
    94.          super(context, attrs);
    95.      }
    96.      
    97.      public FileView(Context context, File file, CharSequence lable, int resId) {
    98.          super(context);
    99.          this.file = file;
    100.          text = new TextView(context);
    101.          text.setText(lable);
    102.          image = new ImageView(context);
    103.          this.resId = resId;
    104.          image.setImageResource(resId);
    105.      }
    106.  
    107.      public int getResId() {
    108.          return resId;
    109.      }
    110.  
    111.      public void setResId(int resId) {
    112.          this.resId = resId;
    113.      }
    114.  
    115.  }
    116.  
    117.  ------------------------------------------------------
    118.    FileAdapter.java:
    119.    ------------------------------------------------------
    120.  package com.vsb.file;
    121.  
    122.  import java.util.ArrayList;
    123.  import java.util.List;
    124.  
    125.  import android.content.Context;
    126.  import android.view.View;
    127.  import android.view.ViewGroup;
    128.  import android.widget.BaseAdapter;
    129.  
    130.  public class FileAdapter extends BaseAdapter {
    131.  
    132.      private List<FileView> list;
    133.      private Context context;
    134.      
    135.      public FileAdapter(Context context) {
    136.          this.context = context;
    137.          list = new ArrayList<FileView>();
    138.      }
    139.      
    140.      public void addFile(FileView fv) {
    141.          list.add(fv);
    142.      }
    143.      @Override
    144.      public int getCount() {
    145.          return list.size();
    146.      }
    147.  
    148.      @Override
    149.      public Object getItem(int position) {
    150.          return list.get(position);
    151.      }
    152.  
    153.      @Override
    154.      public long getItemId(int position) {
    155.          return position;
    156.      }
    157.  
    158.      @Override
    159.      public View getView(int position, View convertView, ViewGroup parent) {
    160.          FileView fv;
    161.          if(convertView == null) {
    162.              fv = new FileView(context, null, list.get(position).getText().getText(), list.get(position).getResId());
    163.          } else {
    164.              fv = (FileView)convertView;
    165.              fv.setText(list.get(position).getText().getText())  ;
    166.              fv.setImage(list.get(position).getResId());
    167.          }
    168.          return fv;
    169.      }
    170.  
    171.  }        
    172.  
     

    Advertisement

  2. abcd2000

    abcd2000 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    5
    0
    5
    Found the issue. I forgot to add the image and text to the FileView in its constructor.
    Code (Text):
    1.  
    2. public FileView(Context context, File file, CharSequence lable, int resId) {
    3.         super(context);
    4.         this.file = file;
    5.         text = new TextView(context);
    6.         text.setText(lable);
    7.         image = new ImageView(context);
    8.         this.resId = resId;
    9.         image.setImageResource(resId);
    10.         addView(image);
    11.         addView(text);
    12.     }
    13.  
     

Share This Page

Loading...