I have defined the separate adapter Class and getter and setters.
Can anyone help to clear the error?
Thanks in Advance.
this is the class which using the recycler view. the separate card view has attached to the Recycler
public class boxInService extends AppCompatActivity {
RecyclerView recyclerView;
inserviceAdapter inadapter;
List<serviceData> serviceDataList;
ProgressDialog progressDialog;
private SharedPreferences pref;
private static final String PREF_NAME="share_pref";
String op;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_box_in_service);
getSupportActionBar().setTitle("Boxes in Service");
serviceDataList=new ArrayList<>();
recyclerView=findViewById(R.id.in_service_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
pref=getSharedPreferences(PREF_NAME,MODE_PRIVATE);
op=pref.getString("operator_stored","");
progressDialog=new ProgressDialog(this);
progressDialog.setMessage("Loading....");
progressDialog.show();
loadData();
}
private void loadData() {
StringRequest request=new StringRequest(
Request.Method.GET,
ROOT_URL + boxinService + op,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONArray jsonArray=new JSONArray(response);
for (int i=0;i<=jsonArray.length();i++){
JSONObject object=jsonArray.getJSONObject(i);
String chip=object.getString("chip_id");
String ua=object.getString("ua_key");
String cas=object.getString("cas");
String model=object.getString("stb_type");
serviceData s_Data=new serviceData(chip,ua,cas,model);
serviceDataList.add(s_Data);
}
inadapter=new inserviceAdapter(boxInService.this,serviceDataList);
recyclerView.setAdapter(inadapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}
);
VolleyRequest.getInstance(this).addToRequest(request);
}
}
Is anything Missing?
Related
I want to send the user-id data I received with android to laravel.
codes for android;
public class MainActivity extends AppCompatActivity {
public String userId;
Button send;
private static final String KEY_USERNAME ="userId" ;
String insertUrl ="http://127.0.0.1:8000/registerDevices";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userId= "12345";
send=(Button) findViewById(R.id.buttonPush);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(), userId, Toast.LENGTH_LONG).show();
registerUser();
}
});
}
public void registerUser(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, insertUrl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), userId, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,userId);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
My route in laravel;
Route::post('/registerDevices', 'RegisterController#registerdevices');
public function registerdevices(Request $request)
{
\Log::info('api call from andriod');
$jsonPostedData = Input::get('userId');
return $this->service->registerdevices($jsonPostedData);
}
my code does not work and the error it gives;
java.net.ConnectException:Connection Refused
Can you help me?
Im trying to create a currency converter app on android.
im getting this String request from an api
{"base":"USD","date":"2018-07-27","rates":{"RON":3.9819354839}}
i'd like to somehow parse this string (using json) and get the RON value
this is my code:
public class MainActivity extends AppCompatActivity {
Button convert;
TextView textView;
RequestQueue requestQueue;
Spinner spinner;
Spinner spinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.spinner);
spinner2=(Spinner)findViewById(R.id.spinner2);
final EditText textBox1 = (EditText)findViewById(R.id.editText);
final EditText textBox2 = (EditText)findViewById(R.id.editText2);
final TextView textView=(TextView)findViewById(R.id.textView);
Button convert = (Button)findViewById(R.id.button);
Cache cache = new DiskBasedCache(getCacheDir(),1024*1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue=new RequestQueue(cache,network);
requestQueue.start();
convert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text1 = spinner.getSelectedItem().toString();
String text2= spinner2.getSelectedItem().toString();
String URL ="https://exchangeratesapi.io/api/latest?base="+text1+"&symbols="+text2;
StringRequest stringRequest= new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
textView.setText(response);
Log.e("Rest response", response.toString());
// requestQueue.stop();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
textView.setText("error......");
error.printStackTrace();
}
});
requestQueue.add(stringRequest);
}
});
}
}
I managed to resolve this issue! Maybe this will help other people too.
JSONObject obj = new JSONObject(response);
JSONObject ratesObj = obj.getJSONObject("rates");
String currency = ratesObj.getString(convertedValue);
I'm trying to fix a problem related to RecyclerView. When I try to show the list of objects for the first time I don't see anything. The data are passed thanks to a volley request that is known to be async. So, I've tried various solutions like onDataSetChanged() but nothing happens. I want to remark that the solution works, the second time I watch the list I can see it without any problems.
Here's my code:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private static MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
adapter = new MyAdapter(this,initData());
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
private List<ParentObject> initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();
for(TitleParent title:titles)
{
List<Object> childList = getList(title);
title.setChildObjectList(childList);
parentObject.add(title);
}
return parentObject;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}
private List<Object> getList(final TitleParent title){
final List<Object> childList = new ArrayList<>();
// Tag used to cancel the request
String tag_string_req = "request";
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_DASHBOARD, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("POL", "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
String certificate = user.getString("numero_contratto");
if(title.getTitle().contains(certificate)){
String scadenza = user.getString("scadenza");
String totale = user.getString("totale");
childList.add(new TitleChild(scadenza,totale));
}
}
Log.d("TRY", "Response: " + response.toString());
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
// Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("DASH", "Login Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(),
// error.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
return childList;
}
Thank you for your help!
well there are couple of points
get list method returning empty list after made volley request.
make sure list filled data from the server then call the set data.
you have to try something like below.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private static MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
// show some loading bar
initData();
}
private void initData() {
TitleCreator titleCreator = TitleCreator.get(this);
List<TitleParent> titles = titleCreator.getAll();
List<ParentObject> parentObject = new ArrayList<>();
for(TitleParent title:titles)
{
List<Object> childList = getList(title);
title.setChildObjectList(childList);
parentObject.add(title);
}
}
private void setData(List<ParentObject> parentObject) {
// load the data here
adapter = new MyAdapter(this,parentObject);
adapter.setParentClickableViewAnimationDefaultDuration();
adapter.setParentAndIconExpandOnClick(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
((MyAdapter)recyclerView.getAdapter()).onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}
private List<Object> getList(final TitleParent title){
final List<Object> childList = new ArrayList<>();
// Tag used to cancel the request
String tag_string_req = "request";
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_DASHBOARD, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("POL", "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
for (int i = 0; i < jObj.getJSONArray("polizze").length(); i++) {
JSONObject user = jObj.getJSONArray("polizze").getJSONObject(i);
String certificate = user.getString("numero_contratto");
if(title.getTitle().contains(certificate)){
String scadenza = user.getString("scadenza");
String totale = user.getString("totale");
childList.add(new TitleChild(scadenza,totale));
}
setData(childList);
}
Log.d("TRY", "Response: " + response.toString());
} catch (JSONException e) {
// JSON error
e.printStackTrace();
// Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("DASH", "Login Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(),
// error.getMessage(), Toast.LENGTH_LONG).show();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
return childList;
}
I code to send some message and email using HTTP post using Volley.
when i run the emulator using Genymotion.Everything luking fine but i click the click button it show HTTP has been stopped. I am giving logcat picture Please click this Logcat picture to see Please help me what is wrong and how to resolve this problem
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String URL_CLICK = "http://www.careoservice.goyalsoftwares.com/feedback/add.php";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EMAIL = "email";
private EditText editTextMessages;
private EditText editTextEmail;
private Button buttonSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextEmail = (EditText) findViewById(R.id.editmsg);
editTextEmail = (EditText) findViewById(R.id.editmail);
buttonSend = (Button) findViewById(R.id.btnclk);
buttonSend.setOnClickListener(this);
}
public void sendView() throws JSONException {
final String message = editTextMessages.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CLICK, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
Toast.makeText(MainActivity.this,s, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_MESSAGE, message);
params.put(KEY_EMAIL, email);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
#Override
public void onClick(View v) {
if (v == buttonSend) {
try {
sendView();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
You are not initialazing the "editTextMessages", this is your null pointer exception.
Hello I populated a listview by following this Tutorial
And I want the listview to automatically refreshed after I add an item in a dialog form (consist of EditText etc.) There are suggestion I already followed but unfortunately still cant achieved it.
Here is My Code
public class Attendance extends AppCompatActivity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
//Attendance List json url
private static final String url = "http://10.0.2.2/MobileClassRecord/getAttendanceList.php";
private ProgressDialog pDialog;
private List<AttendanceList> attList = new ArrayList<>();
private ListView mylistView;
private CustomAttendanceListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mylistView = (ListView) findViewById(R.id.list);
adapter = new CustomAttendanceListAdapter(this, attList);
mylistView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonArrayRequest attReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
AttendanceList alist = new AttendanceList();
alist.setDate(jsonObject.getString("att_date"));
alist.setName(jsonObject.getString("att_name"));
attList.add(alist);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(TAG, "Error: " + volleyError.getMessage());
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(attReq);
}
#Override
protected void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
Any help would be much appreciated :)
I simply use this technique : in the Attendance activity put this
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
refreshData();
}
as soon the AlertDialog is dismissed, it calls therefreshData() method that refreshs the ListView