user1356029
user1356029

Reputation: 381

Dynamically Creating Multiple ListViews in Android

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.

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:

Screenshot Link: http://www.dev-smart.com/wp-content/uploads/2011/03/device-200x300.png

Pulse App Screenshot: http://a1525.phobos.apple.com/us/r1000/080/Purple/v4/ba/6a/01/ba6a01d1-f0b7-4bb7-3f94-5a5761653e3c/mzl.zxnjzzmk.480x480-75.jpg

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;
    }   
};
}

XML:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<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"
  >

  <com.devsmart.android.ui.HorizontalListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:background="#ddd"
  />

</LinearLayout>
</ScrollView>

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.

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.

I hope you guys can maybe shed some light on this for me.

Thanks

Update 1: using Views?

//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);

Upvotes: 2

Views: 1165

Answers (1)

Nathan
Nathan

Reputation: 1767

HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);

This line will always return the first view found with R.id.listview. Not very nice when you have multiple list views.

Now instead of calling Activity.findViewById() call findViewById() on a subview. You can contain each listview inside of a layout. Then call findViewById to find the layout that is the parent to the listview. The call findViewById on the layout.

Once you have found the listView the first time remember it for later use.

In the updated code you posted, remove this line. It is not doing anything b/c this view is not apart of the layout R.layout.listviewdemo.

View view = getLayoutInflater().inflate(R.layout.listviewStyle, null);

Then in The xml for R.layout.listviewdemo add both listviews to the same .xml (assuming that is your goal). Whatever the case maybe you should add all of your views to the same layout then retrieve the views using findViewById.

Upvotes: 1

Related Questions