gpt4 book ai didi

android - 如何通过 Scanner 向 TableLayout 添加数据?

转载 作者:行者123 更新时间:2023-11-29 00:59:00 25 4
gpt4 key购买 nike

我是 Android 应用程序开发的新手。我正在尝试通过扫描仪将数据添加到表中,问题是当我启动扫描仪时它读取条形码但我无法将该值添加到我的表中。但是,如果我在不打开扫描仪的情况下添加数据,我的表就可以正常工作,我有以下代码,有人可以支持我。谢谢。

在设计方面,我有以下内容:

*1- TextView *2- EditText *2- 按钮 *1- TableLayout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/scan_format"
android:hint="muestra datos del codigo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/scan_button" />
<EditText
android:id="@+id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="DATO1" />

<EditText
android:id="@+id/txtlastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="DATO2" />

<Button
android:id="@+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="Escanear"
android:text="ABRIR ESCANER" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="AGREGAR DATOS A LA TABLA" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent">

</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>

在我的 Activity_main 中,我有以下代码。

import com.google.zxing.Result;
import java.util.ArrayList;
import java.util.StringTokenizer;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity {

Button scanBtn;
TextView formatTxt, contentTxt;
ZXingScannerView vistaescaner;

TableLayout tableLayout;
EditText txtName, txtLastName;
Button btn1;
String[]header={"Id","Nombre","Apellido"};
ArrayList<String[]> rows = new ArrayList<>();
TableDynamic tableDynamic;
String _a,_b;

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

tableLayout=(TableLayout)findViewById(R.id.table);
txtName=(EditText)findViewById(R.id.txtname);
txtLastName=(EditText)findViewById(R.id.txtlastname);
btn1=(Button)findViewById(R.id.button);


tableDynamic = new TableDynamic(tableLayout,getApplicationContext());
tableDynamic.addHeader(header);
tableDynamic.addData(getClients());
tableDynamic.backgroundHeader(Color.BLUE);
tableDynamic.backgroundData(Color.parseColor("#58D3F7"),Color.WHITE);
tableDynamic.lineColor(Color.BLACK);
tableDynamic.textColorhHeader(Color.WHITE);
tableDynamic.textColorData(Color.parseColor("#000000"));

contentTxt = (TextView)findViewById(R.id.scan_format);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_a=txtName.getText().toString();
_b=txtLastName.getText().toString();
String[] item = new String[]{"2",_a,_b};
tableDynamic.addItems(item);
}
});
}
public ArrayList<String[]> getClients() {
rows.add(new String[]{"PRUEBA", "DE", "QUE LLENA"});
//rows.add(new String[]{"2","b","Lopez"});

return rows;
}

public void Escanear(View view){
vistaescaner = new ZXingScannerView(MainActivity.this);
vistaescaner.setResultHandler(new zxingscanner());
setContentView(vistaescaner);
vistaescaner.startCamera();
}
public class zxingscanner implements ZXingScannerView.ResultHandler{
@Override
public void handleResult(Result result) {
String dato = result.getText();
//setContentView(R.layout.activity_main);
vistaescaner.stopCamera();
formatTxt = (TextView)findViewById(R.id.scan_format);
formatTxt.setText(dato);
String s = dato;
StringTokenizer st = new StringTokenizer(s, ",");
dato = st.nextToken();
String[] item = new String[]{dato,"prueba","pruebaa"};
tableDynamic.addItems(item);
}
}
}

我有一个名为 Table Dynamic 的类负责填充表格。

public class TableDynamic {
private TableLayout tableLayout;
private Context context;
private String[]header;
private ArrayList<String[]>data;
private TableRow tableRow;
private TextView txtCell;
private int indexC;
private int indexR;
private boolean multicolor=false;
int firstColor, secondColor,textColor;


public TableDynamic(TableLayout tableLayout, Context context){
this.tableLayout=tableLayout;
this.context=context;
}
public void addHeader(String[]header){
this.header=header;
createHeader();

}

public void addData(ArrayList<String[]>data){
this.data=data;
createDataTable();
}

private void newRow(){
tableRow=new TableRow(context);
}
private void newCell(){
txtCell= new TextView(context);
txtCell.setGravity(Gravity.CENTER);
txtCell.setTextSize(25);
}
private void createHeader(){
indexC=0;
newRow();
while (indexC<header.length){
newCell();
txtCell.setText(header[indexC++]);
tableRow.addView(txtCell,newTableRowParams());
}
tableLayout.addView(tableRow);
}
private void createDataTable(){
String info;
for(indexR=1;indexR<=data.size();indexR++){
newRow();
for(indexC=0;indexC<header.length;indexC++){
newCell();
String[] row = data.get(indexR-1);
info=(indexC<row.length)?row[indexC]:"";
txtCell.setText(info);
tableRow.addView(txtCell,newTableRowParams());
}

tableLayout.addView(tableRow);
}
}

public void addItems(String[]Item){
String info;
data.add(Item);
indexC=0;
newRow();
while (indexC<header.length){
newCell();
info=(indexC<Item.length)?Item[indexC++]:"";
txtCell.setText(info);
tableRow.addView(txtCell,newTableRowParams());
}
tableLayout.addView(tableRow,data.size());
reColor();
}

public void backgroundHeader(int color){
indexC=0;
while (indexC<header.length){
txtCell=getCell(0,indexC++);
txtCell.setBackgroundColor(color);
}
}

public void backgroundData(int firstColor, int secondColor){
for(indexR=1;indexR<=data.size();indexR++){
multicolor=!multicolor;
for(indexC=0;indexC<header.length;indexC++){
txtCell=getCell(indexR,indexC);
txtCell.setBackgroundColor((multicolor)?firstColor:secondColor);
}
}
this.firstColor=firstColor;
this.secondColor=secondColor;
}

public void lineColor(int color){
indexR=0;
while(indexR<data.size()){
getRow(indexR++).setBackgroundColor(color);
}
}

public void textColorData(int color){
for(indexR=1;indexR<=data.size();indexR++)
for(indexC=0;indexC<header.length;indexC++)
getCell(indexR,indexC).setTextColor(color);

this.textColor=color;
}

public void textColorhHeader(int color){
indexC=0;
while(indexC<header.length){
getCell(0,indexC++).setTextColor(color);
}
}

private void reColor(){
indexC=0;
multicolor=!multicolor;
while (indexC<header.length){
txtCell=getCell(data.size(),indexC++);
txtCell.setBackgroundColor((multicolor)?firstColor:secondColor);
txtCell.setTextColor(textColor);
}
}

private TableRow getRow(int index){
return (TableRow)tableLayout.getChildAt(index);
}

private TextView getCell(int rowindex, int columindex){
tableRow=getRow(rowindex);
return (TextView) tableRow.getChildAt(columindex);
}

private TableRow.LayoutParams newTableRowParams(){
TableRow.LayoutParams params= new TableRow.LayoutParams();
params.setMargins(1,1,1,1);
params.weight=1;
return params;
}
}

在我的 Build Gradle 中,我对扫描器有以下依赖性

implementation 'me.dm7.barcodescanner:zxing:1.9.8'

最佳答案

您能否尝试在您的 MainActivity 类中添加 implements ZXingScannerView.ResultHandler 而不是在您的类内部创建一个新类,然后创建该类的属性,例如: private ZXingScannerView mScannerView; 然后当你想扫描代码时做这样的事情:

mScannerView = new ZXingScannerView(this);
mScannerView.setResultHandler(this);
setContentView(mScannerView)
mScannerView.startCamera();

当您实现 ZXingScannerView.ResultHandler 时,您必须创建此方法:

@Override
public void handleResult(Result result) {
Toast.makeText(getApplicationContext(), result.getText(), Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
mScannerView.stopCamera();
}

尝试此代码后,您能否验证 Toast 是否显示了正确的内容?

关于android - 如何通过 Scanner 向 TableLayout 添加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52485686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com