2017-03-15 68 views
-2

我正在使用項目在列表視圖中顯示圖像,當我們單擊列表視圖中的任何圖像時,然後轉到下一個意圖。而當我點擊時,特定項目的ID不會傳遞給其他意圖。我不知道該怎麼辦。當我們點擊該項目時,如何在列表視圖中獲取項目的ID?

主Activity.java

List<GetDataAdapter> GetDataAdapter11; 

RecyclerView recyclerView; 

RecyclerView.LayoutManager recyclerViewlayoutManager; 

RecyclerView.Adapter recyclerViewadapter; 

String GET_JSON_DATA_HTTP_URL = "http://192.1622/.22/new.php"; 
String JSON_IMAGE_TITLE_NAME = "image_title"; 
String JSON_IMAGE_URL = "image_url"; 

JsonArrayRequest jsonArrayRequest ; 

RequestQueue requestQueue ; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_tab_activity_1); 
    GetDataAdapter11 = new ArrayList<>(); 

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

    recyclerView.setHasFixedSize(true); 

    recyclerViewlayoutManager = new LinearLayoutManager(this); 

    recyclerView.setLayoutManager(recyclerViewlayoutManager); 


    JSON_DATA_WEB_CALL(); 


} 

public void JSON_DATA_WEB_CALL(){ 

    jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL, 

      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 

        JSON_PARSE_DATA_AFTER_WEBCALL(response); 
       } 
      }, 

      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 

    requestQueue = Volley.newRequestQueue(this); 

    requestQueue.add(jsonArrayRequest); 
} 

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){ 

    for(int i = 0; i<array.length(); i++) { 

     GetDataAdapter GetDataAdapter2 = new GetDataAdapter(); 

     JSONObject json = null; 
     try { 

      json = array.getJSONObject(i); 
      // GetDataAdapter2.setImageTitleNamee(String.valueOf(json.getInt(String.valueOf(JSON_id)))); 

      GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME)); 

      GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL)); 

     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 
     GetDataAdapter11.add(GetDataAdapter2); 
    } 

    recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter11, this); 

    recyclerView.setAdapter(recyclerViewadapter); 


} 
} 

RecyclerViewAdapter.Java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { 

Bitmap bitmap; 
Context context; 
List<GetDataAdapter> getDataAdapter; 
ImageLoader imageLoader1; 
int id; 


public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context) { 

    super(); 
    this.getDataAdapter = getDataAdapter; 
    this.context = context; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false); 

    ViewHolder viewHolder = new ViewHolder(v,context,getDataAdapter); 

    return viewHolder; 
} 

@Override 
public void onBindViewHolder(ViewHolder Viewholder, int position) { 

    GetDataAdapter getDataAdapter1 = getDataAdapter.get(position); 

    imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader(); 

    imageLoader1.get(getDataAdapter1.getImageServerUrl(), 
      ImageLoader.getImageListener(
        Viewholder.networkImageView,//Server Image 
        R.mipmap.ic_launcher,//Before loading server image the default showing image. 
        android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server. 
      ) 
    ); 


    Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1); 
    Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName()); 

} 

@Override 
public int getItemCount() { 

    return getDataAdapter.size(); 
} 

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    public TextView ImageTitleNameView; 
    // public TextView id; 
    public NetworkImageView networkImageView; 

    List<GetDataAdapter> getDataAdapter ;//= new List<GetDataAdapter>(); 
    Context context; 

    public ViewHolder(View itemView,Context context,List<GetDataAdapter> getDataAdapter) { 

     super(itemView); 
     this.getDataAdapter=getDataAdapter; 
     this.context=context; 
     itemView.setOnClickListener(this); 

     ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item); 
     networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1); 
     id = Integer.parseInt(((TextView) itemView.findViewById(R.id.pic_id)).getText().toString()); 


    } 
    @Override 
    public void onClick(View v) { 
     int position = getAdapterPosition(); 
     GetDataAdapter getDataAdapters=this.getDataAdapter.get(position); 
     Intent intent=new Intent(this.context,Secondpage.class); 

     intent.putExtra("image_title",getDataAdapters.getImageTitleName()); 
     Log.e("id","id of clicked image" +ImageTitleNameView); 
     intent.putExtra("image_url",getDataAdapters.getImageServerUrl()); 
     Log.e("id","id of clicked image" +networkImageView); 
     intent.putExtra("id",getDataAdapters.getId()); 
     Log.e("id","id of clicked image" +id); 
     this.context.startActivity(intent); 

    } 
} 
+0

我看不到這與MySQL – Strawberry

+0

關於image_title和image_url有什麼關係。這些值是否在Secondpage.class中? – Minkoo

+0

不,image_title和圖片url也不會 – Jincy

回答

0
view.setTag(id); 

在onBindViewHolder和

view.getTag(); 

中的onClick會解決你的問題

+0

先生,什麼是標籤? – Jincy

+0

我們可以使用set-tag函數設置一個值來查看,並且可以使用get-tag檢索該值。 –

0

首先,確保你獲得點擊項目的位置,然後從ITEMLIST

獲得項目嘗試這樣:

int pos = (int) v.getTag(); 
    Item item =ItemList.get(pos); 

,然後嘗試從這個項目(即item.getId)

0

添加ClickListener在onBindViewHolder和ViewHolder

刪除的onClick獲得ID
@Override 
    public void onBindViewHolder(ViewHolder Viewholder, int position) { 

     GetDataAdapter getDataAdapter1 = getDataAdapter.get(position); 

     imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader(); 

     imageLoader1.get(getDataAdapter1.getImageServerUrl(), 
       ImageLoader.getImageListener(
         Viewholder.networkImageView,//Server Image 
         R.mipmap.ic_launcher,//Before loading server image the default showing image. 
         android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server. 
       ) 
     ); 


     Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1); 
     Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName()); 

     Viewholder.networkImageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int position = getAdapterPosition(); 
      GetDataAdapter getDataAdapters=this.getDataAdapter.get(position); 
      Intent intent=new Intent(this.context,Secondpage.class); 

      intent.putExtra("image_title",getDataAdapters.getImageTitleName()); 
      Log.e("id","id of clicked image" +ImageTitleNameView); 
      intent.putExtra("image_url",getDataAdapters.getImageServerUrl()); 
      Log.e("id","id of clicked image" +networkImageView); 
      intent.putExtra("id",getDataAdapters.getId()); 
      Log.e("id","id of clicked image" +id); 
      context.startActivity(intent); 
     } 
    }); 

    } 



    class ViewHolder extends RecyclerView.ViewHolder { 

     public TextView ImageTitleNameView; 
     // public TextView id; 
     public NetworkImageView networkImageView; 

     List<GetDataAdapter> getDataAdapter ;//= new List<GetDataAdapter>(); 
     Context context; 

     public ViewHolder(View itemView,Context context,List<GetDataAdapter> getDataAdapter) { 

      super(itemView); 
      this.getDataAdapter=getDataAdapter; 
      this.context=context; 

      ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item); 
      networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1); 
      id = Integer.parseInt(((TextView) itemView.findViewById(R.id.pic_id)).getText().toString()); 

     } 
    } 
相關問題