title: Dynamically Creating Multiple ListViews in Android
body: <p>I am currently in the process of developing a basic news aggregation app in android, I know there's loads out there but i'm basically just creating one because I think its a good place to begin for someone who's just starting out with Android development. </p>
  
  <p>My goal is to display articles from several feeds. Each feed will have its own horizontal sliding list, kind of like the pulse news app. So far I have managed to find a tutorial on creating a "HorizontalListView" and I now have my app displaying a basic horizontal sliding listview like below:</p>
  
  <p><strong>Screenshot Link:</strong> <a href="http://www.dev-smart.com/wp-content/uploads/2011/03/device-200x300.png" rel="nofollow">http://www.dev-smart.com/wp-content/uploads/2011/03/device-200x300.png</a></p>
  
  <p><strong>Pulse App Screenshot:</strong> <a href="http://a1525.phobos.apple.com/us/r1000/080/Purple/v4/ba/6a/01/ba6a01d1-f0b7-4bb7-3f94-5a5761653e3c/mzl.zxnjzzmk.480x480-75.jpg" rel="nofollow">http://a1525.phobos.apple.com/us/r1000/080/Purple/v4/ba/6a/01/ba6a01d1-f0b7-4bb7-3f94-5a5761653e3c/mzl.zxnjzzmk.480x480-75.jpg</a></p>
  
  <p>Code:</p>
  
  <pre><code>public class HorizontalListViewDemo extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.listviewdemo);
  
      HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
      listview.setAdapter(mAdapter);
  
  
  }
  
  private static String[] dataObjects = new String[]{ "Text #1",
      "Text #2",
      "Text #3", "Text #4"  }; 
  
  private BaseAdapter mAdapter = new BaseAdapter() {
  
      private OnClickListener mOnButtonClicked = new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              AlertDialog.Builder builder = new AlertDialog.Builder(HorizontalListViewDemo.this);
              builder.setMessage("hello from " + v);
              builder.setPositiveButton("Cool", null);
              builder.show(); 
          }
      };
  
      @Override
      public int getCount() {
          return dataObjects.length;
      }
  
      @Override
      public Object getItem(int position) {
          return null;
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
          TextView title = (TextView) retval.findViewById(R.id.title);
          Button button = (Button) retval.findViewById(R.id.clickbutton);
          button.setOnClickListener(mOnButtonClicked);
          title.setText(dataObjects[position]);
          return retval;
      }   
  };
  }
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  &gt;
  
  &lt;LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000"
    &gt;
  
    &lt;com.devsmart.android.ui.HorizontalListView
      android:id="@+id/listview"
      android:layout_width="fill_parent"
      android:layout_height="250dp"
      android:background="#ddd"
    /&gt;
  
  &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>As I mentioned each HorizontalListView will represent 1 feed and display its associated articles as above. So my next step in this development is to try and make the creation of these HorizontalListView's dynamic so a user could declare a feed from within the app and it would then dynamically be created below the already defined ones.</p>
  
  <p>Disregarding all other efforts involved in that process I am really only asking for help with creating a new HorizontalListView that can recycle the already defined XML listview layout id="listview". I know I could get this to work if I where to pre-define all feeds and just create unique XML  layouts with unique id's, but I'm wondering is there a way I can define a new HorizontalListView that reuses existing layouts.</p>
  
  <p>I hope you guys can maybe shed some light on this for me.</p>
  
  <p>Thanks</p>
  
  <p><strong>Update 1:</strong>
  using Views?</p>
  
  <pre><code>//Defining main XML file "listviewdemo.xml"
  setContentView(R.layout.listviewdemo);
  //Creating a new view to apply to my HorizontalListViews, /res/layout/listviewStyle.xml
  View view = getLayoutInflater().inflate(R.layout.listviewStyle, null);
  
  //Defining my first HorizontalListView      
  HorizontalListView listview = (HorizontalListView) view.findViewById(R.id.listviewReuse);
  listview.setAdapter(mAdapter);
  </code></pre>