有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从自定义ExpandableList适配器类中获取值,并将其设置到UI组件的前一个片段中

我有两个类,一个是片段类,另一个是定制的expandablelistadapter类。我希望从expandablelistadapter类中获取值,并将值设置到片段UI组件中,如设置到textview中,我在下面的类中描述为注释,从何处获取值 以及在哪里设置值

SlideContentFragment。java

public class SlidingContentFragment extends Fragment {
    static final String LOG_TAG = "SlidingContentFragment";
// store category list from Conastant list, used for to display pagetitle
List<String> catList = AppConstants.CATEGORY_LIST;

private static String[] tmpId  = {"35","36","41","42","43","44","45","46"};

ExpandableListView exListCategory;


private SlidingTabLayout mSlidingTabLayout;


/**
 * A {@link 安卓.support.v4.view.ViewPager} which will be used in conjunction with the {@link SlidingTabLayout} above.
 */
private ViewPager mViewPager;
//private ExpandableListAdapter mAdapter; // added new
private SamplePagerAdapter myAdapter;

public SlidingContentFragment() {
}

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

    ImageButton cartImgBtn = (ImageButton)v.findViewById(R.id.imgBtnCart);
    TextView totalCntItem = (TextView)v.findViewById(R.id.tvCartItemCount);

    // I want here set the value of totalCounter from ExpandableList Adapter class when i click on plus
    // OR minus button of particular item it updates its value as total count of all item selected

    // here following line of code which not worked
    totalCntItem.setText(String.format("%d",CategoryItemListAdapter.getTotalCounter()));

    return v;
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // BEGIN_INCLUDE (setup_viewpager)
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    this.myAdapter = new SamplePagerAdapter(); // added new
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
    mViewPager.setAdapter(this.myAdapter);
    // END_INCLUDE (setup_viewpager)



    // BEGIN_INCLUDE (setup_slidingtablayout)
    // Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
    // it's PagerAdapter set.
    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setViewPager(mViewPager);

}

public class SamplePagerAdapter extends PagerAdapter {
    public SamplePagerAdapter() {
        super();
    }

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

    @Override
    public boolean isViewFromObject(View view, Object o){
        return o == view;
    }


    @Override
    public CharSequence getPageTitle(int position) {
       return catList.get(position);
    }


    @Override
    public int getItemPosition (Object object)
    {
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        //return super.instantiateItem(container, position);
        ViewPager viewPager = (ViewPager)container;
        View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
                container, false);
        viewPager.addView(view);

        exListCategory = (ExpandableListView)view.findViewById(R.id.myExpandableListView);
        //exListCategory.setIndicatorBounds(10,20);
        exListCategory.setDividerHeight(2);

        if(ConnectionDetector.isInternetAvailable(getActivity())) {
            new CategoryJSONAsyncTask().execute("http://..../api/Main/GetCateenOrderCategoryItemListDetail?CategoryID=" + tmpId[position].trim());
        }else{
            Utility.buildDialog(getActivity()).show();
        }

        Log.i(String.format("%s: POSITION", LOG_TAG), String.valueOf(position));
        Log.i(String.format("%s: CATLIST", LOG_TAG),String.valueOf(catList.get(position)));

        view.setTag(position);

        return view;

    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //viewpager
        ViewPager viewPager = (ViewPager)container;
        View view = (View) object;
        view.getTag(position);
        viewPager.removeView(view);
        //((ViewPager) container).removeView((View) object);
    }
}


public class CategoryJSONAsyncTask  extends AsyncTask<String,Void,String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        try {
            HttpGet httppost = new HttpGet(params[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity);

                return result;
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

        @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
            ArrayList<CategoryParentItemList> listParent = fetchResponse(result.replace("\n","").trim());
            /*for (Object obj : listParent){
                if(obj.getClass() == CategoryParentItemList.class){
                    CategoryParentItemList p = (CategoryParentItemList)obj;
                    System.out.println("P-ItemName: "+ p.subCategoryName);
                }
            }*/
            CategoryItemListAdapter adapter = new CategoryItemListAdapter(SlidingContentFragment.this.getActivity(), listParent);
            exListCategory.setAdapter(adapter);
            adapter.notifyDataSetChanged();

    }
}

public ArrayList<CategoryParentItemList> fetchResponse(String result)
{
    ArrayList<CategoryParentItemList> listParent = new ArrayList<>();
    if (!result.equals(""))
    {
        try
        {
            JSONObject jsono = new JSONObject(result);
            JSONArray jarray = jsono.getJSONArray("SBL");

            CategoryParentItemList parent;

            for (int i = 0; i < jarray.length(); i++)
            {
                ArrayList<CategoryChildListItem> childrens = new ArrayList<>();
                childrens.clear();
                CategoryChildListItem child;

                JSONObject object = jarray.getJSONObject(i);
                //System.out.println("SCI: " + object.getInt("SubCategoryID"));
                //System.out.println("SCN: " + object.getString("SubCategoryName"));

                JSONArray subItemArray = object.getJSONArray("SubCategoryItemList");

                if (subItemArray.length() > 0)
                {
                    for (int j = 0; j < subItemArray.length(); j++)
                    {
                        JSONObject subItemObject = subItemArray.getJSONObject(j);
                        String strItemName = subItemObject.getString("ItemName");
                        String strDefaultPrice = subItemObject.getString("DefaultPrice");

                        child = new CategoryChildListItem(strItemName, strDefaultPrice);
                        childrens.add(child);

                        //Log.i("strItemName", strItemName);
                        //Log.i("strDefaultPrice", strDefaultPrice);
                    }
                    parent = new CategoryParentItemList(object.getString("SubCategoryName"),childrens);
                    listParent.add(parent);
                }
            }
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    return listParent;
}
}

类别匹配器。java

public class CategoryItemListAdapter extends BaseExpandableListAdapter{
    private Context context;
    public static int totalCounter=0;

private ArrayList<CategoryParentItemList> listParent;

static class ViewHolderGroup {
    public TextView lblSubCategoryName;
}

static class ViewHolderChild {
    public TextView lblItemName;
    public TextView lblDefualtPrice;
    public TextView lblQty;
    public ImageButton imgPlus;
    public ImageButton imgMinus;
}


public CategoryItemListAdapter(Context context, ArrayList<CategoryParentItemList> listParent) {
    super();
    this.context = context;
    this.listParent = listParent;
}

@Override
public int getGroupCount() {

    return listParent.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<CategoryChildListItem> ch = listParent.get(groupPosition).getChildList();
    return ch.size();
}

@Override
public Object getGroup(int groupPosition) {

    return listParent.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<CategoryChildListItem> ch = listParent.get(groupPosition).getChildList();
    return ch.get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {

    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {

    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    //CategoryParentItemList parentItem = (CategoryParentItemList)listParent.get(groupPosition);
    CategoryParentItemList parentItem = (CategoryParentItemList) getGroup(groupPosition);
    ViewHolderGroup holderGroup;

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.group_header, null);
        holderGroup = new ViewHolderGroup();
        holderGroup.lblSubCategoryName = (TextView) convertView.findViewById(R.id.tvItemName);
        convertView.setTag(holderGroup);
    } else {
        holderGroup = (ViewHolderGroup) convertView.getTag();
    }

    holderGroup.lblSubCategoryName.setText(parentItem.getSubCategoryName());
    return convertView;
}

@Override
public View getChildView(int groupPosition,int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    //final CategoryParentItemList parentItem = (CategoryParentItemList) listParent.get(groupPosition);
    //final CategoryChildListItem childItem = (CategoryChildListItem) parentItem.getChildList().get(childPosition);
    CategoryChildListItem childItem = (CategoryChildListItem) getChild(groupPosition, childPosition);

    ViewHolderChild holder;


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_row, null);

        holder = new ViewHolderChild();

        holder.lblItemName = (TextView) convertView.findViewById(R.id.tvSubItemName);
        holder.lblDefualtPrice = (TextView) convertView.findViewById(R.id.tvrRupees);
        holder.lblQty = (TextView) convertView.findViewById(R.id.tvQty);
        holder.imgPlus = (ImageButton) convertView.findViewById(R.id.imageButtonPlus);
        holder.imgMinus = (ImageButton) convertView.findViewById(R.id.imageButtonMinus);



        convertView.setTag(holder);
    } else {
        holder = (ViewHolderChild) convertView.getTag();

    }

    holder.lblItemName.setText(childItem.getSubItemName());
    holder.lblDefualtPrice.setText(childItem.getDefaultPrice());


    int tmpCount = Integer.parseInt(holder.lblQty.getText().toString());
    holder.imgPlus.setOnClickListener(new ClickUpdateListener(childItem,holder, tmpCount));
    holder.imgMinus.setOnClickListener(new ClickUpdateListener(childItem,holder, tmpCount));

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

@Override
public boolean areAllItemsEnabled() {
    return true;
}

public static int getTotalCounter() {
    return totalCounter;
}

private class ClickUpdateListener implements View.OnClickListener {

    ViewHolderChild holder;
    public CategoryChildListItem childItem;


    int counter = 0;
    String  counterMin;

    public ClickUpdateListener(CategoryChildListItem childItem,ViewHolderChild holder, int cnt) {
        this.childItem = childItem;
        this.holder = holder;
        this.counter = cnt;

    }


    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.imageButtonPlus) {
            counter = counter + 1;
            totalCounter+=1;

            System.out.println(childItem.getSubItemName()+" : "+childItem.getDefaultPrice() + ": C+ :" + counter);

            holder.lblQty.setText(String.format("%d", counter));

            notifyDataSetChanged();

        }
        if(v.getId() == R.id.imageButtonMinus){
            counterMin = (String) holder.lblQty.getText();
            counter = Integer.parseInt(counterMin.toString().trim());
            counterMin = null;
            if(counter > 0) {
                counter = counter - 1;
                totalCounter-=1;

                System.out.println(childItem.getSubItemName()+" : "+childItem.getDefaultPrice() + ": C- :" + counter);

                holder.lblQty.setText(String.format("%d", counter));

                notifyDataSetChanged();
            }else{
                Toast.makeText(context,"Qty Zero",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

共 (0) 个答案