Customized ListView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Customized ListView

Postby peter0601 » Wed Nov 19, 2008 4:32 pm

Hi folks,

I have the following problem. I have written my own ArrayAdapter class
which cutomizes a ListView. More precisely I would like to have a list
which contains a horizontal LinearLayout with one TextView and a
ImageView per row. I have tried to achieve this by using the attached
code which I created by following this tutorial
http://lbellonda.blogspot.com/2007/12/a ... -with.html

I have a class that extends ListActivity and looks like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DDboard extends ListActivity{
  2.  
  3.  
  4.  
  5.    @Override
  6.  
  7.    public void onCreate(Bundle savedInstanceState) {
  8.  
  9.              try{
  10.  
  11.        super.onCreate(savedInstanceState);
  12.  
  13.        setContentView(R.layout.dd_list);
  14.  
  15.              String[] string1 = new String[2];
  16.  
  17.        string1[0] = "Description 1";
  18.  
  19.        string1[1] = "green";
  20.  
  21.              String[] string2 = new String[2];
  22.  
  23.        string1[0] = "Description 2";
  24.  
  25.        string1[1] = "yellow";
  26.  
  27.              String[] string3 = new String[2];
  28.  
  29.        string1[0] = "Description 3";
  30.  
  31.        string1[1] = "red";
  32.  
  33.              ArrayList dAL = new ArrayList();
  34.  
  35.              dAL.add(string1);
  36.  
  37.        dAL.add(string2);
  38.  
  39.        dAL.add(string3);
  40.  
  41.              DArrayAdapter daa = new DArrayAdapter(this, dAL);
  42.  
  43.        setListAdapter(daa);
  44.  
  45.              }catch(Exception e){
  46.  
  47.            e.printStackTrace();
  48.  
  49.        }
  50.  
  51.    }
  52.  
  53. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Within this class, as you can see, I do instantiate DArrayAdapter and
sending the current context and my ArrayList. Depending on what is in
[1] of the String array I would like to place different images in my
ImageView. But have a look at that later.

DArrayAdapter looks like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public class DArrayAdapter extends ArrayAdapter{
  5.  
  6.  
  7.  
  8.    public DArrayAdapter(Context context, List<String[]> dds) {
  9.  
  10.        super(context, R.layout.dd_row, dds);
  11.  
  12.    }
  13.  
  14.      @Override
  15.  
  16.    public View getView(int position, View convertView, ViewGroup
  17.  
  18. parent){
  19.  
  20.        String[] stringArray = (String[]) getItem(position);
  21.  
  22.        DDListRow ddlr = new DDListRow(super.getContext(),
  23.  
  24. stringArray);
  25.  
  26.        return ddlr;
  27.  
  28.    }
  29.  
  30. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


And DDListRow, which extends LinearLayout looks like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class DemandDashboardListRow extends LinearLayout {
  3.  
  4.  
  5.  
  6.    //String Title="";
  7.  
  8.    TextView mText;
  9.  
  10.    ImageView mView;
  11.  
  12.      public DDListRow(Context context, String[] dd) {
  13.  
  14.        super(context);
  15.  
  16.              this.setOrientation(HORIZONTAL);
  17.  
  18.        this.setLayoutParams(new LinearLayout.LayoutParams
  19.  
  20. (LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  21.  
  22.              mText = new TextView(context);
  23.  
  24.        mText.setLayoutParams(new LinearLayout.LayoutParams
  25.  
  26. (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  27.  
  28.        addView(mText);
  29.  
  30.        setText(dd[0]);
  31.  
  32.              mView = new ImageView(context);
  33.  
  34.        mView.setLayoutParams(new LinearLayout.LayoutParams
  35.  
  36. (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  37.  
  38.              if (demand[1].equals("red")){
  39.  
  40.            mView.setImageResource(R.drawable.android_red);
  41.  
  42.        }
  43.  
  44.        if (demand[1].equals("yellow")){
  45.  
  46.            mView.setImageResource(R.drawable.android_yellow);
  47.  
  48.        }
  49.  
  50.        if (demand[1].equals("green")){
  51.  
  52.            mView.setImageResource(R.drawable.android_green);
  53.  
  54.        }
  55.  
  56.        addView(mView);
  57.  
  58.    }
  59.  
  60.    public void setText(String text)
  61.  
  62.    {
  63.  
  64.        mText.setText(text);
  65.  
  66.    }
  67.  
  68. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


I hope this is at least not complete BS but if I run the programm I
get a ClassCastException at the method call "setListAdapter(daa);" in
the ListActivity class. And to be honest, I don't have a clue what I
am doing wrong. The res/layout files are created as described in the
well-known notepad tutorial but I would post them immediately if they
are of any help for you guys to solve my issue.

Any help is greatly appreciated!!!

Cheers
Peter
peter0601
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 19, 2008 4:27 pm

Top

Postby peter0601 » Thu Nov 20, 2008 7:25 pm

Hi forum,

i solved the issue by choosing to use a BaseAdapter and following the tutorial from plusminus about "Iconified TextList - The making of"
http://www.anddev.org/iconified_textlis ... f-t97.html

But i don't have clue why my previous code was not working. Just out of curiousity I would like to know what went wrong there. Could it be the case that the problem lies in the constructor of ArrayAdapter. Because if I initialize

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. super(context, R.layout.dd_row, dds);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


and the file dd_row.xml does not match the View that I will generate with my overridden getView-method?

Cheers
Peter
peter0601
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 19, 2008 4:27 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests