Smith Smith - 1 year ago 48
Android Question

How to add Items to recyclerView after scanning QR Code dynamically


1.On Floating Button click QR Code is scan result is
**acc123**
and it's going to call JSON data from web service which is
View_ID='v1'
.Which is working properly.

2.What I have to do is on next scan result is
**acc124**
and it's going to call JSON data from web service which is
View_ID = "v2"
. I want it to store in the same recycler View as well as bind it to offline.


  1. But, At this point when I scan new QR Code all previous data is gone and its call JSON data and store its View.




MainActivity.java

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private Menu menu;
TextView qrCode;
private static final String TAG = "QR CODE";
List<GetAdapter> GetAdapter1;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
JsonArrayRequest jsonArrayRequest;

RequestQueue requestQueue;

//qr code scanner object
private IntentIntegrator qrScan;

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

//intializing scan object
qrScan = new IntentIntegrator(this);
GetAdapter1 = new ArrayList<>();

recyclerView = (RecyclerView) findViewById(R.id.recyclerview3);
qrCode = (TextView) findViewById(R.id.textView_Category);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);

}

/**
* Initializing collapsing toolbar
* Will show and hide the toolbar title on scroll
*/
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
qrScan.initiateScan();
Snackbar.make(view, "Replace with QR code scan action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

//Getting the scan results
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//if qrcode has nothing in it
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
qrCode.setText(result.getContents());

JSON_DATA_WEB_CALL();

//if qr contains data

Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();

}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}


/** JSON CALL */
public void JSON_DATA_WEB_CALL() {

final ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setMessage("Loading...");
mProgressDialog.show();
Log.d(TAG, "JSON_DATA_WEB_CALL: " + Constants.GET_JSON_DATA_HTTP_URL + qrCode.getText().toString());
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
Constants.GET_JSON_DATA_HTTP_URL + qrCode.getText().toString(), null,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
GetAdapter GetAdapter2 = new GetAdapter();

Log.d("Request", response.toString());
try {
response.getString("View_ID");
JSONArray array = response.getJSONArray("View");
GetAdapter2.setViewId(response.getString("View_ID"));
GetAdapter2.setMAC_QR_Code(qrCode.getText().toString());

JSONObject id = (JSONObject) array.get(0);

JSON_PARSE_DATA_AFTER_WEBCALL(array);
Toast.makeText(MainActivity.this, response.getString("View_ID"), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, response.getString("View"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
mProgressDialog.hide();
GetAdapter1.add(GetAdapter2);


}


}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Request", "Error: " + error.getMessage());

mProgressDialog.hide();
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjReq);
recyclerViewadapter = new RecyclerViewAdapter(GetAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

for(int i = 0; i<array.length(); i++) {

GetAdapterButton GetAdapter2 = new GetAdapterButton();

JSONObject json = null;
try {

json = array.getJSONObject(i);

// GetAdapter2.setViewId(json.getString(JSON_VIEW_ID));

// GetAdapter2.setButtonId(json.getString(Constants.JSON_BUTTON_ID));

} catch (JSONException e) {
e.printStackTrace();
}
// GetAdapter1.add(GetAdapter2);
}

recyclerViewadapter = new RecyclerViewAdapter(GetAdapter1, this);

recyclerView.setAdapter(recyclerViewadapter);
}

}


RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;
String MAC_QR_Code;
List<com.test.boariot.adapter.GetAdapter> GetAdapter;
ImageLoader imageLoader1;
public RecyclerViewAdapter(List<GetAdapter> GetAdapter, Context context){
super();
this.GetAdapter = GetAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.album_card, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder Viewholder, final int position) {
final GetAdapter GetAdapter1 = GetAdapter.get(position);
Viewholder.ImageTitleNameView.setText(GetAdapter1.getViewId());
MAC_QR_Code=(GetAdapter1.getMAC_QR_Code());
Viewholder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(Viewholder.overflow);
}
});
}
@Override
public int getItemCount() {
return GetAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView ImageTitleNameView;
public TextView ImageCategory;
public ImageView networkImageView, overflow ;
public View view;
public ViewHolder(View itemView) {
super(itemView);
view = itemView;

ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_Id) ;
overflow = (ImageView) view.findViewById(R.id.overflow);

networkImageView = (ImageView) itemView.findViewById(R.id.placeholder) ;
networkImageView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
// item clicked
// Start NewActivity.class
Intent myIntent = new Intent(v.getContext(),
NextActivity.class);
Log.d(Constants.TAG, "onClick: "+MAC_QR_Code);
myIntent.putExtra("MAC_QR_Code",MAC_QR_Code);

v.getContext().startActivity(myIntent);
}
});
}
}
}


What I want is to store previous data to recycler view as well as add new data to it.

I hope I make my point clear.

Thanks in advance.

Answer Source

You should only setup the adapter like this once in onCreate.

adapterData = new ArrayList<>();
recyclerViewadapter = new RecyclerViewAdapter(adapterData, this);
recyclerView.setAdapter(recyclerViewadapter);

Anytime you add to the Arraylist, you must call recyclerViewadapter.notifyDataSetChanged() for anything to be displayed

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download