如何通过扫描仪将数据添加到TableLayout?

时间:2018-09-24 18:59:11

标签: android android-layout android-studio zxing barcode-scanner

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

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

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

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);
    }
}
}

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

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;
}
}

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

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

在我的Build Gradle中,扫描仪具有以下依赖性

{{1}}

1 个答案:

答案 0 :(得分:1)

您是否可以尝试在类内创建一个新类,而不是在implements ZXingScannerView.ResultHandler类中添加MainActivity,然后创建该类的属性,例如: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显示的是正确的内容?