sowens
sowens

Reputation: 361

How can I show a different listview for each button pressed

I have a row of buttons that I want to populate a listview. That looks like this:


Button1 | Button2 | Button3 | Button4

List Item 1

List Item 2

List Item 3

If I press Button1, I will show one view and if I select Button2, I will show another view. I have the listview working with no button selected. However, I want to select a button and populate a listveiw based on the button selected. This is only my 2nd app so I may be going about it the wrong way.

Here is my layout file:

<LinearLayout 
    android:layout_weight="2" 
    android:layout_height="fill_parent" 
    android:layout_width="match_parent" 
    >
<VideoView 
    android:id="@+id/video_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout> 

<LinearLayout 
    android:layout_weight="1" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:orientation="vertical"  >  
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/chapters"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/chapters" /> 

        <Button
            android:id="@+id/scales"
            android:layout_width="100dp"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/scales" />

        <Button
            android:id="@+id/b_flat"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/b_flat" /> 

        <Button
            android:id="@+id/e_flat"
            android:layout_width="75dp"
            android:layout_height="@dimen/btn_layout_height"    
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/e_flat" /> 

        <Button
            android:id="@+id/concert"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/concert" />

        <Button
            android:id="@+id/bass"
            android:layout_width="75dp"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/bass" />  

        <Button
            android:id="@+id/video"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/video" />

        <Button
            android:id="@+id/practice"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/btn_layout_height"    
            android:textSize="@dimen/text_size"
            android:background="@drawable/button_custom"
            android:text="@string/practice" />                                                      
    </LinearLayout>

<FrameLayout 
          android:id="@+id/fragmentContainer"
          android:layout_height="match_parent"
          android:layout_width="match_parent" 
          />
</LinearLayout>

I want to select the "Chapters" button and populate the list button or select "concert" and populate the listview.

I don't know where or how to set the button pressed. Here is what I tried unsuccessfully:

     public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";

private Button mChaptersButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_improvisation);
    mChaptersButton = (Button)v.findViewById(R.id.chapters);
    mChaptersButton.setPressed(true);

    getActivity().setTitle(R.string.chapters_title);
    mChapters = ChapterList.get(getActivity()).getChapters();

    ChapterAdapter adapter = new ChapterAdapter(mChapters);
    setListAdapter(adapter);        

}

This didn't work because I can't use SetContentView in ListFragment. Also, the buttons didn't have focus. Any suggestions would be greatly appreciated

Upvotes: 0

Views: 363

Answers (1)

JNI_OnLoad
JNI_OnLoad

Reputation: 5442

create common listview adapter and populate the listview with different data set on each button click. Make sure to call your adapter.clear() so that the old data from listview will disappear. I hope you got the idea. For setContentView, non of ListFragment and Fragment parent have thesetContentView()` method. You should have an Activity with a Layout and set your ListFragment there. rest check this link Fragment setcontentView Error

Upvotes: 1

Related Questions