Android Studio 如何从RecyclerView中的按钮更改TextView?

wtlkbnrh  于 2023-05-18  发布在  Android
关注(0)|答案(1)|浏览(214)

主要活动

public class MainActivity extends AppCompatActivity {


    public TextView kalori;
    RecyclerView rv;

    List<yiyecek> yiyecekList;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv=findViewById(R.id.myrecycler);

        kalori=findViewById(R.id.kalori);

        yiyecekList= new ArrayList<>();

        yiyecekList.add(new yiyecek("Egg","50"));
        yiyecekList.add(new yiyecek("Cheese","150"));

        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new MyAdapter(this,yiyecekList));

    }
}

MyAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    List<yiyecek> yiyecekler;

    public MyAdapter(Context context, List<yiyecek> yiyecekler ) {
        this.context = context;
        this.yiyecekler = yiyecekler;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyAdapter.MyViewHolder(LayoutInflater.from(context).inflate(R.layout.yiyecekrowitem,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        

        yiyecek ymk=yiyecekler.get(position);

        String name=ymk.getAd();
        String kalori=ymk.getKalori();

        holder.text.setText(name);
        holder.textkalori.setText(kalori);

    }

    @Override
    public int getItemCount() {
        return yiyecekler.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView text,textkalori,textadet;
        public Button ekle;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            textadet=itemView.findViewById(R.id.adet);
            text = itemView.findViewById(R.id.textViewad);
            textkalori = itemView.findViewById(R.id.textViewKalori);
            ekle=itemView.findViewById(R.id.butonekle);

            itemView.findViewById(R.id.butonekle).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String ilkadet=textadet.getText().toString();  
                    MainActivity mm=new MainActivity();
                    mm.kalori.setText(ilkadet);
                }
            });
        }
    }
}

RecyclerView xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="97dp"
    android:layout_margin="8dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/textViewad"
        android:layout_width="103dp"
        android:layout_height="48dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="11dp"
        android:layout_marginTop="11dp"
        android:gravity="center"
        android:text="TextView"
        android:textAlignment="center"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textViewKalori"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text="100"
        android:textAlignment="center"

        android:textSize="20sp" />

    <Button
        android:id="@+id/butonekle"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="284dp"
        android:layout_marginTop="24dp"
        android:text="EKLE" />

    <TextView
        android:id="@+id/adet"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="191dp"
        android:layout_marginTop="25dp"
        android:textColor="#282828"
        android:text="0"
        android:textSize="29sp"
        android:textAlignment="center"
        android:gravity="center"/>

</RelativeLayout>

例外

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.kalorirecyclerdeneme, PID: 30408
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.kalorirecyclerdeneme.MyAdapter$MyViewHolder$1.onClick(MyAdapter.java:91)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1211)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 30408 SIG: 9

对不起,我的代码不好。我才刚开始我的项目是关于卡路里的计算。我在用RecyclerView分类食物。当我按下RecyclerView上的按钮时,显示数字的TextView值增加了一。我试图将其打印到MainActivity中的TextView中,但它给出了一个空错误。
我得到空错误:
mm.kalori.setText(ilkadet);
我在RecyclerView中有一个按钮,我正在尝试使用此按钮更改MainActivity中TextView的值。

mpbci0fu

mpbci0fu1#

您不能创建Activityinstance,您可以将引用传递给视图,但更好的方法是传递一个interface,它将在单击时从adapter调用

interface ClickDelegate {
    void onClick(String text);
}    

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    List<yiyecek> yiyecekler;
    ClickDelegate delegate;

    public MyAdapter(Context context, List<yiyecek> yiyecekler, ClickDelegate delegate) {
        this.context = context;
        this.yiyecekler = yiyecekler;
        this.delegate = delegate;
    }

    // rest of the code

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView text,textkalori,textadet;
        public Button ekle;

        public MyViewHolder(@NonNull View itemView, ClickDelegate delegate) {
            super(itemView);

            textadet=itemView.findViewById(R.id.adet);
            text = itemView.findViewById(R.id.textViewad);
            textkalori = itemView.findViewById(R.id.textViewKalori);
            ekle=itemView.findViewById(R.id.butonekle);

            itemView.findViewById(R.id.butonekle).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String ilkadet=textadet.getText().toString();
                    delegate.onClick(ilkadet);
                }
            });
        }
    }
}

//in activity
rv.setAdapter(new MyAdapter(this, yiyecekList, new ClickDelegate(){ 
@Override
public void onClick(String text){
    kalori.setText(ilkadet);
}
}));

相关问题