使用全局ArrayList的模型类后如何设置值?

时间:2015-07-16 10:23:47

标签: android arraylist textview

我创建了一个模型类来存储我的产品名称,价格和数量,然后将其附加到另一个Fragment并尝试在我的TextView中显示。不幸的是{I} ArrayList我无法获得商品的位置。以下是我的代码段。我需要帮助。

Modelclass Arr_Model.Java

public class Arr_Model {

    private String BRAND_NAME;
    private String BRNAD_PRICE;
    private String QTY ;

    public Arr_Model(){}

    public String getBRAND_NAME() {
        return BRAND_NAME;
    }

    public String getQTY() {
        return QTY;
    }

    public void setBRAND_NAME(String BRAND_NAME) {
        this.BRAND_NAME = BRAND_NAME;
    }

    public void setBRNAD_PRICE(String BRNAD_PRICE) {
        this.BRNAD_PRICE = BRNAD_PRICE;
    }

    public void setQTY(String QTY) {
        this.QTY = QTY;
    }

    public String getBRNAD_PRICE() {
        return BRNAD_PRICE;
    }
}

这里我从响应中获取值并将其存储在arraylist

ProductDetail.java

1)首先我从响应中获取数据,它是产品名称及其价格

类LoadAlbums扩展了AsyncTask>> {     / **      *在开始后台线程显示进度对话框之前      * * /

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(All_Product_Details.this.getActivity());
    pDialog.setMessage("Loading...");
    pDialog.setIndeterminate(true);
    pDialog.setCancelable(false);
    pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {

    ServiceHandler sh = new ServiceHandler();
    String jsonStr = sh.makeServiceCall(USER_URL, ServiceHandler.GET);
    Log.d("Response: ", "> " + jsonStr);

    if (jsonStr != null) {
        try {


                JSONObject jsonObj = new JSONObject(jsonStr);
                JSONObject pname = jsonObj.getJSONObject(PRODUCT_MAIN);
                System.out.println("Jsonobject product>>" + pname);
                JSONArray sizes = new JSONArray();
                sizes = pname.getJSONArray("attributes");
                for (int j = 0; j < sizes.length(); j++) {
                    JSONObject jsonnewtwo = sizes.getJSONObject(j);
                    option = jsonnewtwo.getJSONArray("options");
                    System.out.println("Option---" + option);

                }
                System.out.println("Attribute size" + sizes);
                user_name = pname.getString(USER_NAME);
                prod_price = pname.getString(PRODUCT_PRICE);
                prod_desc = pname.getString(PRODUCT_DESC);


        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }
    return null;
}

protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
    super.onPostExecute(result);

    if (pDialog.isShowing())
        pDialog.dismiss();
    if(user_name.equals(""))
    {
        uname.setText("Not Available");
    }
    else
    {
        uname.setText(user_name);
    }

    if(prod_price.equals(""))
    {
        pprice.setText("Not Available");
    }
    else
    {
        pprice.setText(dollars+prod_price);
    }

    if(prod_desc.equals(""))
    {
        general_desc.setText("Not Available");
    }
    else
    {
        general_desc.setText(Html.fromHtml(prod_desc).toString());
    }

2)然后我增加产品数量并点击添加到购物车按钮,点击添加到购物车我在arraylist中保存数据..

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


                    cartincrement();
                    productnams = GenericUtility.getStringFromSharedPrefsForKey("selected_prodname", getActivity());
                    //System.out.println("Seleced user name=-=-=-=-=-=-==-=" + productnams);
                    productprc = GenericUtility.getStringFromSharedPrefsForKey("selected_prodprc", getActivity());
                    //System.out.println("Seleced prodprice=-=-=-=-=-=-==-=" + productprc);
                    Arr_Model product = new Arr_Model();
                    product.setBRAND_NAME(user_name);
                    product.setBRNAD_PRICE(prod_price);
                    product.setQTY(_value.getText().toString());
                    productArraylist.add(product);

                    System.out.println("TEst--" + productArraylist.get(0).getBRAND_NAME());
                }
            });
            //System.out.println("Test--" + productArraylist.get(0).getBRAND_NAME());

            mBTIncrement.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    increment();

                }
            });

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

                    decrement();
                }
            });

3)然后我点击购物车图标查看我添加了哪个项目及其数量

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

                        _value.setText("1");
                        Add_to_cart tf = new Add_to_cart();
                        android.support.v4.app.FragmentManager fm = getFragmentManager();
                        android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
                        ft.replace(R.id.frame_container, tf);
                        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                        ft.addToBackStack(null);
                        ft.commit();
                    }
                });

            }

4)现在最后一步显示listview中的项目到下一个片段

public class Add_to_cart extends Fragment {

    private Button continue_shopping;
    private Button checkout;
    ListView list;
    private TextView _decrease,mBTIncrement,_value;
    private CustomListAdapter adapter;
    private ArrayList<String> alst;
    private ArrayList<String> alstprc;
    private String bname;
    private ArrayList<Arr_Model> productArraylist;


    public Add_to_cart(){}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.list_view_addtocart, container, false);

       productArraylist = All_Product_Details.productArraylist;
        // alst=new ArrayList<String>();
       // Bundle bundle = this.getArguments();
       // alst = bundle.getStringArrayList("prodnam");
        //consts.arr = new ArrayList<String>();
        //consts.arr = alst;
       // System.out.println("NAM--" + productArraylist);

       // Bundle bundle1 = this.getArguments();
       // alstprc = bundle1.getStringArrayList("prodprc");




/*
        adapter = new CustomListAdapter(getActivity(),alst);
        list=(ListView)rootView.findViewById(R.id.list_addtocart);
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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


            }
        });*/


        return rootView;
    }

    public class CustomListAdapter extends BaseAdapter {

        private Context context;
        private ArrayList<Arr_Model> listData;
        //private AQuery aQuery;

        String dollars="\u0024";


        public CustomListAdapter(Context context,ArrayList<Arr_Model> listData) {
            this.context = context;
            this.listData=listData;
            // aQuery = new AQuery(this.context);
        }

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

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
                holder.txtproname = (TextView) convertView.findViewById(R.id.proname_addtocart);
                holder.txtprofilecast = (TextView) convertView.findViewById(R.id.proprice_addtocart);

                _decrease = (TextView)convertView.findViewById(R.id.minuss_addtocart);
                mBTIncrement = (TextView)convertView.findViewById(R.id.plus_addtocart);
                _value = (EditText)convertView.findViewById(R.id.edt_procount_addtocart);

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


            mBTIncrement.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    increment();
                }
            });

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

                    decrement();
                }
            });

           /* for(int i=0;i<productArraylist.size();i++)
            {
                holder.txtproname.setText(productArraylist.get(0));
            }

            holder.txtprofilecast.setText(dollars+alstprc.get(position));*/
            // aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

            return convertView;
        }
        class ViewHolder{
            ImageView propic;
            TextView txtproname;
            TextView txtprofilecast;

        }

3 个答案:

答案 0 :(得分:0)

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

        _value.setText("1");
        Add_to_cart tf = new Add_to_cart();

        tf.setList(productArraylist);

        android.support.v4.app.FragmentManager fm = getFragmentManager();
        android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.frame_container, tf);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }
});

在Fragment中添加以下方法。

public void setList(ArrayList<Arr_Model> p_productArraylist) {
       productArraylist = p_productArraylist;
}

答案 1 :(得分:0)

首先创建一个这样的应用程序类,

公共类Applicationc扩展了Application {

public String str;

@Override
public void onCreate() {
    super.onCreate();

    str=null;
}

}

在清单中创建这样的条目   

然后在onPost of Async Task中按以下方式设置值,

Applicationc app =(Applicationc)getApplicatoncotext(); app.str =&#34;数据&#34 ;;

您希望以下列方式检索价值,

Applicationc app =(Applicationc)getApplicatoncotext(); String dd = app.str;

答案 2 :(得分:0)

我无法真正遵循所有这些代码。如果我理解正确,那么您在ActivityAsyncTaskFragment之间传递数据时遇到问题。这里一个好的架构是让Activity跟踪数据。 Fragment始终可以向Activity询问数据,AsyncTask也可以回拨Activity来更新数据。

如果这还不足以解决您的问题,请准确说明您遇到问题的地方。