У меня проблема, когда я хочу получить идентификатор созданного мной Recyclerview. Так что позже, когда Recyclerview будет в щелчке, он отобразит детали в соответствии с идентификатором, который щелкнул. Для данных я храню в MySQL, и у меня есть трудности с его logi c. Спасибо
, это MainActivity. java `publi c class MainActivity extends AppCompatActivity {
private RecyclerView lvhape;
private RequestQueue requestQueue;
private StringRequest stringRequest;
ArrayList<HashMap<String, String>> list_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://192.168.43.77/app_blogvolley/getdata.php";
lvhape = (RecyclerView) findViewById(R.id.lvhape);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
lvhape.setLayoutManager(llm);
requestQueue = Volley.newRequestQueue(MainActivity.this);
list_data = new ArrayList<HashMap<String, String>>();
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response ", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("handphone");
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", json.getString("idhp"));
map.put("merk", json.getString("merk"));
map.put("tipe", json.getString("tipe"));
map.put("gambar", json.getString("gambar"));
map.put("keterangan", json.getString("keterangan"));
list_data.add(map);
AdapterList adapter = new AdapterList(MainActivity.this, list_data);
lvhape.setAdapter(adapter);
lvhape.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && gestureDetector.onTouchEvent(e)) {
int position = rv.getChildAdapterPosition(child);
Intent i = new Intent(getApplicationContext(), DetailKate1.class);
i.putExtra("id", list_data.get(position).get("id"));
startActivity(i);
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
}`
Список адаптеров. java
`public class AdapterList extends RecyclerView.Adapter<AdapterList.ViewHolder>{
Context context;
ArrayList<HashMap<String, String>> list_data;
public AdapterList(MainActivity mainActivity, ArrayList<HashMap<String, String>> list_data) {
this.context = mainActivity;
this.list_data = list_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Glide.with(context)
.load("http://192.168.43.77/app_blogvolley/img/" + list_data.get(position).get("gambar"))
.crossFade()
.placeholder(R.mipmap.ic_launcher)
.into(holder.imghape);
holder.txthape.setText(list_data.get(position).get("merk"));
holder.txtmerk.setText(list_data.get(position).get("tipe"));
}
@Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txthape;
TextView txtmerk;
ImageView imghape;
public ViewHolder(View itemView) {
super(itemView);
txthape = (TextView) itemView.findViewById(R.id.txthape);
txtmerk = (TextView) itemView.findViewById(R.id.txtmerk);
imghape = (ImageView) itemView.findViewById(R.id.imghp);
}
}
} `
DetailKate1. java (как новое действие при переработке при щелчке)
`public class DetailKate1 extends AppCompatActivity {
public TextView txtTitle;
ArrayList<HashMap<String, String>> list_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_kate1);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
txtTitle = (TextView) findViewById(R.id.txtTitle);
txtTitle.setText(getIntent().getStringExtra("id"));
}
}`