2017-10-16 93 views
0

嗨,我正在android應用程序。我在我的應用程序中使用json解析。首先這是我的JSON響應如何訪問arraylist在適配器

{ 
    "Data": { 
    "headerText": "Revolution", 
    "productStartingPrice": "$38.78", 

    "sizeList": [ 
     { 
     "textUpper": "Revolution for (Pink)", 
     "textLower": "$38.78", 
     "packList": [ 
      { 
      "textUpper": "3 DOSES", 
      "textLower": "$38.78", 

      } 
      { 
      "textUpper": "6 DOSES", 
      "textLower": "$71.60", 

      } 
     ] 
     }, 
     { 
     "textUpper": "Revolution for (Blue)", 
     "textLower": "$46.92", 
     "packList": [ 
      { 
      "textUpper": "3 DOSES", 
      "textLower": "$46.92", 
      } 
      { 
      "textUpper": "6 DOSES", 
      "textLower": "$79.56", 


      } 
      { 
      "textUpper": "12 DOSES", 
      "textLower": "$137.27", 

      } 
     ] 
     } 
    ] 

    }, 
    "Status": 1, 
    "Message": "", 
    "UserMessage": "" 
} 

現在我使用自定義警告對話框加載SizeList的陣列和另一個dailog爲packlist陣列。現在我想要的是,當用戶從sizelist中選擇(粉紅色),然後在第二個dailog中,我想要顯示來自packlist的3個劑量和6個劑量,並且當用戶選擇(藍色)時,我想顯示3個劑量,6個劑量,12個劑量。現在我只有最後兩項(藍色)6劑,12劑。任何人都可以幫我解決這個問題嗎?

private void showDialog(){ 

     dialogf = new Dialog(getActivity()); 

     View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_main, null); 

     ListView lv = (ListView) view.findViewById(R.id.custom_list); 
     Button close_btn = (Button) view.findViewById(R.id.close_btn_sizelist); 
     sizeadapter = new CustomListAdapterSize(getActivity(), sizelists); 
     lv.setAdapter(sizeadapter); 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      } 
     }); 

     dialogf.setContentView(view); 

     dialogf.show(); 
     close_btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialogf.dismiss(); 
      } 
     }); 


    } 

    private void showDialogpacks(){ 

     dialogfpacks = new Dialog(getActivity()); 

     View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_main_packs, null); 
     ListView lv = (ListView) view.findViewById(R.id.custom_list_packs); 
     Button close_btn = (Button) view.findViewById(R.id.close_btn_packlist); 

     adapterpacks = new CustomListAdaptertwo(getActivity(), sizelists); 
     lv.setAdapter(adapterpacks); 

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      } 
     }); 

     dialogfpacks.setContentView(view); 
     dialogfpacks.show(); 



    } 
public class CustomListAdapterSize extends BaseAdapter { 
     private Context context; 
     private LayoutInflater inflater; 
     private List<ProductLandingSizeListModel> adaptersizelist; 
     private CheckBox selected =null; 
     private RadioButton mSelectedRB; 
     public CustomListAdapterSize(Context context,List<ProductLandingSizeListModel> movieItems) { 
      this.context = context; 
      this.adaptersizelist=movieItems; 
     } 

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

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

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      final ViewHolder holder; 
      if (convertView == null) { 
       holder = new ViewHolder(); 
       convertView = LayoutInflater.from(context).inflate(R.layout.listitem_productsizes, null); 
       holder.txtproname = (TextView) convertView.findViewById(R.id.txtproductsizenames); 
       holder.txtproprice = (TextView) convertView.findViewById(R.id.txtproductsizeprice); 
       holder.radioBtn = (CheckBox)convertView.findViewById(R.id.productsize_radio); 
       convertView.setTag(holder); 
      }else{ 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      final ProductLandingSizeListModel m = adaptersizelist.get(position); 
      holder.txtproname.setText(m.getSizeList_textUpper()); 
      holder.txtproprice.setText(m.getSizeList_textLower()); 

      if (position == mSelectedPosition) { 
       if (selected == null) { 
        holder.radioBtn.setChecked(true); 
        selected = holder.radioBtn; 
       } 
      } 
      if (position == mSelectedPosition) 
       holder.radioBtn.setChecked(true); 
      else 
       holder.radioBtn.setChecked(false); 

      holder.radioBtn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        if (selected != null) { 
         selected.setChecked(false); 
        } 
        mSelectedPosition =position; 
        if(mSelectedPositionpack!=0) 
        { 
         mSelectedPositionpack=0; 
        } 
        holder.radioBtn.setChecked(true); 
        selected = holder.radioBtn; 

        notifyDataSetChanged(); 

        dialogf.dismiss(); 
        showDialogpacks(); 
       } 
      }); 

      return convertView; 
     } 
     class ViewHolder{ 

      TextView txtproname,txtproprice; 
      public CheckBox radioBtn; 
     } 

    } 

    public class CustomListAdaptertwo extends BaseAdapter { 


     private CheckBox selected =null; 
     private Context context; 
     private LayoutInflater inflater; 
     private List<ProductLandingSizeListModel> adaptersizelist; 


     public CustomListAdaptertwo(Context context,List<ProductLandingSizeListModel> adaptersizelist) { 
      this.context = context; 
      this.adaptersizelist=adaptersizelist; 
      inflater = LayoutInflater.from(context); 
     } 

     @Override 
     public int getCount() { 
      if (adaptersizelist != null) { 
       return adaptersizelist.size(); 
      } 
      return 0; 
     } 

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

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      final ViewHolder holder; 
      if (convertView == null) { 
       holder = new ViewHolder(); 
       convertView = inflater.inflate(R.layout.custom_spinnertwo, null); 
       holder.txtproname = (TextView) convertView.findViewById(R.id.text_main_seentwo); 
       holder.txtproprice = (TextView) convertView.findViewById(R.id.sub_text_seentwo); 
       holder.checkpack = (CheckBox)convertView.findViewById(R.id.productpack_radio); 
       convertView.setTag(holder); 
      }else{ 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      final ProductLandingSizeListModel m = adaptersizelist.get(position); 

      for(int i=0;i<adaptersizelist.get(position).getPacklistmodel().size();i++) 
      { 
       System.out.println("PACK na data"+adaptersizelist.get(position).getPacklistmodel().get(i).getPackList_textUpper()); 

       holder.txtproname.setText(adaptersizelist.get(position).getPacklistmodel().get(i).getPackList_textUpper()); 
       holder.txtproprice.setText(adaptersizelist.get(position).getPacklistmodel().get(i).getPackList_textLower()); 

      } 


      if (position == mSelectedPositionpack) { 
       if (selected == null) { 
        holder.checkpack.setChecked(true); 
        selected = holder.checkpack; 
       } 
      } 
      if (position == mSelectedPositionpack) 
       holder.checkpack.setChecked(true); 
      else 
       holder.checkpack.setChecked(false); 

      holder.checkpack.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        if (selected != null) { 
         selected.setChecked(false); 
        } 
        mSelectedPositionpack =position; 
        holder.checkpack.setChecked(true); 
        selected = holder.checkpack; 
        // selected_pack_text.setText(movieItems.get(position)+" - "+"$"+packItems.get(position)); 
        notifyDataSetChanged(); 



        dialogfpacks.dismiss(); 
       } 
      }); 
      return convertView; 
     } 
     class ViewHolder{ 

      TextView txtproname,txtproprice; 
      CheckBox checkpack; 
     } 

    } 
+0

你需要有packlist類型的數組列表中你SizeList的模式類的內部 –

+0

是的,我已經 – chris

+0

@VivekMishra你能幫 – chris

回答

1

更改您的showDialogpacks像第一個對話框的this.On點擊傳球被點擊的位置,然後在裏面通過只有項目 裏面sizelists特定的位置。

private void showDialogpacks(int position){ 

    dialogfpacks = new Dialog(getActivity()); 

    View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_main_packs, null); 
    ListView lv = (ListView) view.findViewById(R.id.custom_list_packs); 
    Button close_btn = (Button) view.findViewById(R.id.close_btn_packlist); 

    adapterpacks = new CustomListAdaptertwo(getActivity(), sizelists.get(position).getPacklistmodel()); 
    lv.setAdapter(adapterpacks); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     } 
    }); 

    dialogfpacks.setContentView(view); 
    dialogfpacks.show(); 



} 

還將第二個適配器列表類型更改爲ProductLandingPackListModel

public CustomListAdaptertwo(Context context,List<ProductLandingPackListModel> adaptersizelist) { 
     this.context = context; 
     this.adaptersizelist=adaptersizelist; 
     inflater = LayoutInflater.from(context); 
    } 

而在CustomAdapdtertwo getView方法

final ProductLandingPackListModel model = adaptersizelist.get(position); 

    holder.txtproname.setText(model.getPackList_textUpper()); 
    holder.txtproprice.setText(model.getPackList_textLower()); 
+0

它給出的錯誤(getActivity(),sizelists.get(position));這裏。在第二個適配器,我需要改變? – chris

+0

兄弟問題是在這裏第二個適配器得到count.You需要傳遞單個內部數組項目。通過sizelists.get(position).packList。 – Anonymous

+0

我duno你的方法名..你需要像那樣傳遞,也改變arrayList裏面的第二個適配器。它應該是List adaptersizelist – Anonymous

0

嗨克里斯基於尺寸的選擇集packlist ......各種規模用戶的更新包列表中選擇

+0

好的告訴我該怎麼做?請檢查代碼。 – chris

+0

你的第二個適配器應該是ArrayList類型,並且在點擊下面SizeList的showDialogPacks(ArrayList的 packList)設置一樣,將其設置爲recyclerview – Shanmugam

+0

我想,太,,但其NT給予適當輸出 – chris

相關問題