2017-12-13 6 views
0

Ich bekomme JSON-Objekte und parse sie mit Retrofit2. Projekt hat 2 Teile und dieser Teil ist einer von ihnen. In diesem Teil habe ich viele Restaurants und jedes Restaurant hat Menüs. Menüs haben Sorten (Pizza, Burger ...). Diese Sorten (Pizza, ..) haben Namen und Preise (wie große italienische Pizza-> $ 10, kleine italienische Pizza-> $ 7). Nun, was ist mein Problem. Klicken Sie auf ein Restaurant, wird das Menü dieses Restaurants angezeigt. Menü haben Titel (Sorten). Auch Lebensmittel und Preise sind unter diesem Titel. Das ist alles, aber Zeilen ändern sich für jedes Restaurant und Menü. Restorant Teil ist okey. Nach einem Klick auf ein Restaurant, bekomme ich Daten (Menü) und sende ein Fragment. Ich bekomme Daten mit setData() in MenuFragment.java. Zum Beispiel, menus.get (0) .name ist Pizza Sorten, menus.get (1) .name ist Burger Sorten in diesem Restorant. Und menus.get (0) .foods umfasst Pizza Sorten (große italienische-Preis, kleine etc.) Auch Lebensmittel sind Objekt. Mein Verstand ist in diesem Teil verwirrt. Ich zeige Menü, aber Menü hat Listenansicht. Diese Listenansicht hat einen Titel (Textansicht) und eine weitere Listenansicht (Lebensmittel, Preis). Wie kann ich listview und texview in listview tun?(Textansicht und Listview) in Listview für Android

Es ist MenuFragment.java. Wir können die Hauptaktivität sagen.

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; 
} 
} 

Es ist CustomAdapterMenu.java (Es Strukturproblem hat.)

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(); 
} 
} 

Dies ist RowItemMenu.java (Es hat Strukturproblem)

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; 
} 
} 

Das ist mein Haupt Layout. Ich werde list_row_menu.xml für jede Zeile anzeigen. Erste Reihe zeigt Titel (wie Pizza-Sorten) und Inhalt (wie food_name und food_price, kann es 10 oder mehr verschiedene Lebensmittel enthalten)

<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> 

Dies ist list_row_menu.xml und es zeigt menu_name (wie Pizza-Sorten) und menu_list . menu_list zeigt für jede Zeile den Namen food_name und food_price an. Das heißt, es ist 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> 

Dies ist list_row_menu_food.xml und es zeigt food_name (wie italienische Pizza) und food_price für eine Zeile.

<?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> 
+0

Verwendung ExpendableListview.this kann Ihnen helfen https://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ –

+0

Vielen Dank. Wenn ich es nicht lösen kann, kann ich es mit expendablistview ändern. – ozo

+0

Lösung ist ListView mit Abschnitt Header in Android. Sie können im Internet suchen. – ozo

Antwort

0

Die Lösung ist ListView mit Section Header in Android. Sie können im Internet suchen.