2016-05-16 75 views
2

我在CardView內部有一個代表RecycleView項目的按鈕。我已經設法處理ViewHolder類中的click事件,但我需要在MainActivity上調用函數,我如何使用下面的代碼實現它?處理RecycleView項目中的圖像按鈕點擊

我的代碼如下

ShopItemRecyclerViewAdapter.java 
public class ShopItemRecyclerViewAdapter extends RecyclerView.Adapter<ShopItemRecyclerViewAdapter.ListItemViewHolder> { 

      static ArrayList<ShopListItemModel> list; 
      LayoutInflater inflater; 

      public ShopItemRecyclerViewAdapter(ArrayList<ShopListItemModel> list, Context context){ 
       inflater = LayoutInflater.from(context); 
       this.list = list; 
      } 



      public ListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
       View view = inflater.inflate(R.layout.list_item, parent , false); 
       ListItemViewHolder vh = new ListItemViewHolder(view); 
       return vh; 

      } 

      public void onBindViewHolder(ListItemViewHolder holder, int position) { 
       ShopListItemModel current = list.get(position); 
       holder.name.setText(current.getName()); 
       holder.price.setText(String.valueOf(current.getPrice())); 

      } 

      public int getItemCount() { 
       return list.size(); 
      } 

      public static class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
       CardView cv; 
       TextView name; 
       TextView price; 
       ImageButton btnDelete; 

       ListItemViewHolder(final View itemView) { 
        super(itemView); 

        cv = (CardView)itemView.findViewById(R.id.cvShopListItem); 
        name = (TextView)itemView.findViewById(R.id.name); 
        price = (TextView)itemView.findViewById(R.id.price); 
        btnDelete = (ImageButton)itemView.findViewById(R.id.btnDeleteItem); 

        itemView.setOnClickListener(this); 
        btnDelete.setOnClickListener(this); 
       } 

       @Override 
       public void onClick(View v) { 
        //here i can handle the click but i think i need to use it in the main activity 
       } 
      } 
     } 

MainActivity.java(跳過無關緊要代碼)

public class ShopCartScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 

    RecyclerView cartItems; //recycler to hold the cart list 
    ArrayList<ShopListItemModel> list = new ArrayList<ShopListItemModel>(); 
    ShopItemRecyclerViewAdapter adapter; 
    GetShopingCartList getShopingCartList; ////instance of network operation class to retrieve shop cart items list from server data base 

    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.shop_carts_list); 
      cartItems = (RecyclerView) findViewById(R.id.newListItem); 
      cartItems.setHasFixedSize(true); 
      LinearLayoutManager llm = new LinearLayoutManager(this); 
      llm.setOrientation(LinearLayoutManager.VERTICAL); 
      cartItems.setLayoutManager(llm) 
    } 

      public void bindData(int listNumber) { 
      getShopingCartList = new GetShopingCartList(this, list, adapter, cartItems, listNumber, totalPrice); 
      getShopingCartList.execute("link to query which returns json object"); 
      } 
    } 

戈tShopingCartList.java網絡運行

public class GetShopingCartList extends AsyncTask<String, String, ArrayList<ShopListItemModel>> { 

      private ArrayList<ShopListItemModel> shopCartItemList; 
      Context context; 
      RecyclerView items; 
      ShopItemRecyclerViewAdapter adapter; 
      int listNumber; 

      public GetShopingCartList(Context context, ArrayList<ShopListItemModel> shopCartItemList, ShopItemRecyclerViewAdapter adapter, 
             RecyclerView items ,int listNumber) { 
       this.context = context; 
       this.shopCartItemList = shopCartItemList; 
       this.adapter = adapter; 
       this.items = items; 
       this.listNumber = listNumber;  
      } 
     protected ArrayList<ShopListItemModel> doInBackground(String... params) { 
       HttpURLConnection connection = null; 
       BufferedReader reader = null; 
       shopCartItemList = new ArrayList<ShopListItemModel>(); 
     try { 
        URL url = new URL(params[0]); 
        connection = (HttpURLConnection) url.openConnection(); 
        connection.connect(); 
        InputStream stream = connection.getInputStream(); 
        reader = new BufferedReader(new InputStreamReader(stream)); 
        StringBuffer buffer = new StringBuffer(); 

        String line = ""; 
        while ((line = reader.readLine()) != null) { 
         buffer.append(line); 

        } 
        String finalJson = buffer.toString(); 
        JSONObject parentObject = new JSONObject(finalJson); 
        JSONArray parentArray = parentObject.getJSONArray("result"); 
     for (int i = 0; i < parentArray.length(); i++) { 
         JSONObject finalObject = parentArray.getJSONObject(i);//get the cuttent json object which is representaion of shop cart model object 

         String name = finalObject.getString("name"); 
         String price = finalObject.getString("price"); 
         Double d = Double.parseDouble(price); 
         ShopListItemModel item = new ShopListItemModel(name, d); 
         shopCartItemList.add(item);//adds the shopcart to the list of shop carts model 

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

       } finally { 
        if (connection != null) { 
         connection.disconnect(); 
        } 
        try { 
         if (reader != null) { 
          reader.close(); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 

        } 
       } 
       return shopCartItemList; 
      } 

      protected void onPostExecute(ArrayList<ShopListItemModel> s) { 
       super.onPostExecute(s); 
       adapter = new ShopItemRecyclerViewAdapter(shopCartItemList, context); 
       items.setAdapter(adapter); 
     } 
       public ArrayList<ShopListItemModel> getList() { 
       return shopCartItemList; 
      } 

     } 
+0

您可以使用本地廣播接收器,這個..從適配器請在'的onClick()廣播'和你**活動收到**。我可以舉一個例子,如果你想實現..! –

+0

即時通訊對於Android開發來說很新穎,如果您對此有所瞭解,請儘量以解釋爲例。我也從來沒有實施廣播接收器 –

回答

1

內ShopCartScreen.java實現方法,那麼你可以使用適配器內部context對象。

((ShopCartScreen)context).methodImplemented(ShopListItemModel model) 
    //add this code inside onClick event of the button 
+0

我已經這樣實施。主要活動:公共無效deleteItem(int postion){ list = getShopingCartList.getList(); list.remove(postion); adapter = new ShopItemRecyclerViewAdapter(list,this); cartItems.setAdapter(adapter); }它的工作,但通過這種方式刪除動畫不起作用,你有一個解決方案? –

+0

onClick在viewHolder中的按鈕:public void onClick(View v){ //這裏我可以處理點擊,但我認爲我需要在主要活動 ((ShopCartScreen)上下文).deleteItem(getPosition() ); } getPosition也被棄用 –

-1

確定這是我的解決方案,如果有人需要它(ⅰ合併在主活性2種方法/ 1和1中的再循環器適配器):

在我的再循環器適配器添加此刪除方法:

//delete the item from the recycler Immediately for user interaction 
public void delete(int position){ 
     list.remove(position); 
     notifyItemRemoved(position); 
    } 
我ViewHolder類

ListItemViewHolder(final View itemView) { 
      super(itemView); 

      cv = (CardView)itemView.findViewById(R.id.cvShopListItem); 
      name = (TextView)itemView.findViewById(R.id.name); 
      price = (TextView)itemView.findViewById(R.id.price); 
      btnDelete = (ImageButton)itemView.findViewById(R.id.btnDeleteItem); 

      itemView.setOnClickListener(this); 
      btnDelete.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      ((ShopCartScreen)context).deleteItem(getPosition());//calls method in main activity 
      delete(getPosition()); 
     } 

,並在主要活動:

public void deleteItem(int postion){ 
      list = getShopingCartList.getList(); 
      ShopListItemModel tmp = list.get(postion); 
      tmp.getName(); //gets the item name to remove 
      shopCartModel.getNumber(); //gets the cart number for deleting the item for the correct cart 
      new DeleteCartItem(this , shopCartModel , tmp).execute(); //remove the item from the data base 
     }