Cannot call RecycleView from other activity - java

My RecycleView is in my Activity and then I want to set it in my other class. But it always say NullObjectReference with my RecycleView. I dont know why because i have initialize it. Is there something wrong? Here is my code
My Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
RecyclerView recyclerView;
VolleyGet volleyGet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initRecycle();
volleyGet = new VolleyGet();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
Button get = (Button) findViewById(R.id.get);
get.setOnClickListener(this);
}
public void initRecycle(){
recyclerView = findViewById(R.id.recycleview);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get:
volleyGet.volleyRequest(this);
break;
default:
break;
}
}
My Other Class (RecycleView must be set here)
public class VolleyGet {
RecyclerView recyclerView;
public RecycleAdapter adapter;
public ArrayList<User> listUser;
public void volleyRequest(final Context context) {
MainActivity mainActivity = new MainActivity();
mainActivity.initRecycle();
listUser = new ArrayList<>();
adapter = new RecycleAdapter(context, listUser);
String URL = "http://192.168.180.7:8080/get.php";
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
ArrayList<User> items = new Gson().fromJson(response.toString(), new TypeToken<ArrayList<User>>() {
}.getType());
listUser.clear();
listUser.addAll(items);
}
recyclerView.setAdapter(adapter);
} catch (JSONException e) { e.printStackTrace(); } }},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) { }}) {};
queue.add(stringRequest);
}

Looks like the RecycleView init with calling a method. You can try initialize with other way like using Activity like this.
Activity activity;
and this how to get your activity
public VolleyGet(Activity _activity){
this.activity = _activity;
recyclerView = (RecyclerView) this.activity.findViewById(R.id.recycleview);
}
or you can do this if you want to use Context
public VolleyGet(Context context){
recyclerView = (RecyclerView) ((Activity)context).findViewById(R.id.recycleview);
}
dont forget to add this in your MainActivity for sending your activity
volleyGet = new VolleyGet(this);
so the whole code of your other class must be like this
public class VolleyGet {
RecyclerView recyclerView;
public RecycleAdapter adapter;
public ArrayList<User> listUser;
Activity activity;
public VolleyGet(Activity _activity){
this.activity = _activity;
recyclerView = (RecyclerView) this.activity.findViewById(R.id.recycleview);
}
public void volleyRequest(final Context context) {
listUser = new ArrayList<>();
adapter = new RecycleAdapter(context, listUser);
String URL = "http://192.168.180.7:8080/get.php";
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
ArrayList<User> items = new Gson().fromJson(response.toString(), new TypeToken<ArrayList<User>>() {
}.getType());
listUser.clear();
listUser.addAll(items);
}
recyclerView.setAdapter(adapter);
} catch (JSONException e) { e.printStackTrace(); } }},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) { }}) {};
queue.add(stringRequest);

Related

No adapter attached in Recycler View

I am new to Android programming and I'm trying to create a cardview using recycler view using an API but I am getting the error saying "E/RecyclerView: No adapter attached; skipping layout". I tried looking up the solution online but couldn't seem to figure it out.
I tried the solution given by this link :
http://www.chansek.com/RecyclerView-no-adapter-attached-skipping-layout/
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
}
private void parseJSON()
{
String url = "http://apidev.travelhouse.world/api/v1/packages";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject hit = jsonArray.getJSONObject(i);
String holiday_name = hit.getString("holiday_name");
String holiday_price = hit.getString("package_price");
String primary_image = hit.getString("primary_image");
mExampleList.add(new ExampleItem(primary_image,holiday_name,holiday_price));
}
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
/** Passing some request headers* */
#Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Content-Type", "application/json");
headers.put("X-API-KEY", "CODEX#123");
return headers;
}
};
mRequestQueue.add(request);
}
}
I think this is solution which Vlad Kramarenko recommended. I just implemented it in your code and I agree with it.
So the problem is that you need to set Adapter for RecyclerView at the beginning. Once it is set up, you linked your List of ExampleItems to your RecyclerView through ExampleAdapter. Now every time you change your list and you want to updated your RecyclerView, you need to call notifyDataSetChanged() method on your Adapter which is linked to RecyclerView.
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mExampleList = new ArrayList<>();
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
}
private void parseJSON()
{
String url = "http://apidev.travelhouse.world/api/v1/packages";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject hit = jsonArray.getJSONObject(i);
String holiday_name = hit.getString("holiday_name");
String holiday_price = hit.getString("package_price");
String primary_image = hit.getString("primary_image");
mExampleList.add(new ExampleItem(primary_image,holiday_name,holiday_price));
}
mRecyclerView.getAdapter().notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
/** Passing some request headers* */
#Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Content-Type", "application/json");
headers.put("X-API-KEY", "CODEX#123");
return headers;
}
};
mRequestQueue.add(request);
}
Maybe this will sound strange for you, but it says that you create your recyclerview but forgot to add the adapter to recyclerView... But don't forget, you do it. So why?
Answer: when the system tries to draw recyclerView it doesn't have an adapter. You attach adapter in parseJSON function after getting a response from the server. Request and respons from server take some time, but int the meantime system finishes drawing views and says: Hey, your recyclerView doesn't have an adapter, please fix it. You can ignore this warning, but if you want to make all things perfect: you can attach empty adapter in onCreate, and after getting a response do
mRecyclerView.getAdapter().notifyDataSetChanged();
EDIT:
this how will look your onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mExampleList = new ArrayList<>();
mRecyclerView.setAdapter(new ExampleAdapter(mExampleList, some arguments)
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
}
In the onCreate Method, add after creating array list
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
Remove them from the try clause

How to return data from adapter to activity

I have an activity that contains a RecyclerView and I show some data that comes from server to it. The problem is that I I have set the onCLick of RecyclerView in MYAdapter class but now I need to handle the OnClick of RecyclerView in based on some data which is my Activity which contains RecyclerView.
public class ActivityAllUsers extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listitems;
private static final String URL_DATA = "http://shayea.tk/sof/get_allusers.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int id = getIntent().getIntExtra("IDUSERTYPE", 0);// -1 is default value
recyclerView = (RecyclerView) findViewById(R.id.recyvlerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listitems = new ArrayList<>();
loadRecyclerviewData();
}
private void loadRecyclerviewData()
{
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
dialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("result");
for (int i =0;i<array.length();i++)
{
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("usr_id"),
o.getString("usr_name"),
o.getString("usr_phone"),
o.getString("usr_email"),
o.getString("usr_status"),
o.getString("prv_name"),
o.getString("org_name")
);
listitems.add(item);
}
adapter = new MyAdapter(listitems , getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
I want to the OnClick of RecyclerView to be available when ever this int id = getIntent().getIntExtra("IDUSERTYPE", 0); id contains 1 and if id contains anything else the OnClick shouldn't be active.
Here is MyAdapter class which contains the OnClick of RecyclerView :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems;
private Context context;
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ListItem listItem = listItems.get(position);
holder.txtusername.setText(listItem.getusername());
holder.txtuseremail.setText(listItem.getEmail());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, UserInformation.class);
intent.putExtra("Details", (Serializable) listItem);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView txtusername;
public TextView txtuseremail;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
txtusername = (TextView) itemView.findViewById(R.id.textviewusername);
txtuseremail = (TextView) itemView.findViewById(R.id.textviewuseremail);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearlayout);
}
}
}
I need the id which is in ActivityAllUsers ih here :
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, UserInformation.class);
intent.putExtra("Details", (Serializable) listItem);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
The problem is that I I have set the onCLick of RecyclerView in
MYAdapter class but now I need to handle the OnClick of RecyclerView
in based on some data which is my Activity which contains
RecyclerView.
I think with your requirement, you need to change title: How to return data from adapter to activity
In your adapter, you create callback like this
public interface AdapterCallback{
void onItemClicked(int position);
}
AdapterCallback callback;
public MyAdapter(List<ListItem> listItems, Context context, AdapterCallback callback) {
this.listItems = listItems;
this.context = context;
this.callback = callback;
}
...
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// You can move this block codes to Activity
Intent intent = new Intent(context, UserInformation.class);
intent.putExtra("Details", (Serializable) listItem);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
//
// callback here
if(callback != null) {
callback.onItemClicked(position)
}
}
});
And in ActivityAllUsers
public class ActivityAllUsers extends AppCompatActivity implements AdapterCallback {
...
adapter = new MyAdapter(listitems , getApplicationContext(), ActivityAllUsers.this);
...
#Override
public void onItemClicked(int position){
// call back here
}
}
you may pass the value through adapter constructor something like this
adapter = new MyAdapter(listitems , getApplicationContext(), value);
and in your adapter class
int value = -1; //global variable with default value
public MyAdapter(List<ListItem> listItems, Context context, int value) {
this.listItems = listItems;
this.context = context;
this.value = value;
}
and later in onBindViewHolder
if(value == 1) {
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, UserInformation.class);
intent.putExtra("Details", (Serializable) listItem);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
You can pass the data to the adapter when defining the adapter. Here is an example:
adapter = new MyAdapter(listitems , getApplicationContext(), data you want to send);
To receive, you have to edit the constructor and make it like this:
public MyAdapter(List<ListItem> listItems, Context context, String DataYou WantToReceive)
{
this.listItems = listItems;
this.context = context;
this.data = DataYouWantToReceive
}
Try this,
int id = getIntent().getIntExtra("IDUSERTYPE", 0);
adapter = new MyAdapter(listitems , getApplicationContext(),id);
recyclerView.setAdapter(adapter);
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems;
private Context context;
private int id;
public MyAdapter(List<ListItem> listItems, Context context,int id) {
this.listItems = listItems;
this.context = context;
this.id=id;
}
.
.
.
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ListItem listItem = listItems.get(position);
holder.txtusername.setText(listItem.getusername());
holder.txtuseremail.setText(listItem.getEmail());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, UserInformation.class);
intent.putExtra("Details", (Serializable) listItem);
intent.putExtra("ID", id);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
});
}

Custom Listview Won't Display with Volley Request

I have created a Custom Listview that will display area list from my database but sometimes the data won't display. After staying in the activity for a couple of seconds and restarting the activity again it will display the list. Here is my code can you check this out ? If see what's the problem. Thanks!
AreaListActivity.java
private ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setupActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
protected void onResume() {
super.onResume();
setupList();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Only if you need to restore open/close state when
// the orientation is changed
if (adapter != null) {
adapter.saveStates(outState);
}
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Only if you need to restore open/close state when
// the orientation is changed
if (adapter != null) {
adapter.restoreStates(savedInstanceState);
}
}
private void setupList() {
ListView listView = (ListView) findViewById(R.id.list_view);
adapter = new ListAdapter(this, createList());
listView.setAdapter(adapter);
}
private List<String> createList() {
final List<String> list = new ArrayList<>();
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String areaName = obj.getString("area_name");
list.add(areaName);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
GetAreaListRequest registerRequest = new GetAreaListRequest("True", responseListener);
RequestQueue queue = Volley.newRequestQueue(AreaListActivity.this);
queue.add(registerRequest);
return list;
}
private void setupActionBar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
GetAreaListRequest.java
public class GetAreaListRequest extends StringRequest {
private static final String GET_AREA_LIST_URL = "http://192.168.1.7:8080/smsams/get_area_list.php";
private Map<String, String> params;
public GetAreaListRequest(String get_area_status, Response.Listener<String> listener) {
super(Method.POST, GET_AREA_LIST_URL, listener, null);
params = new HashMap<>();
params.put("get_area_status", get_area_status);
}
#Override
public Map<String, String> getParams() {
return params;
}
ListAdapter.java
class ListAdapter extends ArrayAdapter<String> {
private final LayoutInflater mInflater;
private final ViewBinderHelper binderHelper;
private Context context;
ListAdapter(Context context, List<String> objects) {
super(context, R.layout.row_list, objects);
mInflater = LayoutInflater.from(context);
binderHelper = new ViewBinderHelper();
this.context = context;
// uncomment if you want to open only one row at a time
// binderHelper.setOpenOnlyOne(true);
}
#NonNull
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_list, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.text);
holder.statView = convertView.findViewById(R.id.statistic_layout);
holder.mapView = convertView.findViewById(R.id.map_layout);
holder.infoView = convertView.findViewById(R.id.info_layout);
holder.swipeLayout = (SwipeRevealLayout) convertView.findViewById(R.id.swipe_layout);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final String item = getItem(position);
if (item != null) {
binderHelper.bind(holder.swipeLayout, item);
holder.textView.setText(item);
holder.statView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, StatisticActivity.class);
context.startActivity(i);
}
});
holder.mapView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, MapsActivity.class);
i.putExtra("soilArea", holder.mapView.toString());
context.startActivity(i);
}
});
holder.infoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, InfoViewActivity.class);
context.startActivity(i);
}
});
}
return convertView;
}
/**
* Only if you need to restore open/close state when the orientation is changed.
* Call this method in {#link android.app.Activity#onSaveInstanceState(Bundle)}
*/
void saveStates(Bundle outState) {
binderHelper.saveStates(outState);
}
/**
* Only if you need to restore open/close state when the orientation is changed.
* Call this method in {#link android.app.Activity#onRestoreInstanceState(Bundle)}
*/
void restoreStates(Bundle inState) {
binderHelper.restoreStates(inState);
}
private class ViewHolder {
TextView textView;
View statView;
View mapView;
View infoView;
SwipeRevealLayout swipeLayout;
}
Try this:
private List<String> list = new ArrayList<>();
private void setupList() {
ListView listView = (ListView) findViewById(R.id.list_view);
adapter = new ListAdapter(this, list);
listView.setAdapter(adapter);
}
private void createList() {
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String areaName = obj.getString("area_name");
list.add(areaName);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
};
GetAreaListRequest registerRequest = new GetAreaListRequest("True", responseListener);
RequestQueue queue = Volley.newRequestQueue(AreaListActivity.this);
queue.add(registerRequest);
}
The network requests usually happens in an asynchronous way. So there is no specific time that the onResponse() gets called. So in your createList() method you initialised an empty arraylist and before the request produces the response(asynchronously calls onResponse) the list which contains no value is returned.
You can change your code something like this:
In createList():
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String areaName = obj.getString("area_name");
list.add(areaName);
adapter = new ListAdapter(this, list);
listView.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
So here the adapter is set only after the data is pulled from the network.

Load JSON arraylist in separate class and load in another activity

I am trying to load some items from JSON, I am able to get and parse the JSON and load it up in listview when using one activity. However, I want to use a LoadJSON.class to load the JSON, and then the activity can call the json passed and show it in the listview in that activity.
Here is what I have tried:
SongsManager.class
public class SongsManager {
private String TAG = SongsManager.class.getSimpleName();
private static final String API_URL = "http://xxxxxxxxxxxxx.com/jame/mp3/songlist.json";
private List<SolTracks> solTracksList;
private ProgressDialog pDialog;
private final Activity activity;
public SongsManager(Activity activity) {
this.activity = activity;
solTracksList = new ArrayList<>();
pDialog = new ProgressDialog(activity);
fetchSongs();
}
private void fetchSongs() {
pDialog.setMessage("Fetching Playlist...");
pDialog.show();
// Volley's json array request object
JsonArrayRequest req = new JsonArrayRequest(API_URL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, "Responser = " + response.toString());
pDialog.hide();
if (response.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < response.length(); i++) {
try {
JSONObject movieObj = response.getJSONObject(i);
String songTitle = movieObj.getString("title");
String songId = movieObj.getString("id");
String streamUrl = movieObj.getString("stream_url");
SolTracks m = new SolTracks(songTitle, songId, streamUrl);
solTracksList.add(m);
Collections.sort(solTracksList, new TrackComparator());
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
pDialog.hide();
Snackbar snackbar = Snackbar
.make(activity.findViewById(android.R.id.content), "PLEASE CHECK YOUR INTERNET", Snackbar.LENGTH_LONG)
.setAction("DISMISS", new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
// Changing snackbar background
snackbar.getView().setBackgroundColor(ContextCompat.getColor(activity, R.color.colorPrimary));
// Changing message text color
snackbar.setActionTextColor(Color.YELLOW);
// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbar.show();
}
});
req.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
public List<SolTracks> getList() {
return solTracksList;
}
Activity class
public class TheMain1 extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private String TAG = TheMain1.class.getSimpleName();
private static final String API_URL = "http://xxxxxxxxxxx.com/jame/mp3/songlist.json";
private ListView listView;
private SolTracksAdapter adapter;
private ProgressDialog pDialog;
private List<SolTracks> songslist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.track_list_view);
songslist = new ArrayList<>();
SongsManager songsManager = new SongsManager(this);
songslist = songsManager.getList();
adapter = new SolTracksAdapter(this, songslist);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SolTracks track = songslist.get(position);
final String stream_url = track.stream_url;
final String id_url = track.id;
Intent intent = new Intent(TheMain1.this, PlayerActivity.class);
intent.putExtra("songPosition", position);
intent.putExtra("streamUrl", stream_url);
startActivity(intent);
}
}
);
}
As it is right now, I know the JSON is loaded from SongsManager, but its just not displaying in the listview of the Activity class. Can anyone help, and show what I'm doing wrong? Thanks
I was able to fix this by implementing Parcelable to send the list to the receiving activity.
public class SolTracks implements Parcelable {
public String title;
public String id;
public String stream_url;
}
Sending the list from the Activity A:
Intent intent = new Intent(TheMain.this, PlayerActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", solTracksList);
intent.putExtras(bundle);
intent.putExtra("songPosition", position);
startActivity(intent);
and then receiving in Activity B:
Bundle extras = getIntent().getExtras();
if (extras != null) {
songPosition = extras.getInt("songPosition");
trackList = extras.getParcelableArrayList("mylist");
}

Can't back to previous activity

I need to help. I tried to back to previous activity, but when I click arrow button (action bar) it show errors. Can someone help me .. please!
Thank's before for your answers
This is my codes:
First, I get data in KitabActivity (so this is the first activity):
KitabActivity:
public class KitabActivity extends AppCompatActivity {
private List<Kitab> kitabList;
private RecyclerView recyclerView;
private KitabAdapter kitabAdapter;
private RecyclerView.LayoutManager layoutManager;
private Toolbar toolbar;
private ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kitab);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Kitab Hadits");
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.rv_main_kitab);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
kitabList = new ArrayList<>();
getDataKitab();
}
private void getDataKitab() {
final ProgressDialog loading = ProgressDialog.show(this,"Mohon Tunggu...","Sedang mengambil data...",false,false);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigKitab.DATA_URL_KITAB, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray jsonArray) {
loading.dismiss();
parseData(jsonArray);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
loading.dismiss();
Toast.makeText(KitabActivity.this, "Gagal koneksi ke server!", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray jsonArray) {
for(int i=0;i<jsonArray.length();i++){
Kitab kitab = new Kitab();
JSONObject jsonObject = null;
try{
jsonObject = jsonArray.getJSONObject(i);
kitab.setId_kitab(jsonObject.getString(ConfigKitab.TAG_ID_KITAB));
kitab.setNama_kitab(jsonObject.getString(ConfigKitab.TAG_NAMA_KITAB));
}catch(JSONException e){
e.printStackTrace();
}
kitabList.add(kitab);
}
kitabAdapter = new KitabAdapter(kitabList,this);
recyclerView.setAdapter(kitabAdapter);
}
}
In KitabAdapter, I pass KEY_ID to BabKitabActivity:
This is KitabAdapter:
public class KitabAdapter extends RecyclerView.Adapter<KitabVH> {
private Context context;
List<Kitab> kitabList;
public KitabAdapter(List<Kitab> kitabList, Context context){
super();
this.kitabList = kitabList;
this.context = context;
}
#Override
public KitabVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_kitab,parent,false);
return new KitabVH(view);
}
#Override
public void onBindViewHolder(KitabVH holder, int position) {
final Kitab kitab = kitabList.get(position);
holder.bind(kitab);
holder.cv_kitab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), BabKitabActivity.class);
intent.putExtra("KEY_ID",kitab.getId_kitab());
intent.putExtra("KEY_NAMA",kitab.getNama_kitab());
v.getContext().startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return kitabList.size();
}
}
BabKitabActivity:
public class BabKitabActivity extends AppCompatActivity {
private List<BabKitab> babKitabList;
private RecyclerView recyclerView;
private BabKitabAdapter babKitabAdapter;
private RecyclerView.LayoutManager layoutManager;
private Toolbar toolbarBab;
private ActionBar actionBar;
private String id_kitab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bab_kitab);
toolbarBab = (Toolbar) findViewById(R.id.toolbarBab);
toolbarBab.setTitle(getIntent().getExtras().getString("KEY_NAMA"));
setSupportActionBar(toolbarBab);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.rv_main_bab);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
babKitabList = new ArrayList<>();
getDataBab();
}
private void getDataBab() {
id_kitab = getIntent().getExtras().getString("KEY_ID");
String url = ConfigBabKitab.DATA_URL_BAB+id_kitab;
final ProgressDialog loading = ProgressDialog.show(this,"Mohon Tunggu...","Sedang mengambil data...",false,false);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray jsonArray) {
loading.dismiss();
parseData(jsonArray);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
loading.dismiss();
Toast.makeText(BabKitabActivity.this, "Gagal Koneksi ke Server!", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray response) {
for(int i=0;i<response.length();i++){
BabKitab babKitab = new BabKitab();
JSONObject obj = null;
try{
obj = response.getJSONObject(i);
babKitab.setId_bab_kitab(obj.getString(ConfigBabKitab.TAG_ID_BAB));
babKitab.setNomor_bab(obj.getString(ConfigBabKitab.TAG_NOMOR_BAB));
babKitab.setNama_bab(obj.getString(ConfigBabKitab.TAG_NAMA_BAB));
babKitab.setPenjelasan(obj.getString(ConfigBabKitab.TAG_PENJELASAN));
}catch(JSONException e){
e.printStackTrace();
}
babKitabList.add(babKitab);
}
babKitabAdapter = new BabKitabAdapter(babKitabList,this);
recyclerView.setAdapter(babKitabAdapter);
}
}
DetailActivity:
public class DetailActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<DetailHadits> detailHaditsList;
private DetailAdapter detailAdapter;
private String id_bab_kitab;
private TextView tv_penjelasan;
private String penjelasan;
private CardView cv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarDetail);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
cv = (CardView) findViewById(R.id.cv);
tv_penjelasan = (TextView) findViewById(R.id.tv_penjelasan);
//cv.setVisibility(View.GONE);
//tv_penjelasan.setVisibility(View.GONE);
penjelasan = getIntent().getExtras().getString("KEY_PENJELASAN");
tv_penjelasan.setText(penjelasan);
recyclerView = (RecyclerView) findViewById(R.id.rv_main_detail);
//recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
detailHaditsList = new ArrayList<>();
getAyat();
}
private void getAyat() {
id_bab_kitab = getIntent().getExtras().getString("KEY_ID");
String url = ConfigDetail.DATA_URL_AYAT+id_bab_kitab;
final ProgressDialog loading = ProgressDialog.show(this,"Mohon Tunggu...","Sedang mengambil data...",false,false);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray jsonArray) {
loading.dismiss();
parseData(jsonArray);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
loading.dismiss();
Toast.makeText(DetailActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray jsonArray) {
for(int i=0;i<jsonArray.length();i++){
DetailHadits detailHadits = new DetailHadits();
JSONObject obj = null;
try{
obj = jsonArray.getJSONObject(i);
detailHadits.setUrlGambarAyat(obj.getString(ConfigDetail.TAG_GAMBAR));
detailHadits.setTerjemahanAyat(obj.getString(ConfigDetail.TAG_TERJEMAHAN));
}catch(JSONException e){
e.printStackTrace();
}
detailHaditsList.add(detailHadits);
}
detailAdapter = new DetailAdapter(detailHaditsList,this);
recyclerView.setAdapter(detailAdapter);
}
}
and this is the log cat:
FATAL EXCEPTION: main
Process: id.hadits.pencarian.skripsi.nana.aplikasipencarianhadits, PID: 15228
java.lang.RuntimeException: Unable to start activity ComponentInfo{id.hadits.pencarian.skripsi.nana.aplikasipencarianhadits/id.hadits.pencarian.skripsi.nana.aplikasipencarianhadits.Activity.BabKitabActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at id.hadits.pencarian.skripsi.nana.aplikasipencarianhadits.Activity.BabKitabActivity.onCreate(BabKitabActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
thank's in advance ;)
So, in my case BabKitabActivity can load data if there is a KEY_ID from KitabActivity (i guess).
What I want is just go back to previous activity (in this case to BabKitabActivity) without getting KEY_ID again.
It work with button back in emulator or smartphone, but it doesn't work with arrow button in actionbar, what should I do?
before getIntent().getExtras() check its null or not
Bundle extras = getIntent().getExtras();
if (extras != null) {
penjelasan = extras.getString("KEY_PENJELASAN");
}
something like
if (extras != null) {
id_bab_kitab = extras.getString("KEY_ID");
}
Unable to start activity ComponentInfo{id.hadits.pencarian.skripsi.nana.aplikasipencarianhadits/id.hadits.pencarian.skripsi.nana.aplikasipencarianhadits.Activity.BabKitabActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.os.Bundle.getString(java.lang.String)' on a
null object reference
Issue is in your BatkitabActivity. You are getting string from intent which is null. Before getting string value, you can check if intent is null or not.
Bundle extras = getIntent().getExtras();
if (extras != null) {
....
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at id.hadits.pencarian.skripsi.nana.aplikasipencarianhadits.Activity.BabKitabActivity.onCreate(BabKitabActivity.java:46)
penjelasan = getIntent().getExtras().getString("KEY_PENJELASAN");
you can get the Bundle's data from getIntent().getExtras(), but you get null, so you got the NullPointerException, that make a crash in your app.

Categories