java—如何从对话框recyclerview获取数据到其托管活动?

dgsult0t  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(361)

嗨,在那里我有一个麻烦,从回收器内的对话框视图返回文本。有人请帮助获得数据从对话框视图到相同的活动。
我的数据流是:
主活动-->用户详细信息活动-->对话框(recyclerview)--->用户详细信息活动-->更新按钮
现在发生的是:
mainactivity-->userdetailsactivity-->dialog(recyclerview)--->应用程序崩溃!!
我想要的是将税包括钱返回到租金文本视图



下面是我的userdetailsactivity代码。
用户详细信息活动.java

package com.ancorp.aplo.activity;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.telephony.SmsManager;

import android.widget.Toast;

import com.ancorp.aplo.R;
import com.ancorp.aplo.adapter.ChannelAdapter;
import com.ancorp.aplo.database.SqliteDatabase;
import com.ancorp.aplo.model.Contacts;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.time.Month;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class UserDetailsActivity extends AppCompatActivity {
    private final static int SEND_SMS_PERMISSION_REQ=1;
    public TextView textView ;
    EditText name_edit;
    EditText jacode_edit;
    EditText phone_edit;
    EditText balance_edit , paidDate_edit, paidAmount_edit, rent_edit, zone_edit;

    SqliteDatabase myDB;
    String header,data1,data2,timeStamp;
    String month;
    private Context context;
    float amount;
    Integer d1,d2;

    Integer y;
    TextView phone_text,btnRecharge, Activation, Deactivation , rent_text , zone_text,plan_text,name_text,jacode_text, Status_text, id_text;

    TextView balance_text, LastPaidAmount,LastPaidDate;
    int deactive = Color.RED;
    int activeg = Color.GREEN;
    Button btnEdit;
    String id,balance, Lpdate, LpAmount,zone,rent;
    String phoneNo,message;
    String Status;
    Button  btndelete ;
    ImageView editimgbtn,addimgbtn;
    Spinner pkgname;
    String SpinnerData;
    String planAmount,chName,chId,chMoney;

    private static int ACTIVE_CODE = 0;
    private static int DEACTIVE_CODE = 0;
    private SqliteDatabase mDatabase;
    private ArrayList<Contacts> userData;
    private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
    Button recharges;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_details);
        mDatabase = new SqliteDatabase(this);

       // getUserData(id);

        ActionBar actionBar = getSupportActionBar();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        actionBar.setTitle("Customer Details");

        name_edit = findViewById(R.id.user_name);
        jacode_edit = findViewById(R.id.name_jacode);
        phone_edit = findViewById(R.id.name_phone);
        balance_edit = findViewById(R.id.name_balance);
        paidAmount_edit = findViewById(R.id.LastPaidAmount_edit);
        paidDate_edit = findViewById(R.id.lastPaidDate_edit);
        rent_edit = findViewById(R.id.name_rent);
        zone_edit =findViewById(R.id.name_zone);
        pkgname= findViewById(R.id.name_package);
        id_text = findViewById(R.id.id);

        name_text= findViewById(R.id.user_name_text);
        Status_text = findViewById(R.id.cid);
        phone_text= findViewById(R.id.name_phone_text);
        jacode_text= findViewById(R.id.name_jacode_text);
        balance_text =findViewById(R.id.name_balance_text);
        LastPaidAmount = findViewById(R.id.LastPaidAmount_text);
        LastPaidDate = findViewById(R.id.lastPaidDate_text);
        rent_text = findViewById(R.id.name_rent_text);
        zone_text = findViewById(R.id.name_zone_text);
        plan_text = findViewById(R.id.name_package_text);
        Activation = findViewById(R.id.Activate);
        Deactivation = findViewById(R.id.DeActivate);

        //rent_edit = findViewById(R.id.name_rent);
        //zone_edit = findViewById(R.id.name_zone);
        addimgbtn = findViewById(R.id.addimgbtn);
        btnEdit=findViewById(R.id.btnEdit);
        btndelete = findViewById(R.id.btnDelete);
        btnRecharge = findViewById(R.id.btnRecharge);
        editimgbtn = findViewById(R.id.editbtnimg);
        addimgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddChannel(UserDetailsActivity.this);
            }
        });

        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editTask();
            }
        });

        btnRecharge.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                rechargeUser();

            }
        });

        editimgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadSpinnerData();

                name_edit.setVisibility(View.VISIBLE);
                phone_edit.setVisibility(View.VISIBLE);
                jacode_edit.setVisibility(View.VISIBLE);
                balance_edit.setVisibility(View.VISIBLE);
                paidAmount_edit.setVisibility(View.VISIBLE);
                paidDate_edit.setVisibility(View.VISIBLE);
                btndelete.setVisibility(View.VISIBLE);
                btnEdit.setVisibility(View.VISIBLE);
                rent_edit.setVisibility(View.VISIBLE);
                zone_edit.setVisibility(View.VISIBLE);
                pkgname.setVisibility(View.VISIBLE);

                name_text.setVisibility(View.GONE);
                phone_text.setVisibility(View.GONE);
                jacode_text.setVisibility(View.GONE);
                balance_text.setVisibility(View.GONE);
                LastPaidAmount.setVisibility(View.GONE);
                LastPaidDate.setVisibility(View.GONE);
                btnRecharge.setVisibility(View.GONE);
                rent_text.setVisibility(View.GONE);
                zone_text.setVisibility(View.GONE);
                plan_text.setVisibility(View.GONE);

                // rechargebtn.setVisibility(View.GONE);

            }
        });

        Intent intent =getIntent();
        if (intent.getExtras() != null){

            Contacts contacts = (Contacts) intent.getSerializableExtra("data");
           id = getIntent().getStringExtra("id");
           balance = getIntent().getStringExtra("balance");
           Lpdate = getIntent().getStringExtra("LPDATE");
           LpAmount = getIntent().getStringExtra("lpAmount");
           zone = getIntent().getStringExtra("zone");
           rent = getIntent().getStringExtra("rent");
            Status_text.setText(contacts.getStatus());
            Status = contacts.getStatus();
            chId=getIntent().getStringExtra("channelID");
            chName = getIntent().getStringExtra("channelName");
            chMoney = getIntent().getStringExtra("channelMoney");
            Toast.makeText(getApplicationContext(), chId , Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), chName , Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), chMoney , Toast.LENGTH_SHORT).show();

            id_text.setText(id);
            name_edit.setText(contacts.getName());
            phone_edit.setText(contacts.getPhno());
            jacode_edit.setText(contacts.getJacode());
           balance_edit.setText(contacts.getBalance());
           rent_edit.setText(rent);
           zone_edit.setText(zone);
          // plan_edit.setText(contacts.getCplan());

           // balance_edit.setText(balance);

           // paidDate_edit.setText(contacts.getPaidDate());
            paidDate_edit.setText(Lpdate);
           // paidAmount_edit.setText(contacts.getPaidAmount());
            paidAmount_edit.setText(LpAmount);

           // LastPaidDate.setText(contacts.getPaidDate());
            LastPaidDate.setText(Lpdate);
           //LastPaidAmount.setText(contacts.getPaidAmount());
            LastPaidAmount.setText(LpAmount);

            name_text.setText(contacts.getName());
            phone_text.setText(contacts.getPhno());
            jacode_text.setText(contacts.getJacode());
            balance_text.setText(contacts.getBalance());
            rent_text.setText(rent);
            zone_text.setText(zone);
            plan_text.setText(contacts.getCplan());
            //balance_text.setText(balance);

            header = name_edit.getText().toString();

        }
        pkgname.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                SqliteDatabase mydb = new SqliteDatabase(UserDetailsActivity.this);
                SpinnerData = (String) adapterView.getItemAtPosition(i).toString();
                planAmount = mydb.getPlanAmount(SpinnerData);
             //   Toast.makeText(getApplicationContext(),planAmount,Toast.LENGTH_SHORT).show();
                rent_edit.setText(planAmount);
                rent_text.setText(planAmount);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        String active ="Active";
        String deac = "Deactive";
        if (Status.equals(active) == true){
            Activation.setVisibility(View.GONE);
            Deactivation.setVisibility(View.VISIBLE);

            ACTIVE_CODE =1;

        }
        else {
            ACTIVE_CODE =2;
            Activation.setVisibility(View.VISIBLE);
            Deactivation.setVisibility(View.GONE);
        }

        Activation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy - hh:mm:ss");

               String timeStamps = simpleDateFormat.format(new Date()) ;
                String statu ="Active";
                String stats = "Activated On ";
                String name = name_text.getText().toString();
                mDatabase.statusUpdate(id ,statu);
                mDatabase.statusDetails(id , name,stats,timeStamps);
                Status_text.setText(statu);
                Activation.setVisibility(View.GONE);
                Deactivation.setVisibility(View.VISIBLE);
            }
        });
        Deactivation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String statu ="Deactive";
                String stats = "DeActivated On ";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy - hh:mm:ss");
                String timeStamps = simpleDateFormat.format(new Date()) ;
                String name = name_text.getText().toString();
                mDatabase.statusUpdate(id ,statu);
                mDatabase.statusDetails(id , name,stats,timeStamps);
                Status_text.setText(statu);
                Activation.setVisibility(View.VISIBLE);
                Deactivation.setVisibility(View.GONE);
            }
        });
    }

    private void AddChannel(UserDetailsActivity activity) {

        dialog = new Dialog(activity);
        // dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.channel_selection);
        myDB = new  SqliteDatabase(this);

      //  myDB.getChannel();

        channel_id = new ArrayList<>();
        channel_name = new ArrayList<>();
        channel_ntMoney = new ArrayList<>();
        channel_tMoney = new ArrayList<>();

        Cursor cursor = myDB.getChannel();
        if (cursor.getCount()==0){
            Toast.makeText(this, "No Plans Have Added yet ", Toast.LENGTH_SHORT).show();
        }else {
            while (cursor.moveToNext()){
                channel_id.add(cursor.getString(0));
                channel_name.add(cursor.getString(1));
                channel_ntMoney.add(cursor.getString(2));
                channel_tMoney.add(cursor.getString(3));
            }
        }

        RecyclerView recyclerView = dialog.findViewById(R.id.channelRecyclerView);
        ChannelAdapter cAdapter = new ChannelAdapter(UserDetailsActivity.this,this,channel_id,channel_name,channel_ntMoney,channel_tMoney);

        recyclerView.setAdapter(cAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

        dialog.show();

    }

    private void editTask() {

        final String name = name_edit.getText().toString();
        final String id = id_text.getText().toString();
        final String rent = rent_edit.getText().toString();
        final String zone = zone_edit.getText().toString();
        final String ph_no = phone_edit.getText().toString();
        final String jacode = jacode_edit.getText().toString();
        final String balance = balance_edit.getText().toString();
        final String plan = SpinnerData;

        if (TextUtils.isEmpty(name)) {
            Toast.makeText(getApplicationContext(),"Something Went Wrong",Toast.LENGTH_SHORT).show();
        } else {
            // Contacts newContact = new Contacts(Objects.requireNonNull(contacts).getId(),name, ph_no,jacode,balance);
            //mDatabase.updateContacts(newContact);
            mDatabase.updateContacts(id,name, ph_no,jacode,balance,rent,zone,plan);

            name_edit.setText(name);name_text.setText(name);
            phone_edit.setText(ph_no);phone_text.setText(ph_no);
            rent_text.setText(rent);rent_edit.setText(rent);
            jacode_edit.setText(jacode);jacode_text.setText(jacode);
            balance_edit.setText(balance);balance_text.setText(balance);
            zone_text.setText(zone);zone_edit.setText(zone);
            plan_text.setText(plan);//pkgname.set(plan);

            name_edit.setVisibility(View.GONE);
            phone_edit.setVisibility(View.GONE);
            jacode_edit.setVisibility(View.GONE);
            rent_edit.setVisibility(View.GONE);
            balance_edit.setVisibility(View.GONE);
            paidAmount_edit.setVisibility(View.GONE);
            paidDate_edit.setVisibility(View.GONE);
            btndelete.setVisibility(View.GONE);
            btnEdit.setVisibility(View.GONE);
            zone_edit.setVisibility(View.GONE);
            pkgname.setVisibility(View.GONE);

            name_text.setVisibility(View.VISIBLE);
            rent_text.setVisibility(View.VISIBLE);
            zone_text.setVisibility(View.VISIBLE);
            phone_text.setVisibility(View.VISIBLE);
            jacode_text.setVisibility(View.VISIBLE);
            balance_text.setVisibility(View.VISIBLE);
            LastPaidAmount.setVisibility(View.VISIBLE);
            LastPaidDate.setVisibility(View.VISIBLE);
            btnRecharge.setVisibility(View.VISIBLE);
            plan_text.setVisibility(View.VISIBLE);
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void rechargeUser() {

        final AlertDialog builder = new AlertDialog.Builder(this).create();
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.recharge,null);

        builder.setTitle("Recharge " + header);
        final  EditText rechargeField = (EditText) dialogView.findViewById(R.id.rechargeEditText);
        final Button cancel = (Button) dialogView.findViewById(R.id.btnCancelRecharge);
        recharges = (Button) dialogView.findViewById(R.id.btnRechargeSucess);
        recharges.setEnabled(false);
        if(checkPermission(Manifest.permission.SEND_SMS))
        {
            recharges.setEnabled(true);
        }
        else
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQ);
        }

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(), "Cancelled Recharge !!",Toast.LENGTH_LONG).show();

                builder.dismiss();

            }
        });

        recharges.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                  d1 = Integer.valueOf(rechargeField.getText().toString().trim());
                  d2 = Integer.valueOf(balance_edit.getText().toString().trim());

                amount= d2 - d1;
                data1 = String.valueOf(amount);
                data2 = String.valueOf(d1);

                String name = String.valueOf(name_text.getText().toString().trim());

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy - hh:mm:ss");
                SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
                timeStamp = simpleDateFormat.format(new Date()) ;
                String cdate = date.format(new Date());
                Calendar calendar = Calendar.getInstance();
                String month = calendar.getDisplayName(Calendar.MONTH,Calendar.LONG, Locale.UK);
                int year = Calendar.getInstance().get(Calendar.YEAR);
                String due= balance_text.getText().toString().trim();
                String paid = String.valueOf(d1);

                //Toast.makeText(getApplicationContext(),month,Toast.LENGTH_SHORT).show();

               // Toast.makeText(getApplicationContext(),String.valueOf(year),Toast.LENGTH_SHORT).show();
                mDatabase.rechargeUser(id, (int) amount,d1,timeStamp);
                mDatabase.InsertPaymentDetails(id,month,year,timeStamp,paid,due);
                mDatabase.collectionDetails(id,name,d1,cdate,month,year);
                reloadData();
                sendSms();
                builder.dismiss();

            }
        });

        builder.setView(dialogView);
        builder.show();

    }

    private void sendSms() {
        String names = name_text.getText().toString();

        String s1=phone_text.getText().toString();
        String s2=("Hi "+names+", You have Successfully recharged ₹ " +d1 +" to your SKY VISION Cable account  on "+timeStamp+". Your current balance is ₹ "+amount+" . Kindly clear your balance on time without any due. \n - Thank you! \n SKY VISION KONGAD");
        if(!TextUtils.isEmpty(s1)&&!TextUtils.isEmpty(s2))
        {

            if(checkPermission(Manifest.permission.SEND_SMS))
            {
                SmsManager smsManager= SmsManager.getDefault();
                ArrayList<String> part = smsManager.divideMessage(s2);

                smsManager.sendMultipartTextMessage(s1,null,part,null,null);
            }
            else {
                Toast.makeText(UserDetailsActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            Toast.makeText(UserDetailsActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
        }
    }

    private void reloadData() {
        balance_text.setText(data1);
        balance_edit.setText(data1);
        LastPaidAmount.setText(data2);
        LastPaidDate.setText(timeStamp);
        paidAmount_edit.setText(data2);
        paidDate_edit.setText(timeStamp);

    }

    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.user_details, menu);
        return super.onCreateOptionsMenu(menu);

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.payment_details:
                Intent intent = new Intent(this,PaymentDetailsActivity.class);
                intent.putExtra("ID",id);
                startActivity(intent);
                break;
            case R.id.WriteOff:
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Write Off?");
                builder.setMessage("Are you sure you want to Write off ?");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        WriteOff();
                    }
                });

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.create().show();
                break;

            case  R.id.boxDetails:
                Intent boxDetails = new Intent(this,BoxDetailsActivity.class);
                boxDetails.putExtra("ID",id);
                startActivity(boxDetails);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void WriteOff() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy - hh:mm:ss");

        timeStamp = simpleDateFormat.format(new Date()) ;
        Calendar calendar = Calendar.getInstance();
        String month = calendar.getDisplayName(Calendar.MONTH,Calendar.LONG, Locale.UK);
        int year = Calendar.getInstance().get(Calendar.YEAR);
        String due= balance_text.getText().toString().trim();
        String paid = "Write Off";
        balance_text.setText("0");
        balance_edit.setText("0");
        String Writeofbalance ="0";

        mDatabase.writeOff(id,  month ,  year ,  timeStamp ,  paid ,  due,  Writeofbalance);
    }

    private boolean checkPermission(String sendSms) {

        int checkpermission= ContextCompat.checkSelfPermission(this,sendSms);
        return checkpermission== PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode)
        {
            case SEND_SMS_PERMISSION_REQ:
                if(grantResults.length>0 &&(grantResults[0]== PackageManager.PERMISSION_GRANTED))
                {
                    btnRecharge.setEnabled(true);

                }
                break;
        }
    }

    private void loadSpinnerData() {
        SqliteDatabase db = new SqliteDatabase(getApplicationContext());
        List<String> labels = db.getAllPlansSpinner();

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,labels);

        arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        pkgname.setAdapter(arrayAdapter);

    }

ArrayList<String> channel_id,channel_name,channel_ntMoney,channel_tMoney;
    ChannelAdapter cAdapter;
    public static Dialog dialog;

}

这是我的通道适配器活动
channeladapteractivity.java

package com.ancorp.aplo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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

import com.ancorp.aplo.R;
import com.ancorp.aplo.activity.UserDetailsActivity;
import com.ancorp.aplo.model.Contacts;

import java.util.ArrayList;

public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.MyViewHolder> {
    private Context context;
    private Activity activity;
    private  ArrayList<Contacts> allChannels;
    String id,name,money;
    private ArrayList  channel_id,channel_name,channel_ntMoney,channel_tMoney;
    public ChannelAdapter(Activity activity, Context context, ArrayList<String> channel_id, ArrayList<String> channel_name, ArrayList<String> channel_ntMoney, ArrayList<String> channel_tMoney) {

        this.context = context;
        this.activity = activity;
        this.channel_id = channel_id;
        this.channel_name=channel_name;
        this.channel_ntMoney= channel_ntMoney;
        this.channel_tMoney= channel_tMoney;

    }

    public ChannelAdapter(Context context, ArrayList<Contacts> allChannel) {

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater .inflate(R.layout.channel_list,viewGroup,false);
        return new MyViewHolder(view);
    }

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

        holder.channelName.setText(String.valueOf(channel_name.get(i)));
        holder.channelNt.setText(String.valueOf(channel_ntMoney.get(i)));
        holder.channelT.setText(String.valueOf(channel_tMoney.get(i)));
        id = String.valueOf(channel_id.get(i));
        name=String.valueOf(channel_name.get(i));
        money = String.valueOf(channel_tMoney.get(i));

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView channelName,channelNt,channelT;
        LinearLayout channelLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            channelName = itemView.findViewById(R.id.channelName);
            channelNt = itemView.findViewById(R.id.ntMoney);
            channelT = itemView.findViewById(R.id.tMoney);
            channelLayout = itemView.findViewById(R.id.channelMainLayout);

        }
    }
}

谁来帮帮我,我是新手。

gudnpqoy

gudnpqoy1#

这是将数据从适配器发送到活动的另一种方法。请仔细遵循以下步骤:
使用方法创建接口

public interface InterfaceTax {

    public void getTaxMoney(int amount);

}

方法的参数应该包含要发送回活动的数据。
在适配器中为该接口创建一个变量,并将该接口添加到适配器的构造函数中。

private InterfaceTax interfaceT;
public ChannelAdapter(/* other parameters */, InterfaceTax interface) {
    interfaceT = interface;
}

然后在onbindviewholder(或数据所在的任何地方)中调用接口的方法并发送数据

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
    money = String.valueOf(channel_tMoney.get(i));
    interface.getTaxMoney(money);
}

然后在活动中实现接口并重写方法以接收数据

public class UserDetailsActivity extends AppCompatActivity implements InterfaceTax {}
@Override
public void getTaxMonney(int data) {
    // receive data here
}

在addchannel方法中,替换此行

ChannelAdapter cAdapter = new ChannelAdapter(UserDetailsActivity.this,this,channel_id,channel_name,channel_ntMoney,channel_tMoney);

用这个

ChannelAdapter cAdapter = new ChannelAdapter(UserDetailsActivity.this,this,channel_id,channel_name,channel_ntMoney,channel_tMoney, this);

这将自动将数据从适配器发送到活动,并且您可以在活动中重写的方法中接收数据。

相关问题