Trying to Make ListView (new developer)


Last Updated:

  1. Twan

    Twan New Member This Topic's Starter

    Joined:
    Jul 7, 2010
    Messages:
    1
    Likes Received:
    0
    I'm just trying to spit out a ListView based on the Strings in an ArrayList of mine. I'm getting a NullPointerException.

    Code (Text):
    1.  
    2. package org.me.mixtapes;
    3.  
    4. import android.app.Activity;
    5. import android.app.AlertDialog;
    6. import android.content.Context;
    7. import android.os.Bundle;
    8. import android.util.AttributeSet;
    9. import android.view.View;
    10. import android.view.ViewGroup.LayoutParams;
    11. import android.widget.Button;
    12. import android.widget.LinearLayout;
    13. import android.widget.ListView;
    14. import android.widget.TextView;
    15. import java.util.ArrayList;
    16. import java.util.List;
    17.  
    18. /**
    19.  *
    20.  * @author achuinard
    21.  */
    22. public class MixTape extends Activity {
    23.  
    24.     /** Called when the activity is first created. */
    25.  
    26.  
    27.     private Button generator;
    28.     private LinearLayout myLayout;
    29.     private List<String> urlList = new ArrayList<String> ();
    30.     private TextView tempTextView;
    31.     private ListView songList;
    32.     private List<TextView> textViews  = new ArrayList<TextView>();
    33.     private AttributeSet textViewAttr;
    34.     private AlertDialog alerter;
    35.  
    36.     @Override
    37.     public void onCreate(Bundle icicle) {
    38.         super.onCreate(icicle);
    39.         songList = (ListView) findViewById(R.id.songlist);
    40.         urlList.add("http://1.com");
    41.         urlList.add("http://2.com");
    42.         urlList.add("http://3.com");
    43.         urlList.add("http://4.com");
    44.         // setContentView(R.layout.main);
    45.  
    46.         // grab display elements
    47.         // generator = (Button) findViewById(R.id.generate);
    48.        
    49.         myLayout = new LinearLayout(this);
    50.         for (String url : urlList) {
    51.             tempTextView = new TextView(this);
    52.             tempTextView.setText(url);
    53.             textViews.add(tempTextView);
    54.  
    55.         }
    56.  
    57.         for (TextView t : textViews) {
    58.             songList.addFooterView(t);
    59.         }
    60.        
    61.         myLayout.addView(songList);
    62.         setContentView(myLayout);
    63.     }
    64.  
    65. }
    66.  
     

    Advertisement
  2. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    Twan,

    I think you're going about this the wrong way. You should take a look at the Android ListView tutorial. ListViews are much easier than you think.

    The code that should handle what you're intending to do should be fairly small. Something like:
    Code (Text):
    1.  
    2.     public void onCreate(Bundle icicle) {
    3.         super.onCreate(icicle);
    4.         setContentView(R.layout.main);
    5.        
    6.         songList = (ListView) findViewById(R.id.songlist);
    7.        
    8.         urlList.add("http://1.com");
    9.         ...
    10.        
    11.         songList.setAdapter(new ArrayAdapter(this, R.layout.row, R.id.text1, urlList);
    12.     }
    13.  
    The only thing that I really added was the ArrayAdapter which tells the ListView what the contents of each row will be. What I'm not showing you is that you need to create another layout file called "row.xml", and it should have a TextView with an id of "text1".

    --Boogs
     

Share This Page

Loading...