列表视图未填充和getview未被调用

时间:2014-08-18 20:39:53

标签: android listview android-fragments

你好我正在制作音乐播放器,在主要活动中我有3个片段和下面的片段我有一个线性布局,它将显示当前歌曲的名称和图像。

现在的问题是,在我的第一个片段列表视图中没有填充,因为没有调用getview。

logcat显示没有错误,但没有填充。

MainActivity.java

package com.example.scrolltabs;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.example.scrolltabs.FragmentA;
import com.example.scrolltabs.FragmentB;
import com.example.scrolltabs.FragmentC;


public class MainActivity extends FragmentActivity {

    ViewPager viewpager=null;

    static ArrayList<String> data ;
    static ArrayList<String> artist ;
    static ArrayList<String> album ;
    static ArrayList<String> title ;
    static ArrayList<String> displayname ;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);

        data = new ArrayList<String>();
        artist = new ArrayList<String>();
        album = new ArrayList<String>();
        title = new ArrayList<String>();
        displayname = new ArrayList<String>();


        getMyCursor();

        viewpager=(ViewPager) findViewById(R.id.pager);

        FragmentManager fm=getSupportFragmentManager();

        viewpager.setAdapter(new MyAdapter(fm));
    }

    public static ArrayList<String> getDataArrayList()
    {
        return data;
    }

    public static ArrayList<String> getArtistArrayList()
    {
        return artist;
    }

    public static ArrayList<String> getAlbumArrayList()
    {
        return album;
    }

    public static ArrayList<String> getTitleArrayList()
    {
        return title;
    }

    public static ArrayList<String> getDisplayNameArrayList()
    {
        return displayname;
    }


    public Cursor getMyCursor()
    {
        Log.v("JAY","sav pela");

        /*  String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
                android.provider.MediaStore.Audio.Albums.ALBUM };

        Cursor cursor = getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, null,
                null, null);

        if (cursor.moveToFirst()) {
            do {



        String[] column = { MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.MIME_TYPE,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.ARTIST,
        };

        String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";



        String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                column,null,null, orderBy);

        int songcount=0;
        if (cursor.moveToFirst()) {
            do {
                songcount++;

                String s_title =cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                title.add(s_title);

                String s_displayname=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                displayname.add(s_displayname);

                String s_data=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                data.add(s_data);

                String s_album=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                album.add(s_album);

                String s_artist=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                artist.add(s_artist);




            } while (cursor.moveToNext());
        }

        Log.v("JAY",songcount+"");

        return cursor;
    }



}

class MyAdapter extends FragmentStatePagerAdapter
{

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment=null;

        if(arg0==0)
        {
            fragment= new FragmentA();
        }
        else if(arg0==1)
        {
            fragment=new FragmentB();
        }
        else if(arg0==2)
        {
            fragment=new FragmentC();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(position==0)
        {
            return "All Songs";
        }
        else if(position==1)
        {
            return "Tab2"; 
        }
        else if(position==2)
        {
            return "Tab3";
        }
        return null;
    }
}

activity_main.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

     >

   <android.support.v4.view.ViewPager 
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/pager"
        android:layout_weight="2"
        >

    <android.support.v4.view.PagerTitleStrip
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="#000000"
        android:id="@+id/title"
        android:layout_gravity="top"
        android:paddingTop="20dp"
        android:paddingBottom="4dp"
        android:textColor="#FFFFFF"

       >

    </android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:background="#5edfe5"
        android:clickable="true"
        android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:contentDescription="Albumart"
        android:src="@android:drawable/ic_media_play" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>

</LinearLayout>

FragmentA.java

package com.example.scrolltabs;

import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentA extends ListFragment {

    View view=null;



    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootview = inflater.inflate(R.layout.fragmentlayout1,container,false);
        view=rootview;
        return rootview;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        Log.v("JAY","onactivitycreated");

        ListView listview = (ListView) view.findViewById(android.R.id.list) ;

        MyListAdapter adapter = new MyListAdapter(getActivity().getApplicationContext(),R.layout.fragmentlayout1);

        listview.setAdapter(adapter);

    }

}

class MyListAdapter extends ArrayAdapter<String>{

    ArrayList<String> data ;
    ArrayList<String> artist ;
    ArrayList<String> album ;
    ArrayList<String> title ;
    ArrayList<String> displayname ;

    public MyListAdapter(Context applicationContext, int fragmentlayout1) {
        // TODO Auto-generated constructor stub
        super(applicationContext, fragmentlayout1);
        Log.v("JAY","in adapter");
        data=com.example.scrolltabs.MainActivity.getDataArrayList();
        artist=com.example.scrolltabs.MainActivity.getArtistArrayList();
        album=com.example.scrolltabs.MainActivity.getAlbumArrayList();
        title=com.example.scrolltabs.MainActivity.getTitleArrayList();
        displayname=com.example.scrolltabs.MainActivity.getDisplayNameArrayList();



    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.v("JAY","in get view");
        LayoutInflater inf = (LayoutInflater) convertView.getContext().getSystemService(convertView.getContext().LAYOUT_INFLATER_SERVICE);
        View row = inf.inflate(R.layout.peritem, parent, false);

        TextView title_tv = (TextView) row.findViewById(R.id.titletextview);
        TextView artist_tv = (TextView) row.findViewById(R.id.artisttextview);

        title_tv.setText(title.get(position));
        artist_tv.setText(artist.get(position));



        return row;

    }

}

fragmentlayout1.xml 片段1的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ListView>

</LinearLayout>

per_item.xml (片段1中列表中每个项目的布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titletextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/artisttextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

初始化MyListAdapter时,您正在使用此constructor。 第二个参数是资源ID:

包含在实例化视图时使用的TextView的布局文件的资源ID。但是你不需要这个,因为你手动膨胀项目视图:

View row = inf.inflate(R.layout.peritem, parent, false); 

尝试使用this

super(context, 0, data);

<强> UPD。 在MyListAdapter构造函数中尝试:

super(context, 0);

<强> UPD。 2  尝试setListAdapter(adapter)而不是listview.setAdapter(adapter);

<强> UPD。 3 来自Android docs

您必须使用ListFragment.setListAdapter()将列表与适配器相关联。不要直接调用ListView.setAdapter(),否则将跳过重要的初始化。