I will recommend using Recyclerview instead of Listview because Recyclerview supports everything that a Listview does more efficiently.
I would also like to recommend reading below so you can understand the code:
http://developer.android.com/intl/pt-br/training/material/lists-cards.html
First, you need to import the lib from Recyclerview, go to your build.Radle:
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 Recyclerview and your 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 obtained data, in 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, inside the res/layouts folder create a file called my_text_view with only one Textview with the desired layout.
Recyclerview and its Adapter are now correctly configured and you should pass the new scans obtained to Adapter and warn that a new element has been inserted. To send the scanned value you will use the function we created in Adapter, and to warn that a new element has been inserted just call the notifyDataSetChanged( ) function of Adapter. Soon, 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( );
}
}
Someone could show you the way to do it?
– Vitor
so I didn’t understand the part of R.layout.my_text_view,this Textview you put inside another xml file?
– Rafael Alves da Silva