Question

*EDIT:*Ive got this to work now. Made the necessary changes in the code below.

I am trying to create a slidng menu in and android application. The sliding menu must contain a customized list view with an image and a text view. Although I am able to get the sliding menu, I cannot seem to get any content into the list view.

This is my Main activity:

public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Context context=this;
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v=inflater.inflate(R.layout.slidingmenu, null, true);



    final ListView lv=(ListView) v.findViewById(R.id.listView);

    SlidingMenu menu=new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidth(5);
    menu.setFadeDegree(0.0f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setBehindWidth(100);
    menu.setMenu(v);
    menu.setOnOpenListener(new OnOpenListener() {

        @Override
        public void onOpen() {
            MenuAdapter ma=new MenuAdapter(context);
            lv.setAdapter(ma);
        }
    });


}

My Adapter class:

public class MenuAdapter extends ArrayAdapter<String>{
Context context;

@Override
public int getCount() {
    return 2;
}

public MenuAdapter(Context c) {
    super(c, R.layout.menucontent);
    context=c;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    /*int height=LinearLayout.LayoutParams.MATCH_PARENT;
    int width=LinearLayout.LayoutParams.MATCH_PARENT;*/


    LayoutInflater inflater=(LayoutInflater) context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view=inflater.inflate(R.layout.menucontent, parent, false);
    TextView tv=(TextView) view.findViewById(R.id.tv_content);

    tv.setText("Hello World");

    return view;
}   

}

P.S: I have 3 different XML files- main.xml which is the launcher activity, slidingmenu.xml which displays the menu and finally menucontent.xml which has the text and image views that should be displayed in the list.

Was it helpful?

Solution

Override, getCount() method of MenuAdapter to return some value greater than 1 like, 2, 10. Then you will be able to see the list items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top