如何从baseAdapter类中刷新活动或片段中的片段

时间:2016-04-22 04:15:51

标签: android

我有一些问题。当我从listview baseAdapter中单击删除(interestAdepter类中的btnPlus.setOnClickListener)按钮时,我想刷新包含listview的片段。

1.InterestAdapter class {edtied}

    public class InterestAdapter extends BaseAdapter{
        private ArrayList<InterestClass>   m_List;

        public InterestAdapter() {
            m_List = new ArrayList<InterestClass>();
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final int pos = position;
            final Context context = parent.getContext();

            TextView        textName    = null;
            TextView        textDate    = null;
            TextView        textConPrice    = null;
            TextView        textNowPrice    = null;
            TextView        textFog    = null;
            ImageButton     btnPlus     = null;
            CustomHolder    holder  = null;

            if ( convertView == null ) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_interest, parent, false);

                textName        = (TextView) convertView.findViewById(R.id.i_ProdNmae);
                textDate        = (TextView) convertView.findViewById(R.id.i_Date);
                textConPrice    = (TextView) convertView.findViewById(R.id.i_InterPrice);
                textNowPrice    = (TextView) convertView.findViewById(R.id.i_currentPrice);
                textFog         = (TextView) convertView.findViewById(R.id.i_Analysis);
                btnPlus         = (ImageButton)   convertView.findViewById(R.id.i_addBtn);

                holder = new CustomHolder();
                holder.i_TextView    = textName;
                holder.i_TextView1   = textDate;
                holder.i_TextView2   = textConPrice;
                holder.i_TextView3   = textNowPrice;
                holder.i_TextView4   = textFog;
                holder.i_Btn         = btnPlus;
                convertView.setTag(holder);
            }
            else {
                holder          =  (CustomHolder) convertView.getTag();
                textName        =  holder.i_TextView;
                textDate        =  holder.i_TextView1;
                textConPrice    =  holder.i_TextView2;
                textNowPrice    =  holder.i_TextView3;
                textFog         =  holder.i_TextView4;
                btnPlus         =  holder.i_Btn;
            }

            /*if(position == 0) {
                textName.setText("종목");
                textDate.setText("관심일");
                textConPrice.setText("관심가");
                textNowPrice.setText("현재가");
                textFog.setText("수급");
                btnPlus.setEnabled(false);
            }else{
            }*/
                textName.setText(m_List.get(position).getName());
                textDate.setText(m_List.get(position).getDate());
                textConPrice.setText(m_List.get(position).getConPrice());
                textNowPrice.setText(m_List.get(position).getNowPrice());
                textFog.setText(m_List.get(position).getFog());
                btnPlus.setEnabled(true);

            textName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,DetailActivity.class);
                    context.startActivity(intent);
                }
            });

            btnPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Crypto enc = new Crypto();

                    SharedPreferences pref = context.getSharedPreferences("com.rabiaband.pref", 0);
                    String[] add_Params = new String[2];
                    try {
                        add_Params[0] = enc.AES_Encode(pref.getString("no", null), enc.global_deckey);
                        add_Params[1] = enc.AES_Encode(m_List.get(pos).getJmcode(), enc.global_deckey);
                    } catch (Exception e) {
                        Log.e("HomeCryptographError:", "" + e.getMessage());
                    }

                    AsyncCallAddDelFavorite delFavorite = new AsyncCallAddDelFavorite();
                    delFavorite.execute(add_Params);



                }
            });
    private class AsyncCallAddDelFavorite extends AsyncTask<String, Void, Void> {
        JSONObject del_Favorite_List;
        /* public AsyncCallAddFavorite(HomeFragment home){
             this.home=home;
         }*/
        @Override
        protected void onPreExecute() {
            /*Log.i(TAG, "onPreExecute");*/
        }

        @Override
        protected Void doInBackground(String... params) {
            dbConnection db_Cont=new dbConnection();
            String id=params[0];
            String jmcode=params[1];
            Log.v("doinbackgroud",""+id+"ssssss"+jmcode);
            del_Favorite_List=db_Cont.delFavorite(id, jmcode);
            Log.v("doinbackgroud",del_Favorite_List.toString());
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {

            /*Log.i(TAG, "onPostExecute");*/
        }
    }
}

2.InterestFragment {编辑}

    public class InterestFragment extends Fragment {
        private static final String ARG_PARAM1 = "param1";
        private static final String ARG_PARAM2 = "param2";

        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;
        private OnFragmentInteractionListener mListener;
        private ListView interest_ListView;
        private InterestAdapter interest_Adapter;
        String TAG="response";
        RbPreference keep_Login;

        public InterestFragment() {
            // Required empty public constructor
        }
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment InterestFragment.
         */
        // TODO: Rename and change types and number of parameters
        public static InterestFragment newInstance(String param1, String param2) {
            InterestFragment fragment = new InterestFragment();
            Bundle args = new Bundle();
            args.putString(ARG_PARAM1, param1);
            args.putString(ARG_PARAM2, param2);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {
                mParam1 = getArguments().getString(ARG_PARAM1);
                mParam2 = getArguments().getString(ARG_PARAM2);
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            keep_Login = new RbPreference(getContext());
            Log.v("interFrag","oncreate");
            // Inflate the layout for this fragment
            /*if(keep_Login.get("name",null)!=null) {
                return inflater.inflate(R.layout.fragment_interest, container, false);
            }else{
                return inflater.inflate(R.layout.need_login, container, false);
            }*/
            return inflater.inflate(R.layout.fragment_interest, container, false);
        }

        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.v("ListFragment", "onActivityCreated().");
            Log.v("ListsavedInstanceState", savedInstanceState == null ? "true" : "false");

            if(keep_Login.get("no",null)!=null) {

                String enc_Id="";
                Crypto enc=new Crypto();
                try{
                    enc_Id=enc.AES_Encode(keep_Login.get("no",null),enc.global_deckey);
                }catch(Exception e){

                }
                interest_Adapter = new InterestAdapter();
                interest_ListView = (ListView) getView().findViewById(R.id.i_ListView);

                AsyncCallInterest task = new AsyncCallInterest();
                task.execute(enc_Id);

            }
        }
  private class AsyncCallInterest extends AsyncTask<String, Void, JSONArray> {
        JSONArray interest_Array;
        InterestFragment interest;
        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected JSONArray doInBackground(String... params) {
            String id=params[0];
            dbConnection db_Cont=new dbConnection();
            interest_Array=db_Cont.getFavorite(id);
            Log.v("doinbackgroud", interest_Array.toString());
            return interest_Array;

        }

        @Override
        protected void onPostExecute(JSONArray result) {
            try{
               for (int i = 0 ; i < result.length() ; i ++){
                   InterestClass i_Class = new InterestClass();
                   String date=result.getJSONObject(i).getString("indate");
                   String time=date.substring(5,date.length());
                   i_Class.setJmcode(result.getJSONObject(i).getString("jmcode"));
                   i_Class.setName(result.getJSONObject(i).getString("jmname"));
                   i_Class.setDate(time);
                   i_Class.setConPrice(result.getJSONObject(i).getString("fprice"));
                   i_Class.setNowPrice(result.getJSONObject(i).getString("price"));
                   i_Class.setFog("40");
                   i_Class.setPlus(true);
                   interest_Adapter.add(i_Class);
                }
                interest_ListView.setAdapter(interest_Adapter);
            }catch(Exception e){
               Toast.makeText(getActivity(), "interest"+e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}

有没有办法从baseAdapter类中的按钮刷新片段?我搜索了很多,但我不能回答。请帮帮我,谢谢。 {}编辑 我添加了AsyncCallAddDelFavorite类和AsyncCallInterest类。谢谢。

2 个答案:

答案 0 :(得分:0)

如果我理解正确您想要从数据库中删除该项目之后从ListView删除该项目,那么您可以将其称为

m_List.remove(position);
interest_Adapter..notifyDataSetChanged();

第一行删除该项目,第二行将确保在删除该项目后适配器更新。

希望这有帮助

答案 1 :(得分:0)

更新适配器构造函数。删除内容后,从arraylist中删除该项目并通知更改的数据集。

   public class InterestAdapter extends BaseAdapter{
        private ArrayList<InterestClass>   m_List;
        private Context mContext;

        public InterestAdapter(Context context, ArrayList<InterestClass> list) {
            m_List = list;
            mContext = context;
        }

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

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final int pos = position;
            final Context context = parent.getContext();

            TextView        textName    = null;
            TextView        textDate    = null;
            TextView        textConPrice    = null;
            TextView        textNowPrice    = null;
            TextView        textFog    = null;
            ImageButton     btnPlus     = null;
            CustomHolder    holder  = null;

            if ( convertView == null ) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_interest, parent, false);

                textName        = (TextView) convertView.findViewById(R.id.i_ProdNmae);
                textDate        = (TextView) convertView.findViewById(R.id.i_Date);
                textConPrice    = (TextView) convertView.findViewById(R.id.i_InterPrice);
                textNowPrice    = (TextView) convertView.findViewById(R.id.i_currentPrice);
                textFog         = (TextView) convertView.findViewById(R.id.i_Analysis);
                btnPlus         = (ImageButton)   convertView.findViewById(R.id.i_addBtn);

                holder = new CustomHolder();
                holder.i_TextView    = textName;
                holder.i_TextView1   = textDate;
                holder.i_TextView2   = textConPrice;
                holder.i_TextView3   = textNowPrice;
                holder.i_TextView4   = textFog;
                holder.i_Btn         = btnPlus;
                convertView.setTag(holder);
            }
            else {
                holder          =  (CustomHolder) convertView.getTag();
                textName        =  holder.i_TextView;
                textDate        =  holder.i_TextView1;
                textConPrice    =  holder.i_TextView2;
                textNowPrice    =  holder.i_TextView3;
                textFog         =  holder.i_TextView4;
                btnPlus         =  holder.i_Btn;
            }

            /*if(position == 0) {
                textName.setText("종목");
                textDate.setText("관심일");
                textConPrice.setText("관심가");
                textNowPrice.setText("현재가");
                textFog.setText("수급");
                btnPlus.setEnabled(false);
            }else{
            }*/
                textName.setText(m_List.get(position).getName());
                textDate.setText(m_List.get(position).getDate());
                textConPrice.setText(m_List.get(position).getConPrice());
                textNowPrice.setText(m_List.get(position).getNowPrice());
                textFog.setText(m_List.get(position).getFog());
                btnPlus.setEnabled(true);

            textName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,DetailActivity.class);
                    context.startActivity(intent);
                }
            });

            btnPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Crypto enc = new Crypto();

                    SharedPreferences pref = context.getSharedPreferences("com.rabiaband.pref", 0);
                    String[] add_Params = new String[2];
                    try {
                        add_Params[0] = enc.AES_Encode(pref.getString("no", null), enc.global_deckey);
                        add_Params[1] = enc.AES_Encode(m_List.get(pos).getJmcode(), enc.global_deckey);
                    } catch (Exception e) {
                        Log.e("HomeCryptographError:", "" + e.getMessage());
                    }

                    AsyncCallAddDelFavorite delFavorite = new AsyncCallAddDelFavorite();
                    delFavorite.execute(add_Params);

                   **m_List.remove(position);
                   notifyDatasetChanged();**

                }
            });
    private class AsyncCallAddDelFavorite extends AsyncTask<String, Void, Void> {
        JSONObject del_Favorite_List;
        /* public AsyncCallAddFavorite(HomeFragment home){
             this.home=home;
         }*/
        @Override
        protected void onPreExecute() {
            /*Log.i(TAG, "onPreExecute");*/
        }

        @Override
        protected Void doInBackground(String... params) {
            dbConnection db_Cont=new dbConnection();
            String id=params[0];
            String jmcode=params[1];
            Log.v("doinbackgroud",""+id+"ssssss"+jmcode);
            del_Favorite_List=db_Cont.delFavorite(id, jmcode);
            Log.v("doinbackgroud",del_Favorite_List.toString());
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {

            /*Log.i(TAG, "onPostExecute");*/
        }
    }
}

更新片段中的适配器初始化。您正在直接向适配器添加项目。使用arraylist并在下载数据后创建适配器实例。

 private ArrayList<InterestClass>   interestList = new ArrayList<>();

并更新你的onPostExecute方法

@Override
        protected void onPostExecute(JSONArray result) {
            try{
               for (int i = 0 ; i < result.length() ; i ++){
                   InterestClass i_Class = new InterestClass();
                   String date=result.getJSONObject(i).getString("indate");
                   String time=date.substring(5,date.length());
                   i_Class.setJmcode(result.getJSONObject(i).getString("jmcode"));
                   i_Class.setName(result.getJSONObject(i).getString("jmname"));
                   i_Class.setDate(time);
                   i_Class.setConPrice(result.getJSONObject(i).getString("fprice"));
                   i_Class.setNowPrice(result.getJSONObject(i).getString("price"));
                   i_Class.setFog("40");
                   i_Class.setPlus(true);
                   interestList.add(i_Class);
                }
                interest_Adapter = new InterestAdapter(getActivity,interestList);
                interest_ListView.setAdapter(interest_Adapter);
            }catch(Exception e){
               Toast.makeText(getActivity(), "interest"+e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }