Question Detail

I want to implement ListView for a Food List there should be only a Food name. How can I do it without Adapter in Android?

Please Suggest me a very easy way to implement ListView.


Thread Reply

Hemant Sharma

- 2 years ago

 

main_activity.xml file put it in your main activity XML file

<ListView
    android:id="@+id/foodCategory_list_view_ID"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

 

This is for List Item layout:  create_food_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/headingTextStyle_18"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:layout_marginLeft="@dimen/headingTextSize"
    android:gravity="center|left"
    android:textColor="@color/offBlack" />

//NO NEED HERE ANY ID OR CLICK EVENT IT WILL HANDLE BY LISTVIEW

 

FOR ACTIVITY

In your java File: MainActivity.java

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.create_food_list_item, mobileArray);
ListView listView = (ListView)findViewById(R.id.foodCategory_list_view_ID);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
    }
});

FOR FRAGMENT

View view = inflater.inflate(R.layout.fragment_create_food_mainlayout, container, false);
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.create_food_list_item, mobileArray);
ListView listView = (ListView) view.findViewById(R.id.foodCategory_list_view_ID);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        Toast.makeText(getActivity(),position+"",Toast.LENGTH_SHORT).show();
    }
});