简体   繁体   English

如何在 recyclerview 中提供 onclick 以从 mysql 获取另一个活动的值

[英]how to give onclick in recyclerview to get values to another activity from mysql

I have a problem when I want to get the ID of the Recyclerview I created.当我想获取我创建的 Recyclerview 的 ID 时,我遇到了问题。 So later when the Recyclerview is in the click, it will appear details according to the ID that is click.所以后面Recyclerview在点击的时候,会根据点击的ID出现详情。 For the data i store in MySQL, and I have difficulties in its logic.对于我存储在 MySQL 中的数据,我的逻辑有困难。 Thanks谢谢

this is MainActivity.java `public class MainActivity extends AppCompatActivity {这是 MainActivity.java `public class MainActivity 扩展 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);
}

}` }`

The AdapterList.java AdapterList.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 (as new activity when recycler on click) 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"));

}

} ` } `

there are so many ways to implement a listener in a RecyclerView , the most common way would be to have an interface and use it as a listener on your adapter to retrieve the current item clicked.RecyclerView中实现listener器的方法有很多,最常见的方法是拥有一个interface并将其用作适配器上的侦听器来检索当前单击的项目。

You can have something like你可以有类似的东西

interface ItemListener { public <HashMap<String, String> onClick(<HashMap<String, String> item); }

You can pass an implementation of that on the constructor, save it as a property and then implement it in the onBindViewHolder , something like您可以在构造函数上传递它的实现,将其保存为属性,然后在onBindViewHolder中实现它,例如

holder.itemView.setOnClickListener({ listener.onClick(list_data.get(position)); })

Then in your Activity you just need to implement this interface and you will get the current item clicked and obtain the id without a problem!然后在你的Activity中,你只需要实现这个接口,你就可以点击当前的item,获取id没有问题!

I hope this helps you with this problem:)我希望这可以帮助你解决这个问题:)

Maybe you can try this, it makes you click the item and send the id.也许你可以试试这个,它会让你点击项目并发送 id。 add this in your onBindViewHolder将此添加到您的 onBindViewHolder

@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"));

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, DetailKate1.class);
            intent.putExtra("id", list_data.get(position).get("id"));
            context.startActivity(intent);
        }
    });

}

and then your DetailKate1 class will catch the id with然后您的 DetailKate1 class 将使用

txtTitle.setText(getIntent().getStringExtra("id"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从RecyclerView获取值以进行详细活动 - How to get values from RecyclerView to detail activity 基于URL ID从一个活动传递到另一个活动的onclick事件值在recyclerview中出现问题 - Issues in recyclerview with onclick event values passed from one activity to another activity based on url id RecyclerView onClick获取值 - RecyclerView onClick get values 在哪里以及如何将 onclick 放入 recyclerview 以获取包含该片段的活动的结果? - where and how to put a onclick in recyclerview to get the result in the activity that contain the fragment? 如何将参考从RecyclerView传递到另一个活动 - How to pass reference from RecyclerView to another activity 如何将一个活动的输入输入到另一个活动的回收视图中? - How do I get the input of an activity into the recyclerview of another activity? 将对象从RecyclerView发送到onclick新活动 - Send object from RecyclerView to new activity onclick 如何从另一个视图获取值并在同一活动中使用它? - How to get values from another view and use it in same activity? 如何从内部的 ImageButton 开始另一个活动到 RecyclerView 中? - How to start another activity from a ImageButton that it's inside into a RecyclerView? 如何从另一个活动(按钮)更新recyclerview JAVA - How to update recyclerview from another activity (button) JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM