如何在另一个TextView中的RecyclerView设置文本



创建了一个RecyclerView适配器。当我添加一个新项目时,我需要在两个TextView中设置文本。在第一个TextView(android:id="@+id/text")我可以设置文本没有任何问题。我如何设置文本在TextView(android:id="@+id/textCost")方法:

private double SumCost(String result){
switch (result){
case "4820049490145":
return 2.34;
default: return 0;
}
}

在这里输入图像描述因为String result我是从ScanPage得到的。谢谢你的帮助。

我的完整代码:activity_scan_page.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="10dp">

<TextView
android:id="@+id/SumText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="0.00"
android:textSize="30sp"/>
<Button
android:id="@+id/scanBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan now"
android:textSize="30sp"
android:layout_gravity="center_horizontal"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>

</LinearLayout>

。:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:contentPadding="5dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/app_name"
android:textSize="20sp" />
<TextView
android:id="@+id/textCost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:text="@string/app_name"
android:textSize="20sp" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="3dp"
android:layout_marginEnd="3dp"
android:text="Delete" />
</RelativeLayout>
</androidx.cardview.widget.CardView>

ScanPage.java(我在这里得到IntentResult result,并将android:id="@+id/text"设置为items.add(result);):

package com.example.scanapp;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ActionBar;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.util.LinkedList;
import java.util.List;
public class ScanPage extends AppCompatActivity implements View.OnClickListener{

Button scanBtn;
TextView editText;
List<String> items = new LinkedList<>();

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

scanBtn = findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(this);
}
public void AddItem(String result){          
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DemoAdapter adapter = new DemoAdapter(items);
recyclerView.setAdapter(adapter);
items.add(result);
}

@Override
public void onClick(View view) {
scanCode();
}
private  void scanCode(){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CaptureAct.class);
integrator.setOrientationLocked(false);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scanning code");
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents() != null){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(result.getContents());
builder.setTitle("Scanning Result");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
///
AddItem(result.getContents());
///
editText = (TextView) findViewById(R.id.SumText);
editText.setText(Cost(SumCost(result.getContents())));
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else {
Toast.makeText(this,"No result", Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
////////
double SumVar = 0;

private double SumCost(String result){
switch (result){
case "4820049490145":
return 2.34;
default: return 0;
}
}
private String Cost(double cost){
SumVar += cost;
return Double.toString(SumVar);
}
////////
}

DemoAdapter.java:

package com.example.scanapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class DemoAdapter extends RecyclerView.Adapter<DemoVH>{
List<String> items;

public DemoAdapter(List<String> items){
this.items = items;
}
@NonNull
@Override
public DemoVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new DemoVH(view).linkAdapter(this);
}
@Override
public void onBindViewHolder(@NonNull DemoVH holder, int position) {
holder.textView.setText(items.get(position));
}
@Override
public int getItemCount() {
return items.size();
}
}
class DemoVH extends RecyclerView.ViewHolder{
TextView textView;
private DemoAdapter adapter;
public DemoVH(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
itemView.findViewById(R.id.delete).setOnClickListener(view -> {
adapter.items.remove(getAdapterPosition());
adapter.notifyItemRemoved(getAdapterPosition());
});
}
public DemoVH linkAdapter(DemoAdapter adapter){
this.adapter = adapter;
return this;
}
}

你必须在你的AddItem()方法中调用notifyItemInserted():

...
items.add(result);
notifyItemInserted(position)
}

相关内容

  • 没有找到相关文章

最新更新