2016-04-22 24 views
1

問題があります。 listview baseAdapterからdelete(interestAdepterクラスのbtnPlus.setOnClickListener)ボタンをクリックすると、listviewを含むフラグメントをリフレッシュする必要があります。アクティビティやフラグメントからフラグメントをリフレッシュする方法(baseAdapterクラスから)

1.InterestAdapterクラス{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クラスを追加します。ありがとうございました。

+0

'interest_ListView'にも – USKMobility

+0

あなたは' interest_Adapter'を取り付けるAsyncCallInterestとAsyncCallAddDelFavoriteのコードを追加してください更新? –

+0

AsyncCallInterestとAsyncCallAddDelFavoriteを追加しました。 interest_Adapterは、AsyncCallInterestクラスのnterest_ListViewにアタッチされます。ありがとうございました。 – Park

答えて

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(); 
      } 
     } 
+0

ありがとうございました!アダプターにクラスを直接追加するべきではありません。 – Park

0

私が正しくあなたを理解していれば、あなたがこの

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

最初の行は項目と2つ目の意志を削除する呼び出すことができることを実現するために、あまりにもあなたがあなたのデータベースから削除した後、あなたのListViewから項目を削除したいですアイテムが削除された後にアダプタが更新されることを保証します。

ホープこれは

関連する問題