从另一个类获取按钮调用。

huangapple go评论66阅读模式
英文:

Get button call from another class

问题

我有两个类`OrderAdapter.java``OrderActivity.java`。在类`OrderAdapter.java`中有一个按钮当我点击它时它应该更改类`OrderActivity.java`中的一个文本视图当我运行应用程序时文本视图会因为这一行而改变

```java
textView_order_price.setText(adapter.totalAmount + "");

但是当我再次点击按钮时,它不应该再次更改文本,但实际上没有发生变化。你能帮我解决这个问题吗?

OrderActivity.java

package com.nfc.netvision;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class OrderActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    TextView textView_order_price;
    ArrayList<ModelOrder> orderArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        recyclerView = findViewById(R.id.recyclerview_order_scroll);
        textView_order_price = findViewById(R.id.textView_order_price);

        orderArrayList = new ArrayList<>();
        orderArrayList.add(new ModelOrder(R.drawable.coke, "Coka Cola", "Eine Cola hält dich wach und schmeckt dazu.", "3", 0));
        orderArrayList.add(new ModelOrder(R.drawable.fastfood, "Pommes", "Fritten für die Titten.", "5", 0));
        orderArrayList.add(new ModelOrder(R.drawable.water, "Wasser", "Still und sanft, so mag ich es.", "5", 0));
        orderArrayList.add(new ModelOrder(R.drawable.burger, "Burger", "Ach mir fällt nichts ein.", "10", 0));

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        RecyclerView.LayoutManager recLiLayoutManager = layoutManager;

        recyclerView.setLayoutManager(recLiLayoutManager);

        OrderAdapter adapter = new OrderAdapter(this, orderArrayList);

        recyclerView.setAdapter(adapter);
        textView_order_price.setText(adapter.totalAmount + "");
        System.out.println(adapter.totalAmount + "TOASBTORT");

    }
}

OrderAdapter.java

package com.nfc.netvision;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firestore.v1.StructuredQuery;
import java.util.ArrayList;
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
    public int totalAmount;
    private Context mContext;
    private ArrayList<ModelOrder> nList;
    OrderAdapter(Context context, ArrayList<ModelOrder> list) {
        mContext = context;
        nList = list;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.recyclerview_order_items, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final ModelOrder orderItem = nList.get(position);
        ImageView image = holder.item_image;
        final TextView name, place, price;
        name = holder.item_name;
        place = holder.item_place;
        price = holder.item_price;
        image.setImageResource(orderItem.getImage());
        name.setText(orderItem.getName());
        place.setText(orderItem.getPlace());
        price.setText(orderItem.getPrice());
        holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(orderItem.getCounter() > 0) {
                    orderItem.setCounter(orderItem.getCounter()-1);
                    holder.order_item_count.setText("" + orderItem.getCounter());
                    calculatePrice(Integer.parseInt((String) price.getText()), false);
                }
            }
        });
        holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(orderItem.getCounter() < 9) {
                    orderItem.setCounter(orderItem.getCounter() + 1);
                    holder.order_item_count.setText("" + orderItem.getCounter());
                    calculatePrice(Integer.parseInt((String) price.getText()), true);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return nList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView item_image;
        TextView item_name, item_place, item_price, order_item_minus, order_item_count, order_item_plus;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            item_image = itemView.findViewById(R.id.order_item_image);
            item_name = itemView.findViewById(R.id.order_item_name);
            item_place = itemView.findViewById(R.id.order_item_place);
            item_price = itemView.findViewById(R.id.order_item_price);
            order_item_minus = itemView.findViewById(R.id.order_item_minus);
            order_item_plus = itemView.findViewById(R.id.order_item_plus);
            order_item_count = itemView.findViewById(R.id.order_item_count);
        }
    }
    public void calculatePrice(int pPrice, boolean pUpDown) {
        if(pUpDown) {
            totalAmount = totalAmount + pPrice;
        }
        else {
            totalAmount = totalAmount - pPrice;
        }
        System.out.println(totalAmount);
    }
}
英文:

I have the two classes OrderAdapter.java OrderActivity.java. In the class OrderAdapter.java is a button and when i click it has to change a textview in the class OrderActivity.java. When i run the app the textview changes one time because of this line which is intend:
textView_order_price.setText(adapter.totalAmount + &quot;&quot;);

but when i press the button again it should change the text again but it doesn't. Can you guys help me?

OrderActivity.java

package com.nfc.netvision;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
public class OrderActivity extends AppCompatActivity {
RecyclerView recyclerView;
TextView textView_order_price;
ArrayList&lt;ModelOrder&gt; orderArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
recyclerView = findViewById(R.id.recyclerview_order_scroll);
textView_order_price = findViewById(R.id.textView_order_price);
orderArrayList = new ArrayList&lt;&gt;();
orderArrayList.add(new ModelOrder(R.drawable.coke, &quot;Coka Cola&quot;, &quot;Eine Cola h&#228;lt dich wach und schmeckt dazu.&quot;, &quot;3&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, &quot;Pommes&quot;, &quot;Fritten f&#252;r die Titten.&quot;, &quot;5&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.water, &quot;Wasser&quot;, &quot;Still und sanft, so mag ich es.&quot;, &quot;5&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.burger, &quot;Burger&quot;, &quot;Ach mir f&#228;llt nichts ein.&quot;, &quot;10&quot;,0));
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView.LayoutManager recLiLayoutManager = layoutManager;
recyclerView.setLayoutManager(recLiLayoutManager);
OrderAdapter adapter = new OrderAdapter(this, orderArrayList);
recyclerView.setAdapter(adapter);
textView_order_price.setText(adapter.totalAmount + &quot;&quot;);
System.out.println(adapter.totalAmount + &quot;TOASBTORT&quot;);
}
}

OrderAdapter.java

package com.nfc.netvision;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firestore.v1.StructuredQuery;
import java.util.ArrayList;
public class OrderAdapter extends RecyclerView.Adapter&lt;OrderAdapter.ViewHolder&gt; {
public int totalAmount;
private Context mContext;
private ArrayList&lt;ModelOrder&gt; nList;
OrderAdapter(Context context, ArrayList&lt;ModelOrder&gt; list) {
mContext = context;
nList = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.recyclerview_order_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final ModelOrder orderItem = nList.get(position);
ImageView image = holder.item_image;
final TextView name, place, price;
name = holder.item_name;
place = holder.item_place;
price = holder.item_price;
image.setImageResource(orderItem.getImage());
name.setText(orderItem.getName());
place.setText(orderItem.getPlace());
price.setText(orderItem.getPrice());
holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(orderItem.getCounter() &gt; 0) {
orderItem.setCounter(orderItem.getCounter()-1);
holder.order_item_count.setText(&quot;&quot; + orderItem.getCounter());
calculatePrice(Integer.parseInt((String) price.getText()), false);
}
}
});
holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(orderItem.getCounter() &lt; 9) {
orderItem.setCounter(orderItem.getCounter() + 1);
holder.order_item_count.setText(&quot;&quot; + orderItem.getCounter());
calculatePrice(Integer.parseInt((String) price.getText()), true);
}
}
});
}
@Override
public int getItemCount() {
return nList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView item_image;
TextView item_name, item_place, item_price,order_item_minus,order_item_count, order_item_plus;
public ViewHolder(@NonNull View itemView) {
super(itemView);
item_image = itemView.findViewById(R.id.order_item_image);
item_name = itemView.findViewById(R.id.order_item_name);
item_place = itemView.findViewById(R.id.order_item_place);
item_price = itemView.findViewById(R.id.order_item_price);
order_item_minus = itemView.findViewById(R.id.order_item_minus);
order_item_plus = itemView.findViewById(R.id.order_item_plus);
order_item_count = itemView.findViewById(R.id.order_item_count);
}
}
public void calculatePrice(int pPrice, boolean pUpDown) {
if(pUpDown) {
totalAmount = totalAmount + pPrice;
}
else {
totalAmount = totalAmount - pPrice;
}
System.out.println(totalAmount);
}
}

答案1

得分: 0

2种解决问题的方法:

第一种方法 - 非常简单 - 只需将您的textView从活动添加到构造适配器中;

private TextView orderPrice;

OrderAdapter(Context context, ArrayList<ModelOrder> list, TextView orderPrice) {
    mContext = context;
    nList = list;
    this.orderPrice = orderPrice;
}

public void calculatePrice(int pPrice, boolean pUpDown) {
    if (pUpDown) {
        totalAmount = totalAmount + pPrice;
    } else {
        totalAmount = totalAmount - pPrice;
    }
    // 在这里将totalAmount添加到您的活动中的textView
    orderPrice.setText(Integer.toString(totalAmount));
    System.out.println(totalAmount);
}

第二种方法,在适配器中创建接口,并在活动中实现它;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
    // ... 其他代码 ...

    interface TotalListener {
        void onTotalChanged(String result);
    }

    OrderAdapter(Context context, ArrayList<ModelOrder> list, TotalListener listener) {
        mContext = context;
        nList = list;
        this.listener = listener;
    }

    // ... 其他代码 ...

    public void calculatePrice(int pPrice, boolean pUpDown) {
        // ... 计算逻辑 ...

        // 这是回调到您的活动
        listener.onTotalChanged(Integer.toString(totalAmount));
        System.out.println(totalAmount);
    }
}

// ... 其他代码 ...

public class OrderActivity extends AppCompatActivity implements OrderAdapter.TotalListener {
    // ... 其他代码 ...

    @Override
    public void onTotalChanged(String result) {
        textView_order_price.setText(result);
    }
}
英文:

2 ways to solve your problem:

1st way - very simple - just add your textView from activity to constructor adapter;

private TextView orderPrice;
OrderAdapter(Context context, ArrayList&lt;ModelOrder&gt; list,TextView orderPrice) {
mContext = context;
nList = list;
this.orderPrice = orderPrice;
}
public void calculatePrice(int pPrice, boolean pUpDown) {
if(pUpDown) {
totalAmount = totalAmount + pPrice;
}
else {
totalAmount = totalAmount - pPrice;
}
//here you add totalAmount to your textView in activity
orderPrice.setText(totalAmount.toString)
System.out.println(totalAmount);
}	

2d way create interface in your adapter and implement it in your activity;

//1st create interface in your adapter like this:

public class OrderAdapter extends 
RecyclerView.Adapter&lt;OrderAdapter.ViewHolder&gt; {
public int totalAmount;
private Context mContext;
private ArrayList&lt;ModelOrder&gt; nList;
private TotalListener listener;
interface TotalListener{
void onTotalChanged(String result);
}
OrderAdapter(Context context, ArrayList&lt;ModelOrder&gt; list,TotalListener listener) {
mContext = context;
nList = list;
this.listener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.recyclerview_order_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final ModelOrder orderItem = nList.get(position);
ImageView image = holder.item_image;
final TextView name, place, price;
name = holder.item_name;
place = holder.item_place;
price = holder.item_price;
image.setImageResource(orderItem.getImage());
name.setText(orderItem.getName());
place.setText(orderItem.getPlace());
price.setText(orderItem.getPrice());
holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(orderItem.getCounter() &gt; 0) {
orderItem.setCounter(orderItem.getCounter()-1);
holder.order_item_count.setText(&quot;&quot; + orderItem.getCounter());
calculatePrice(Integer.parseInt((String) price.getText()), false);
}
}
});
holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(orderItem.getCounter() &lt; 9) {
orderItem.setCounter(orderItem.getCounter() + 1);
holder.order_item_count.setText(&quot;&quot; + orderItem.getCounter());
calculatePrice(Integer.parseInt((String) price.getText()), true);
}
}
});
}
@Override
public int getItemCount() {
return nList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView item_image;
TextView item_name, item_place, item_price,order_item_minus,order_item_count, order_item_plus;
public ViewHolder(@NonNull View itemView) {
super(itemView);
item_image = itemView.findViewById(R.id.order_item_image);
item_name = itemView.findViewById(R.id.order_item_name);
item_place = itemView.findViewById(R.id.order_item_place);
item_price = itemView.findViewById(R.id.order_item_price);
order_item_minus = itemView.findViewById(R.id.order_item_minus);
order_item_plus = itemView.findViewById(R.id.order_item_plus);
order_item_count = itemView.findViewById(R.id.order_item_count);
}
}
public void calculatePrice(int pPrice, boolean pUpDown) {
if(pUpDown) {
totalAmount = totalAmount + pPrice;
}
else {
totalAmount = totalAmount - pPrice;
}
//this is callback to your activity
listener.onTotalChanged(totalAmount.toString)
System.out.println(totalAmount);
}

}

and this is your updated activity

 public class OrderActivity extends AppCompatActivity impelemts OrderAdapter.TotalListener {
RecyclerView recyclerView;
TextView textView_order_price;
ArrayList&lt;ModelOrder&gt; orderArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
recyclerView = findViewById(R.id.recyclerview_order_scroll);
textView_order_price = findViewById(R.id.textView_order_price);
orderArrayList = new ArrayList&lt;&gt;();
orderArrayList.add(new ModelOrder(R.drawable.coke, &quot;Coka Cola&quot;, &quot;Eine Cola h&#228;lt dich wach und schmeckt dazu.&quot;, &quot;3&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, &quot;Pommes&quot;, &quot;Fritten f&#252;r die Titten.&quot;, &quot;5&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.water, &quot;Wasser&quot;, &quot;Still und sanft, so mag ich es.&quot;, &quot;5&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.burger, &quot;Burger&quot;, &quot;Ach mir f&#228;llt nichts ein.&quot;, &quot;10&quot;,0));
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView.LayoutManager recLiLayoutManager = layoutManager;
recyclerView.setLayoutManager(recLiLayoutManager);
OrderAdapter adapter = new OrderAdapter(this, orderArrayList,this);
recyclerView.setAdapter(adapter);
textView_order_price.setText(adapter.totalAmount + &quot;&quot;);
System.out.println(adapter.totalAmount + &quot;TOASBTORT&quot;);
}
@Override
public void onTotalChanged(String result) {
textView_order_price.setText(result);
}

}

答案2

得分: 0

将此侦听器添加到您的适配器类中,当单击按钮监听器时调用:

private OnButtonClickListener listener;

public void setOnButtonClickListener(OnButtonClickListener listener){
    this.listener = listener;
}

public interface OnButtonClickListener{
    void OnButtonClick();
}

在适配器的按钮点击事件中:

listener.OnButtonClick();

在您的活动类中:

OrderAdapter adapter = new OrderAdapter(this, orderArrayList);
adapter.setOnButtonClickListener(new OnButtonClickListener () {
    @Override
    public void OnButtonClick() {
        // 在这里您可以对文本视图进行任何操作
    }
});
英文:

add this listener to your adpater class and when you clik button listner call

private OnButtonClickListener listener;
public void setOnButtonClickListener(OnButtonClickListener listener){
this.listener=listener;
}
public interface OnButtonClickListener{
void OnButtonClick();
}

in Button click in your adapter :

listener.OnButtonClick();

in your activty class

OrderAdapter adapter = new OrderAdapter(this, orderArrayList);
adapter.setOnButtonClickListener(new OnButtonClickListener () {
@Override
public void OnButtonClick() {
//here you can do any thing on your text view 
}
});

huangapple
  • 本文由 发表于 2020年5月5日 21:28:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61614333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定