繁体   English   中英

如何从自定义ExpandableList Adapter类获取值并将其设置为UI组件的先前片段

How to fetch the value from custom ExpandableList Adapter class and set into previous fragment of UI component

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我有以下两个类,一个是片段类,另一个是自定义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 android.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;
}
}

CategoryListItemAdaptor.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();
                }
            }
        }
    }
}
问题暂未有回复.您可以查看右边的相关问题.
5 Antd Forms,从自定义组件获取值?

我正在尝试在getFieldDecorator添加一些自定义组件,并获取添加的onCreate值。 由于状态在“自定义”组件中找到,因此不确定如何处理。 理想情况下,自定义组件将处理所有用户输入值,但不确定如何将这些值作为对象onCreate的一部分传递。 ...

6 通过Hashmap获取值并将其显示在自定义列表视图中

我正在设计一个应用程序,其中使用自定义适配器,并且使用哈希映射值, 我想这样显示我的文本: 模式 :振铃模式粗体显示,但振铃模式为普通文本。 我可以得到想要的文本。 我把两个值都使用不同的哈希键和不同的文本视图,但是我得到的文本像。 :铃声模式 这是我正在使用的一些代码, ...

7 包装npm的组件并将其导出为自定义组件

从npm安装第3方组件时出现问题。 例如,有一个下拉反应模块,我可以在我的模块中轻松使用它。 但是我必须在我的许多模块中声明其样式和该组件的其他依赖关系。 喜欢这个组件 我必须在每个模块中注入其风格 如何解决这个问题呢? ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM