2013-01-17 50 views
5

我想在一个活动中填充两个列表视图。为此,我在一个布局中放置了两个列表视图。我的问题是,这两个listviews填充,但只有一个列表视图是可见的。现在,当我使一个布局INVISIBLE,它将显示另一个列表视图。一个布局中的多个列表视图android

在这里,我张贴我的XML文件。我的布局有什么问题吗?请建议解决方案。

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

填充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); 

      } 
     }); 

现在,当我在项目点击启动另一个活动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; 
    } 

在albumsonglistActivity,如果我在项目点击它不调用..

+0

在第一个listview中只有3个项目存在,那么它也不显示另一个l istview。要查看另一个列表视图,我必须隐藏一个布局。在那之后,如果我点击第二个listview项目,那么它将启动另一个也包含listview的活动,但是这个listview项目没有响应。这意味着当我点击任何项目..它不会触发onItemClick()事件... – zanky

+0

你使用setOnItemClickListener设置监听器为listview? – Dharmendra

+0

是的......我设置了监听器。 – zanky

回答

3

你给出了相对布局android:layout_height="fill_parent"。这就是为什么你无法显示两个列表视图。

使用线性布局,通过使用权重,可以把屏幕同时显示的列表视图

2

尝试更新这样

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

这是你的代码是问题,因为你设置以下的第一另一布局一个和第一个将高度填充到fill_parent。

你应该使用线性布局,并将两个列表视图,并使用layout_weight属性,使两个列表视图相同的高度。

例如,您可以按照下面的层次结构进行操作。它不是原来的代码,但它只是你可以用它来开发你的布局结构

<LinearLayout orientation="verical"> 
    <ListView layout_weight=1></ListView> 
    <ListView layout_weight=1></ListView> 
</LinearLayout> 
+0

好吧,但现在,如果我点击secondlistview项目它开始另一个活动,其他布局有ListView,它是自动populatd。当我点击这个新的listviews项时,它不会触发onItemCLick事件。而logcat节目“无法保存...的ID”。那么问题是什么? – zanky

+0

@zanky粘贴您的代码以更好地了解您的问题。 – Dharmendra

+0

我编辑了我的问题..我可以参考我的代码.. – zanky

3

添加

android:layout_weight = "1" 

RelativeLayouts包含listView

2

第二个布局是FILL_PARENT和第一个放在它下面。所以不可能看到。

我想,你希望这样的事情

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

这个最好的解决方案是创建一个自定义滚动型,并将其与< in.parx.VerticalScrollView>

使用使用 取代<ScrollView>以下为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; 
    } 
} 
相关问题