我正在使用項目在列表視圖中顯示圖像,當我們單擊列表視圖中的任何圖像時,然後轉到下一個意圖。而當我點擊時,特定項目的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);
}
}
我看不到這與MySQL – Strawberry
關於image_title和image_url有什麼關係。這些值是否在Secondpage.class中? – Minkoo
不,image_title和圖片url也不會 – Jincy