Search code examples
androidlistviewlistviewitem

(Textview and Listview) in Listview for android


I get json objects and parse them with retrofit2. Project has 2 parts and this part is one of them. In this part, I have a lot of restaurants and each restaurant has menus. Menus have varieties(pizzas, burgers...). These varieties(pizzas,..) have foods name and prices(like big italian pizza-> $10, small italian pizza-> $7). Now, what is my problem. If people click on a restaurant, this restaurant's menu will be displayed. Menu have titles(varieties). Also, foods and prices are under this title. That's all but rows change for each restaurant and menu. Restorant part is okey. After click on a restaurant, I get data(menu) and send to fragment. I get data with setData() in MenuFragment.java. For example, menus.get(0).name is pizza varieties, menus.get(1).name is burger varieties in this restorant. And menus.get(0).foods includes pizza varieties(big italian-price, small etc.) Also, foods is object. My mind is confused in this part. I will show menu but menu has listview. This listview has title(textview) and another listview(foods,price). How can I do listview and texview in listview.

It is MenuFragment.java. We can say mainactivity.

public class MenuFragment extends Fragment
{
public List<Restaurant.Menu> menus = new ArrayList<>();

public List<RowItemMenu> rowItemMenus;
public ListView mylistview;

public List<String> name = new ArrayList<String>();
public List<Object> foods = new ArrayList<Object>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_rest, container, false);

    //Toast.makeText(getActivity(), "This! " + menus.get(0).name , Toast.LENGTH_LONG).show();

    mylistview = (ListView) view.findViewById(R.id.rest_list);
    CustomAdapterMenu adapter = new CustomAdapterMenu(getActivity(), rowItemMenus);

    for(int i=0; i<menus.size(); i++)
    {
        name.add(menus.get(i).name);
        foods.add(menus.get(i).foods);


        RowItemMenu item = new RowItemMenu(name.get(i), foods.get(i));
        rowItemMenus.add(item);
    }
    adapter.setData(rowItemMenus);
    mylistview.setAdapter(adapter);

    return view;
}

public void setData(List<Restaurant.Menu> menus)
{
    this.menus = menus;
}
}

It is CustomAdapterMenu.java (It has structure problem.)

public class CustomAdapterMenu extends BaseAdapter
{
Context context;
List<RowItemMenu> rowItemMenus;

List<RowItemMenu> data;

public ListView mylistview;

public CustomAdapterMenu(List<RowItemMenu> rowItemMenus)
{
    this.rowItemMenus = rowItemMenus;
}

public CustomAdapterMenu(Context context, List<RowItemMenu> rowItemMenus)
{
    this.context = context;
    this.rowItemMenus = rowItemMenus;
}

@Override
public int getCount()
{
    return rowItemMenus.size();
}

@Override
public Object getItem(int position)
{
    return rowItemMenus.get(position);
}

@Override
public long getItemId(int position)
{
    return rowItemMenus.indexOf(getItem(position));
}

/* private view holder class */
private class ViewHolder
{
    TextView name;
    TextView foodName;
    TextView foodPrice;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
    {
        convertView = mInflater.inflate(R.layout.list_row_menu, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.name = (TextView) convertView.findViewById(R.id.menu_name);
    holder.foodName = (TextView) convertView.findViewById(R.id.food_name);
    holder.foodPrice = (TextView) convertView.findViewById(R.id.food_price);

    RowItemMenu row_pos = rowItemMenus.get(position);

    holder.name.setText(row_pos.getName());
    holder.foodName.setText(row_pos.getFoodName());
    holder.foodPrice.setText(row_pos.getFoodPrice());

    return convertView;
}

public void setData(List<RowItemMenu> data)
{
    this.data=data;
    notifyDataSetChanged();
}
}

This is RowItemMenu.java (It has structure problem)

public class RowItemMenu
{
private String name;
//private String foodName;
//private String foodPrice;
public List<Object> foods = new ArrayList<Object>();

public RowItemMenu(String name, List<Object> foods)
{
    this.name = name;
    this.foods = foods;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<Object> getFoods() {
    return foods;
}

public void setFoods(List<Object> foods) {
    this.foods = foods;
}
}

This is my main layout. I will show list_row_menu.xml for each row. First row will show title(like pizza varieties) and content(like food_name and food_price, it can contain 10 or more different foods)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="oz.ncclife.fragments.RestFragment">

<ListView
    android:id="@+id/rest_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:dividerHeight="10dp"/>

</FrameLayout>

This is list_row_menu.xml and it shows menu_name(like pizza varieties) and menu_list. menu_list will show different food_name and food_price for each row. That is, it is list_row_menu_food.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" >

<TextView
    android:id="@+id/menu_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="25dp"
    android:textStyle = "bold"
    android:paddingTop="10dp"/>

<ListView
    android:id="@+id/menu_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:dividerHeight="10dp"/>

</LinearLayout>

This is list_row_menu_food.xml and it shows food_name(like italian pizza) and food_price for one row.

<?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="horizontal"
android:weightSum="1">

<TextView
    android:id="@+id/food_name"
    android:layout_width="0dip"
    android:layout_weight="0.8"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:textStyle = "bold"
    android:paddingTop="10dp" />

<TextView
    android:id="@+id/food_price"
    android:layout_width="0dip"
    android:layout_weight="0.2"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:textSize="25dp"
    android:textStyle="bold" />

</LinearLayout>

Solution

  • Solution is ListView with Section Header in Android. You can search on the internet.