Multiple listviews in one layout android
Asked Answered
B

6

5

I want to populate two listviews in one activity. For that i placed two listviews in one layout. My problem is that both listviews are populated but only one listview is visible. Now when i make one layout INVISIBLE, it will display another listview.

Here i am posting my xml file. Is there any problem in my layout ? Please suggest solution.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:id="@+id/playlist_lyt"
    >

    <Button 
        android:id="@+id/btnAlbumwise"
        android:text="View Albumwise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        />

    <RelativeLayout 
        android:id="@+id/layout_songlist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_below="@+id/layout_albumlist"  
        >

        <EditText android:id="@+id/inputSearch"
            android:layout_width="fill_parent"
            android:layout_height="33dp"
            android:hint="Search.."
            android:inputType="textVisiblePassword"
            android:cursorVisible="false"
            android:background="@drawable/rounded_edittext" 
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"            
        />

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="#000000"
            android:dividerHeight="4dp"
            android:listSelector="@drawable/list_selector"
            android:layout_below="@+id/inputSearch" 
            android:paddingTop="8dp"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layout_albumlist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:layout_below="@+id/btnAlbumwise"        
        >

         <ListView  
            android:id="@+id/liist"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="#242424"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector"
        />
    </RelativeLayout>
</RelativeLayout>

Code to populate listview "liist". PlaylistActivity.java

      Cursor cursor1 = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,null, null, null);
      if (cursor1 == null) 
      {
          //Query Failed , Handle error.
      }
      else if (!cursor1.moveToFirst()) 
      {
         //No media on the device.
      }
      else
      {   
        //  int albumName = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
          int id = cursor1.getColumnIndex(MediaStore.Audio.Albums._ID);
          int albumartist = cursor1.getColumnIndex(MediaStore.Audio.Albums.ARTIST);

          for(int i=0;i<cursor1.getCount();i++)
          {
                String Name = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM));
                Integer albumid = cursor1.getInt(id);                       
                String artist = cursor1.getString(albumartist);

                Bitmap bitmap = null;
                bitmap = getArtwork(context, albumid);
                    lstImage.add(bitmap);
                    lstName.add(Name);
                    lstArtist.add(artist);

                cursor1.moveToNext();
           }
       }
       cursor1.close();                    

       rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < lstName.size(); i++) {
            RowItem item = new RowItem(lstImage.get(i), lstName.get(i) , lstArtist.get(i));
            rowItems.add(item);
        }          
        CustomListViewAdapter adapter = new CustomListViewAdapter(PlayListActivity.this,
                R.layout.list_item, rowItems);
        lv1.setAdapter(adapter);
        lv1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                RowItem item = rowItems.get(position);
                String title = item.getTitle();
                Intent in = new Intent(PlayListActivity.this,AlbumsongListActivity.class);

                // Sending songIndex to PlayerActivity
                in.putExtra("albumTitle", title);
                in.putExtra("list", 0);
                in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(in);

            }
        });

Now when i click on item it starts another activity Albumsonglistactivity.java

public class AlbumsongListActivity extends ListActivity {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
String[] from = { "songTitle", "artist" };
        int[] to = { R.id.album_songTitle, R.id.album_songArtist };
        adapter = new SimpleAdapter(this, getAlbumSongList(context),
                R.layout.albumsonglist_item, from, to) 
        {
            public long getItemId(int position) 
            {
                return songsListData.indexOf(getItem(position));
            }
        };

        setListAdapter(adapter);
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {
                // getting listitem index
                int songIndex = (int) id;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),MusicPlayertemp.class);
                in.putExtra("songIndex", songIndex);
                in.putExtra("albumtitle", albumName);
                in.putExtra("listFlag", 3);
                startActivity(in);              
            }
        });
    }

    public ArrayList<HashMap<String, String>> getAlbumSongList(Context c) 
    {
        String whereClause = MediaStore.Audio.Media.ALBUM + " = ? ";
        String s[] = new String[] { albumName };
        Cursor cursor = c.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, whereClause,s, MediaStore.Audio.Media.TITLE);
        if (cursor == null) 
        {
            // Query Failed , Handle error.
        } 
        else if (!cursor.moveToFirst()) 
        {
            // No media on the device.
        } 
        else 
        {
            int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
            int artistcolumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
            albumid = cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM_ID));
            int id = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
            for (int i = 0; i < cursor.getCount(); i++) 
            {
                String thisTitle = cursor.getString(titleColumn);
                String path = cursor.getString(idColumn);
                String artist = cursor.getString(artistcolumn);         
                String ID = cursor.getString(id);
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", thisTitle);
                song.put("songPath", path);
                song.put("artist", artist);
                song.put("id", ID);
                // Adding each song to SongList
                songsList.add(song);
                cursor.moveToNext();
            }
        }
        // looping through playlist
        for (int i = 0; i < songsList.size(); i++) 
        {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);
            // adding HashList to ArrayList
            songsListData.add(song);
        }
        return songsListData;
    }

In albumsonglistActivity, if i click on item it doesn't invoked..

Bedim answered 17/1, 2013 at 11:37 Comment(7)
in first listview only 3 items are present then also it does'n show another listview. To view another listview , i have to hide one layout. After that, if i click on second listview item then it will start another activity which also contains listview but this listview items are not responsive. It means when i click on any item..it doesn't fire onItemClick() event...Bedim
Have you set the listener to listview using setOnItemClickListener?Vladimir
yes...i have set listener.Bedim
Have you checked by putting a log message inside your onItemClickListener method?Vladimir
i have set breakpoints at each line of onItemClickListener but debugger doesn't go there. So there is no use to put log message inside it.Bedim
Follow this answer for your solution https://mcmap.net/q/1922355/-android-onitemclicklistener-in-listview-not-workingVladimir
Also if you are using samsung Galexy S then might have this issue. https://mcmap.net/q/1922356/-stepping-through-android-code-on-the-phone-big-line-number-discrepancyVladimir
V
5

This is the problem because you are setting another layout below the first one and first one takes height to fill_parent.

You should use Linear Layout and put both listview inside that and use layout_weight property to make both listview same height.

For example you can follow below hierarchy. Its not original code but it just a structure which you can use to develop your layout

<LinearLayout orientation="verical">
   <ListView layout_weight=1></ListView>   
   <ListView layout_weight=1></ListView>
</LinearLayout>
Vladimir answered 17/1, 2013 at 11:45 Comment(1)
ok..but now, if i click on secondlistview item it starts another activity which has other layout having listview and it is populatd automatically. When i click on this new listviews item, it doesn't fire onItemCLick event. And logcat shows "couldn't save id of...". So what is the problem ?Bedim
K
3

you have given android:layout_height="fill_parent" to both Relative layout. this is why you are unable to show both listviews.

use linear layout and By using weight you can divide the screen for displaying both listviews

Khiva answered 17/1, 2013 at 11:42 Comment(0)
E
3

Add

android:layout_weight = "1" 

to both RelativeLayouts that contains the listView

Erleena answered 17/1, 2013 at 11:46 Comment(0)
D
2

Try updating your code like this

<RelativeLayout 
    android:id="@+id/layout_songlist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:layout_below="@+id/layout_albumlist"  
    >

    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="33dp"
        android:hint="Search.."
        android:inputType="textVisiblePassword"
        android:cursorVisible="false"
        android:background="@drawable/rounded_edittext" 
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"            
    />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#000000"
        android:dividerHeight="4dp"
        android:listSelector="@drawable/list_selector"
        android:layout_below="@+id/inputSearch" 
        android:paddingTop="8dp"/>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/layout_albumlist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:layout_below="@+id/btnAlbumwise"        
    >

     <ListView  
        android:id="@+id/liist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#242424"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
    />
</RelativeLayout>
Dependable answered 17/1, 2013 at 11:43 Comment(0)
W
2

The second layout is fill_parent and the first one is placed below it. So it is impossible to see.

I assume you want something like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/playlist_lyt"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="10dp" >

    <Button
        android:id="@+id/btnAlbumwise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View Albumwise" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:weightSum="1" >

        <RelativeLayout
            android:id="@+id/layout_albumlist"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <ListView
                android:id="@+id/liist"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:divider="#242424"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/list_selector" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout_songlist"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <EditText
                android:id="@+id/inputSearch"
                android:layout_width="fill_parent"
                android:layout_height="33dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/rounded_edittext"
                android:cursorVisible="false"
                android:hint="Search.."
                android:inputType="textVisiblePassword" />

            <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/inputSearch"
                android:divider="#000000"
                android:dividerHeight="4dp"
                android:listSelector="@drawable/list_selector"
                android:paddingTop="8dp" />
        </RelativeLayout>

    <RelativeLayout>
</RelativeLayout>
Walt answered 17/1, 2013 at 11:46 Comment(0)
A
0

Best solution for this is to create a custom ScrollView and use it to replace <ScrollView> with <in.parx.VerticalScrollView>

Use following for VerticalScrollView.java

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VerticalScrollView extends ScrollView{

    public VerticalScrollView(Context context) {
        super(context);
    }

    public VerticalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                super.onTouchEvent(ev);
                break;

            case MotionEvent.ACTION_MOVE:
                return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                super.onTouchEvent(ev);
                break;

            case MotionEvent.ACTION_UP:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " +ev.getAction());
        return true;
    }
}
Arkansas answered 17/10, 2016 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.