2017-12-13 38 views
0

jsonオブジェクトを取得し、retrofit2で解析します。プロジェクトには2つの部分があり、この部分がその一部です。この部分では、たくさんのレストランがあり、各レストランにメニューがあります。メニューには品種(ピザ、ハンバーガーなど)があります。これらの品種(ピザ、..)には、食べ物の名前と価格(大きなイタリアンピザ - > 10ドル、小さなイタリアンピザ - > 7ドルなど)があります。 今、私の問題は何ですか。人々がレストランをクリックすると、このレストランのメニューが表示されます。メニューにはタイトル(品種)があります。また、食品と価格はこのタイトルの下にあります。それはレストランとメニューごとに行が変わるだけです。修復部分はokeyです。レストランをクリックすると、データ(メニュー)が表示され、フラグメントに送信されます。 MenuFragment.javaでsetData()を使ってデータを取得します。たとえば、menus.get(0).nameはピザの品種、menus.get(1).nameはこの修復剤のバーガーの品種です。そしてmenus.get(0).foodsにはピザの品種(大きなイタリアン価格、小さいものなど)が含まれています。また、食品は対象です。 私の心はこの部分で混乱しています。私はメニューを表示しますが、メニューにはリストビューがあります。このリストビューにはタイトル(テキストビュー)と別のリストビュー(食品、価格)があります。 listviewでlistviewとtexviewをどうすればいいですか?(TextviewとListview)androidのリストビュー

MenuFragment.javaです。 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; 
} 
} 

それはCustomAdapterMenu.java(これは、構造上の問題があります。)

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

これは、これは私のメインのレイアウトであるRowItemMenu.java(これは、構造上の問題がある)

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

です。各行に対してlist_row_menu.xmlを表示します。最初の行は、これはlist_row_menu.xmlであり、それはmenu_name(ピザの品種のような)とmenu_listを示し

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

(food_nameとfood_priceのように、それは10の以上の異なる食品を含むことができます)(ピザの品種など)のタイトルと内容が表示されます。 menu_listには、各行ごとに異なるfood_nameとfood_priceが表示されます。すなわち、これはlist_row_menu_food.xmlであり、それは1行分のfood_name(イタリアのピザなど)food_priceを示す

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

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

ExpendableListviewを使用してください。https://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ –

+0

ありがとうございます。私がそれを解決できない場合、私はexpendablelistviewでそれを変更することができます。 – ozo

+0

解決策はAndroidのセクションヘッダーを含むListViewです。インターネットで検索することができます。 – ozo

答えて

0

解決策は、Androidのセクションヘッダー付きリストビューです。インターネットで検索することができます。

関連する問題