洗牌自定义适配器

时间:2014-12-03 20:26:47

标签: android android-intent android-activity

我的问题是我正在尝试将字符串数组移到Shuffle并将随机列表显示回列表视图。我的随机播放器位于我的自定义适配器中。

My SongAdapter

public class SongAdapter extends ArrayAdapter<Model> {
 public static String[] listSongs = new String[]{}; //String Array of ListSongs#
public void loadFile() {
        try {
            Resources ResFiles = myContext.getResources(); //ResFile is set to getResources,
            InputStream ReadDbFile = ResFiles.openRawResource(R.raw.song); //InputStream is Called to get Text file Songs
            BufferedReader reader = new BufferedReader(new InputStreamReader(ReadDbFile));
            String DbLines;
            while ((DbLines = reader.readLine()) != null)//While DBlines is not Null keep Reading.
            {
                listSongs = DbLines.split("#");//Dblines is Splitting the Text where # is..
                Model SongModel1 = new Model(); //Call the Model Class
                SongModel1.setName(listSongs[0]);//Set Name from String Array
                SongModel1.setSigner(listSongs[1]);//Set Signer from String Array
                SongModel1.setUrl(listSongs[2]); //Set URL from String Array
                this.add(SongModel1);//Add SongModel String Array to the Model Class
            }
            //Catch any Exception..
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void shuffle(){
        Collections.shuffle(Arrays.asList(listSongs));
        notifyDataSetChanged();
    }

列出片段

public class MenuFragment extends ListFragment {
    static protected SongAdapter adapter;



    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, container, false);
        adapter = new SongAdapter(getActivity(),
                android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        setHasOptionsMenu(true);
        return view;
    }

最后,我在主要课程

中的ActionBar On Click中调用
public class MainActivity extends ActionBarActivity {
    static protected SongAdapter adapter;
@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId()) {
            case R.id.shuffle:
                adapter.shuffle();
               return true;

我在运行时遇到的错误是

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.dfoley.project2, PID: 1304
    java.lang.NullPointerException
            at com.example.dfoley.project2.MainActivity.onOptionsItemSelected(MainActivity.java:135)
            at android.app.Activity.onMenuItemSelected(Activity.java:2600)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
            at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
            at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
            at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:551)
            at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
            at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
            at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
            at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您应该使用MenuFragment.adapter.shuffle(),因为adapter上的MainActivity与另一个MenuFragment不同(位于MenuFragment内)

作为建议,您应该在adapter.shuffle()上为MainActivity创建一个公共方法,并通过{{1}}进行调用。

答案 1 :(得分:1)

SongAdapter中的FragmentActivity中的array.shuffle()不同,只会初始化片段中的{{1}}。您可以做的是在片段中定义一个调用{{1}}的方法,并在按下菜单按钮时调用此方法