简体   繁体   English

intent.putExtra传递空值

[英]intent.putExtra passes empty value

I am trying to develop a qr scanner where I want to show the history of scanned results to user. 我正在尝试开发一个二维扫描仪,我想在其中向用户显示扫描结果的历史记录。 Now although the scanned data values are passed to a different activity properly; 现在,尽管已将扫描的数据值正确传递给其他活动; the problem with it is even if I have not scanned the qr the empty data is passed to the listview of the next activity. 问题是,即使我没有扫描过qr,空数据也会传递到下一个活动的列表视图中。

My question is how do I make sure that only if the qr is scanned the data is passed to the next activity in the listview? 我的问题是我如何确保仅在扫描qr时才将数据传递到listview中的下一个活动?

This is the code for my barcode activity. 这是我的条形码活动的代码。

    public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
//Initialization
    private static final int REQUEST_CAMERA = 1;
    private ZXingScannerView mScannerView;
    ArrayList<String> listOfBarcodes = new ArrayList<String>();

//This is the part where I handle the result

     @Override
        public void handleResult(Result rawResult) {

            final String result = rawResult.getText();
            if(!result.isEmpty()) {
                //whenever you capture a new barcode add it to this list
                listOfBarcodes.add(result);
            }

PS: After this am handling the result to show in AlertDialog. PS:处理完要显示在AlertDialog中的结果之后。

The part where I am sending the data to next activity. 我将数据发送到下一个活动的部分。

 Intent data = new Intent( getApplicationContext(), History.class );
                if (listOfBarcodes!=null) {
                data.putExtra("Barcode", listOfBarcodes);
            }
                startActivity( data );

History.java History.java

public class History extends AppCompatActivity {
public static final ArrayList completeList = new ArrayList<String>();
TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        textView = findViewById(R.id.text_view);
        Intent callingIntent= getIntent();
        ArrayList<String> listOfBarcodes = callingIntent.getExtras().getStringArrayList("Barcode");
        completeList.add(listOfBarcodes);
        ArrayAdapter<String> aA = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, completeList);
        ListView lv = (ListView) findViewById(R.id.my_list_id);
        lv.setAdapter(aA);

Apart from this I also do get the warning in completeList.add(listOfBarcodes); 除此之外,我还收到completeList.add(listOfBarcodes);的警告completeList.add(listOfBarcodes);

Unchecked call to 'add(E)' as a member of raw type 'java.util.ArrayList' less... (Ctrl+F1) Signals places where an unchecked warning is issued by the compiler, for example: 原始类型为java.util.ArrayList的成员的对add(E)的未经检查的调用less ...(Ctrl + F1)指示编译器发出未经检查的警告的位置,例如:

void f(HashMap map) { map.put("key", "value"); void f(HashMap map){map.put(“ key”,“ value”); } }

In History.java the listview with empty value appears as []. History.java ,具有空值的列表视图显示为[]。 Is there a different method to pass the data or am I missing something here? 是否有其他方法可以传递数据,或者我在这里错过了什么?

You have to use putStringArrayListExtra() as you are sending ArrayList<String> . 发送ArrayList<String>必须使用putStringArrayListExtra() First make sure list listOfBarcodes has the data. 首先确保列表listOfBarcodes具有数据。

Intent data = new Intent( getApplicationContext(), History.class );
    if(listOfBarcodes.size()!=0)
    data.putStringArrayListExtra("Barcode",listOfBarcodes);
    startActivity(data);

In History Activity:- 在历史活动中:

 ListView lv = (ListView) findViewById(R.id.my_list_id);
    Intent callingIntent= getIntent();
    if(callingIntent.hasExtra("Barcode")) {
        ArrayList<String> listOfBarcodes = callingIntent.getStringArrayListExtra("Barcode");
        completeList.addAll(listOfBarcodes);
        ArrayAdapter<String> aA = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, completeList);
        lv.setAdapter(aA);
    }else{
        // No scanned handle here
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM