问题
Hi i am making android application. I am using json parsing in my application. first of all this is my json response
{
"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": ""
}
Now I am using custom alert dialog to load sizelist array and another dailog for packlist array. Now what i want is when user select (Pink) from sizelist then in second dailog i want to display 3 doses and 6 doses from packlist and when user select (Blue) i want to display 3 doses, 6 doses ,12 doses. Right now i am getting only last two items of (Blue) 6 doses ,12 doses. can any one help me to solve this?
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;
}
}
回答1:
Change your showDialogpacks like this.On click of the first dialog pass the position that is been clicked and then inside that pass only the item inside sizelists for particular position.
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();
}
Also change the second adapter list type to ProductLandingPackListModel
.
public CustomListAdaptertwo(Context context,List<ProductLandingPackListModel> adaptersizelist) {
this.context = context;
this.adaptersizelist=adaptersizelist;
inflater = LayoutInflater.from(context);
}
And in the inside of the CustomAdapdtertwo getView method
final ProductLandingPackListModel model = adaptersizelist.get(position);
holder.txtproname.setText(model.getPackList_textUpper());
holder.txtproprice.setText(model.getPackList_textLower());
回答2:
Hi Chris based upon size selection set packlist...update pack list of every size user selects
来源:https://stackoverflow.com/questions/46764155/how-to-access-arraylist-of-arraylist-in-adapters