2017-10-28 82 views
0

我使用listview顯示來自API的響應,這是一個JSON對象。在listview下面,一個按鈕將會在那裏。單擊該按鈕時,我需要以JSON格式從listview中獲取所有數據。Listview以JSON格式獲取列表中的所有數據

活動類

package com.aryvart.myaromasupply; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.design.widget.CoordinatorLayout; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.View; 
import android.widget.AbsListView; 
import android.widget.Button; 
import android.widget.ListView; 
import com.aryvart.myaromasupply.Adapter.CartListAdapterKV; 
import com.aryvart.myaromasupply.Bean.CommonBean; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 

/** 
* Created by android01 on 28/8/17. 
*/ 

public class CartPageKV extends Activity implements MyInterface { 
    String json = null; 
    List<CommonBean> movieList = new ArrayList<>(); 
    RecyclerView recyclerView; 
    CartListAdapterKV mAdapter; 
    Context context; 
    CoordinatorLayout coordinatorLayout; 
    Button btn_submit; 
    HashMap<String, JSONObject> hsFilterGmap; 
    String value; 
    JSONArray jsonArray; 
    ListView llView; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.cart_page_kv); 
     context = this; 
     recyclerView = (RecyclerView) findViewById(R.id.my_recyclerView); 
     btn_submit = (Button) findViewById(R.id.btn_submit); 
     coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout); 
     llView = (ListView) findViewById(R.id.ll_view); 
     loadJSONFromAsset(); 

     //Response API 
     try { 
      JSONObject obj = new JSONObject(json); 
      Log.e("NN", "json-->" + obj.toString()); 

      JSONArray respArray = obj.getJSONArray("results"); 
      Log.e("NN", "respArray-->" + respArray.toString()); 
      for (int i = 0; i < respArray.length(); i++) { 
       JSONObject jsonObj = respArray.getJSONObject(i); 
       CommonBean drawerBean = new CommonBean(); 
       drawerBean.setStr_cart_id(jsonObj.getString("id")); 
       drawerBean.setStr_cart_title(jsonObj.getString("name")); 
       drawerBean.setStr_quan(jsonObj.getString("quantity")); 
       drawerBean.setStr_tot_quant(jsonObj.getString("total_quantity")); 
       movieList.add(drawerBean); 
      } 

      // Getting adapter by passing xml data ArrayList 
      mAdapter = new CartListAdapterKV(movieList, context, (MyInterface) context); 
      llView.setAdapter(mAdapter); 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     //Button Click Event 

     btn_submit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Log.e("NN:fc", String.valueOf(hsFilterGmap)); 
       Iterator myVeryOwnIterator = hsFilterGmap.keySet().iterator(); 
       JSONArray jsonArray = new JSONArray(); 
       while (myVeryOwnIterator.hasNext()) { 
        String key = (String) myVeryOwnIterator.next(); 
        JSONObject value1 = hsFilterGmap.get(key); 
        Log.e("NN:value", value1.toString()); 
        jsonArray.put(value1); 

       } 

       Log.e("NN:fcAr", jsonArray.toString().replaceAll("\\\\", "")); 
       System.out.println("the JSON ARRAY is" + jsonArray.toString()); 

      } 
     }); 
    } 

    public String loadJSONFromAsset() { 

     try { 

      InputStream is = getAssets().open("data.json"); 
      int size = is.available(); 
      byte[] buffer = new byte[size]; 
      is.read(buffer); 
      is.close(); 
      json = new String(buffer, "UTF-8"); 


     } catch (IOException ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
     return json; 

    } 


    // Interface to get Checked value from listview(Hashmap for not allowing duplicates) 
    @Override 
    public HashMap<String, JSONObject> getUnCheckedVal(HashMap<String, JSONObject> strVal, String str_removed_id) { 

     hsFilterGmap = strVal; 
     Log.e("NN:fc", String.valueOf(hsFilterGmap)); 
     return hsFilterGmap; 
    } 
} 

適配器類別

package com.aryvart.myaromasupply.Adapter; 

import android.content.Context; 
import android.support.v7.widget.CardView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.aryvart.myaromasupply.Bean.CommonBean; 
import com.aryvart.myaromasupply.MyInterface; 
import com.aryvart.myaromasupply.R; 
import com.like.LikeButton; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.util.HashMap; 
import java.util.List; 

/** 
* Created by android01 on 28/8/17. 
*/ 

public class CartListAdapterKV extends BaseAdapter { 
    private List<CommonBean> commonBeanList; 
    Context c; 
    MyInterface my_interface; 
    private static LayoutInflater inflater = null; 
    HashMap<String, JSONObject> hsMap = new HashMap<String, JSONObject>(); 

    // constructor 
    public CartListAdapterKV(List<CommonBean> movieList, Context context, MyInterface inter) { 
     this.commonBeanList = movieList; 
     Log.e("NN", "size-->" + this.commonBeanList); 
     this.c = context; 
     this.my_interface = inter; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return commonBeanList.size(); 
    } 

    public Object getItem(int position) { 
     return commonBeanList.get(position); 
    } 

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

    public View getView(final int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (convertView == null) 
      view = inflater.inflate(R.layout.cart_items_kv, parent, false); 
     TextView txt_cartTitle; 
     final CheckBox cb_box; 
     txt_cartTitle = (TextView) view.findViewById(R.id.textView2); 
     cb_box = (CheckBox) view.findViewById(R.id.checkBoxKV); 
     final CommonBean recyclerBean = commonBeanList.get(position); 
     cb_box.setChecked(true); 
     txt_cartTitle.setText(recyclerBean.getStr_cart_title()); 

     //check if checkbox is checked. if yes the add value to hashmap(by default all checkboxes will be checked in listview 

     if (cb_box.isChecked()) { 
      Toast.makeText(c, "--" + recyclerBean.getStr_cart_title(), Toast.LENGTH_SHORT).show(); 
      //JSONArray req = new JSONArray(); 
      JSONObject jsoBj = new JSONObject(); 
      try { 

       jsoBj.put("id", recyclerBean.getStr_cart_id()); 
       jsoBj.put("value", recyclerBean.getStr_cart_title()); 
       jsoBj.put("checked", "true"); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      hsMap.put(recyclerBean.getStr_cart_id(), jsoBj); 
      my_interface.getUnCheckedVal(hsMap, recyclerBean.getStr_cart_id()); 
      Log.e("NN", "AdpaMap--" + hsMap.toString()); 

     } else { 

      JSONObject jsoBj = new JSONObject(); 
      try { 
       jsoBj.put("id", recyclerBean.getStr_cart_id()); 
       jsoBj.put("value", recyclerBean.getStr_cart_title()); 
       jsoBj.put("checked", "false"); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      //adding the json object in hashmap to remove duplicates 
      hsMap.put(recyclerBean.getStr_cart_id(), jsoBj); 
      my_interface.getUnCheckedVal(hsMap, recyclerBean.getStr_cart_id()); 
      Toast.makeText(c, "-*-" + recyclerBean.getStr_cart_title(), Toast.LENGTH_SHORT).show(); 
     } 
     cb_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
       final CommonBean recyclerBean = commonBeanList.get(position); 
       if (cb_box.isChecked()) { 
        Toast.makeText(c, "--" + recyclerBean.getStr_cart_title(), Toast.LENGTH_SHORT).show(); 
        JSONObject jsoBj = new JSONObject(); 
        try { 

         jsoBj.put("id", recyclerBean.getStr_cart_id()); 
         jsoBj.put("value", recyclerBean.getStr_cart_title()); 
         jsoBj.put("checked", "true"); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        hsMap.put(recyclerBean.getStr_cart_id(), jsoBj); 
        my_interface.getUnCheckedVal(hsMap, recyclerBean.getStr_cart_id()); 
        Log.e("NN", "AdpaMap--" + hsMap.toString()); 

       } else { 

        JSONObject jsoBj = new JSONObject(); 
        try { 
         jsoBj.put("id", recyclerBean.getStr_cart_id()); 
         jsoBj.put("value", recyclerBean.getStr_cart_title()); 
         jsoBj.put("checked", "false"); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        hsMap.put(recyclerBean.getStr_cart_id(), jsoBj); 
        my_interface.getUnCheckedVal(hsMap, recyclerBean.getStr_cart_id()); 
        Toast.makeText(c, "-*-" + recyclerBean.getStr_cart_title(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
     return view; 
    } 
} 

上的按鈕的初始點擊,我發現了這是在前景(對用戶可見的數據),一旦我滾動剩餘的數據越來越。

任何人都可以幫助我怎樣才能得到在ListView的整個值在按鈕點擊?

+0

如果從JSON填充ListView控件,然後嘗試檢索上點擊相同的JSON那麼爲什麼你不只是持有後添加點擊填充listView後的JSON然後引用? – SRoseDev88

回答

0

打開此cart_items_kv xml並放置按鈕。 轉到適配器 並在那裏生成事件。您可以通過適配器中的位置獲取項目的數據。

另一種解決方案

找到視圖

public ViewHolder(View itemLayoutView) { 
// here `enter code here` 
itemLayoutView.setOnClickListener(this); 
} 
+0

當我檢查並取消選中複選框時,我從適配器獲取值。但我需要整個列表視圖數據作爲單擊按鈕上的JSON對象。它顯示在listview下面。 – NandhiniB

相關問題