2017-03-23 8 views
-1

このリサイクルビューのアイテムをクリックすると参照を取得し、このリファレンスを持つ別のアクティビティに移動します。RecyclerViewアイテムをクリックしてデータを取って別のアクティビティに移動

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view" 
     android:layout_width="wrap_content" 
     android:layout_height="119dp" 
     android:layout_gravity="center" 
     android:elevation="3dp" 
     card_view:cardUseCompatPadding="true" 
     card_view:cardElevation="4dp" 
     card_view:cardCornerRadius="1dp" 
     > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="112dp"> 

      <TextView 
       android:text="ID Article : " 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_idArticle1" 
       android:textStyle="normal|bold" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:textColor="@android:color/black" /> 
      <TextView 
       android:text="Reference : " 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_reference1" 
       android:textStyle="normal|bold" 

       android:layout_above="@+id/txt_des" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_marginBottom="5dp" 
       android:textColor="@android:color/background_dark" /> 
      <TextView 
       android:text="Prix : " 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_des1" 
       android:textStyle="normal|bold" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_marginBottom="-51dp" 
       android:layout_below="@+id/txt_des" 
       android:textColor="@android:color/background_dark" /> 
      <TextView 
       android:text="TextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_des" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/txt_prix1" 
       android:layout_toEndOf="@+id/txt_prix1" /> 
      <TextView 
       android:text="TextView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_prix" 
       android:layout_alignBaseline="@+id/txt_des1" 
       android:layout_alignBottom="@+id/txt_des1" 
       android:layout_toRightOf="@+id/txt_des1" 
       android:layout_toEndOf="@+id/txt_des1" /> 
      <TextView 
       android:text="TextView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_idArticle" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/txt_idArticle1" 
       android:layout_toEndOf="@+id/txt_idArticle1" /> 
      <TextView 
       android:text="TextView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_reference" 
       android:layout_alignBaseline="@+id/txt_reference1" 
       android:layout_alignBottom="@+id/txt_reference1" 
       android:layout_toRightOf="@+id/txt_reference1" 
       android:layout_toEndOf="@+id/txt_reference1" /> 
      <TextView 
       android:text="Designation : " 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txt_prix1" 
       android:textStyle="normal|bold" 
       android:textColor="@android:color/black" 
       android:layout_below="@+id/txt_reference1" 
       android:layout_alignParentLeft="true" 
       android:layout_marginBottom="5dp" 
       android:layout_alignParentStart="true" /> 
     </RelativeLayout> 
    </android.support.v7.widget.CardView> </LinearLayout> 

avticity_recherche_art.xml

:同じ機能

the recyclerview

card.xmlを持っています10

recherche_art.java:

package com.example.bacha.pfe.activity; 

import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.GridLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import com.example.bacha.pfe.R; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.Response; 

public class recherche_art extends AppCompatActivity { 
    private RecyclerView recyclerView ; 
    private GridLayoutManager gridLayoutManager; 
    private ArticleAdapter adapter ; 
    private List<Article> data_list ; 
    private String recherche_article; 
    private Button btnrechArt ; 
    private EditText arech; 
    int d=0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_recherche_art); 

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

    btnrechArt = (Button) findViewById(R.id.btnrechArt); 
     btnrechArt.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       arech= (EditText) findViewById(R.id.eTextRechArt); 
       data_list = new ArrayList<>(); 
       recherche_article=arech.getText().toString(); 

       load_article_from_server(0); 
       gridLayoutManager = new GridLayoutManager(recherche_art.this,1); 
       recyclerView.setLayoutManager(gridLayoutManager); 

       adapter = new ArticleAdapter(recherche_art.this,data_list); 
       recyclerView.setAdapter(adapter); 

       recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 
        @Override 
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
        if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){ 
          d=data_list.size(); 
          Log.d("PFE", String.valueOf(d)); 
          load_article_from_server(d); 
         } 

        } 
       }); 

      } 
     }); 

    } 

    private void load_article_from_server(final int id) { 

     AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() { 
      @Override 
      protected Void doInBackground(Integer... Params) { 
       OkHttpClient client = new OkHttpClient(); 
       Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build(); 
       try { 
        Response response = client.newCall(request).execute(); 

        JSONArray array = new JSONArray(response.body().string()); 

        for(int i=0;i<array.length();i++){ 
         JSONObject object = array.getJSONObject(i); 
         Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC")); 
         data_list.add(data); 

        } 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (JSONException e) { 
        System.out.print("End of content"); 
       } 


       return null ; 
      } 



      @Override 
      protected void onPostExecute(Void aVoid) { 
       super.onPostExecute(aVoid); 
       adapter.notifyDataSetChanged(); 
      } 
     }; 

     task.execute(id); 

    } 
} 

答えて

0

あなたはArticleAdapterで使用しているホルダーにリスナーを実装する必要があります。

次を参照してください。RecyclerView onClick

関連する問題