How to make a Dynamic ListView generated with BarCodeScan data?

0

I'm a beginner in Java / Android, and I'm trying to get a listview created from Scans using ZXing.

In case I have the screen with the button to scan and it shows the scan response, I want to put the answers of more than one scan in a listview like a stack, one on top of the other, how do I do this?

This is MainActivity (Zxing example pattern).

public class MainActivity extends Activity {
    public static final int REQUEST_CODE = 0;
    private TextView txResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txResult = (TextView) findViewById(R.id.txResult);


    }

    public void callZXing(View view){
        Intent it = new Intent(MainActivity.this, com.google.zxing.client.android.CaptureActivity.class);
        startActivityForResult(it, REQUEST_CODE);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(REQUEST_CODE == requestCode && RESULT_OK == resultCode){
            txResult.setText("RESULTADO: "+data.getStringExtra("SCAN_RESULT")+" ("+data.getStringExtra("SCAN_FORMAT")+")");
        }
    }
}

Thanks to all who can help.

    
asked by anonymous 17.02.2016 / 14:42

1 answer

1

I'll recommend using RecyclerView instead of ListView because RecyclerView supports everything a ListView does most efficiently.

I would also recommend reading below so you can understand the code: link

First, you need to import the RecyclerView lib, go to your build.gradle:

dependencies
    {
        ...
        compile 'com.android.support:recyclerview-v7:23.1.1'
    }

In your activity_main.xml you will need to add a RecyclerView:

...
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...

In your MainActivity.java you will need to start the RecyclerView and its adapter:

public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

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

    ...
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    List<String> mData = new ArrayList<String>();
    mAdapter = new MyAdapter( mData );
    mRecyclerView.setAdapter( mAdapter );
    ...
}
...
}

Your Adapter will need to have a function to add new data, in this case the public void addData (String newData):

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mDataset;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView mTextView;
    public ViewHolder(TextView v) {
        super(v);
        mTextView = v;
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<String> myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.my_text_view, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ...
    ViewHolder vh = new ViewHolder((TextView)v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.mTextView.setText( mDataset.get( position) );

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.size();
}

public void addData( String newData ){
    mDataset.add( newData );
}

}

Remember to create the layout of your list items, within the res / layouts folder, create a file called my_text_view with only one TextView with the desired layout.

Now the RecyclerView and its Adapter are correctly configured and you should pass the new scans you have to the adapter and notify you that a new element has been inserted. To send the scanned value you will use the function we created in the adapter, and to warn you that a new element has been inserted, just call the notifyDataSetChanged () function of the adapter. So your onActivityResult would look like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    if(REQUEST_CODE == requestCode && RESULT_OK == resultCode){
        mAdapter.addData("RESULTADO: "+data.getStringExtra("SCAN_RESULT")+" ("+data.getStringExtra("SCAN_FORMAT")+")");
        mAdapter.notifyDataSetChanged( );
    }
}
    
18.02.2016 / 22:37