填充Listview NOT不在当前布局中

时间:2014-03-27 18:59:18

标签: java android listview android-listview

我正在使用流行的SlidingMenu库,其唯一的东西是listview。所以我尝试使用数组适配器用我拥有的ArrayList填充它,但它给了我一个NullPointer异常。所以我有一种直觉,我应该尝试使用listview将 activity 的布局设置为布局,当然,它确实有效。但这并不能解决我的问题,因为我需要在DRAWER中使用listview。有人可以帮忙吗?

package josh.hutchins.frcmatchscouter2014;

import java.util.ArrayList;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    BlueAllianceHandler bah = new BlueAllianceHandler("sc");
    FileIoHandler fih = new FileIoHandler(/*maybe pass in folder path here?*/);

    ArrayList<String> testTeams = new ArrayList<String>();

     ListView drawerListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerListView = (ListView) findViewById(R.id.drawerListView);


       setupSlidingMenu();
       //Toast.makeText(this, bah.pullData(), Toast.LENGTH_LONG).show();
       pullTeamData();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    void setupSlidingMenu(){
        SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.menu);
    }

    void pullTeamData(){
        //JUST FOR TESTING PURPOSES
        testTeams.add("2655");
        testTeams.add("1533");
        testTeams.add("900");
        testTeams.add("4451");
        testTeams.add("2200");
        testTeams.add("3301");
        testTeams.add("2059");
        testTeams.add("5010");
        testTeams.add("1747");
        testTeams.add("2641");
        testTeams.add("4231");

        fih.createTeamRecords(testTeams);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this, 
                android.R.layout.simple_list_item_1,
                testTeams );

        drawerListView.setAdapter(arrayAdapter);
    }
}

1 个答案:

答案 0 :(得分:0)

请按以下步骤操作:

1.在MainActivity中扩展SlidingFragmentActivity而不仅仅是Activity。

2.在布局文件夹中设置两种FrameLayout类型的布局,如下所示

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" /> 

将名称布局命名为menu_frame,content_frame,menu_frame将保留您需要的列表视图,而content_frame将保存内容。

3.在MainActivity中,执行此操作。

SlidingMenu menu;
@Override
    public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setBehindContentView(R.layout.menu_frame);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.menu_frame, new MenuFragment()).commit();

    setContentView(R.layout.content_frame);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new ShockFeedsFragment()).commit();

    ab=getActionBar();

    menu = getSlidingMenu();
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    menu.setShadowDrawable(R.drawable.shadow);
    menu.setBehindOffset(200);
    menu.setMode(SlidingMenu.LEFT_RIGHT );
}
  1. 现在设计左侧菜单的布局,只需在线性布局中包含一个列表视图,并将布局命名为left_menu。

  2. 创建一个名为MenuFragment的片段,扩展ragment类,在onCreateView方法中,设置菜单列表视图,如下所示

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v=inflater.inflate(R.layout.left_menu, container, false);
    lv=(ListView)v.findViewById(R.id.menuListView);
    iv=(ImageView)v.findViewById(R.id.imageMenuProfile);
    tv=(TextView)v.findViewById(R.id.textViewProfileName);
    LeftMenuAdapter adap=new LeftMenuAdapter(getActivity(), list);
    //lv.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),R.layout.ledt_en_row,R.id.textList,new String[] {"H","R","S","B","C","W"}));
    lv.setAdapter(adap);
    lv.setOnItemClickListener(new MenuListListener());
    
    return v;
    } 
    
  3. 我已经实现了相同的库,以这种方式做到了,完美地工作,希望它对你也一样:)...祝你好运。