I have list data recycler view and I want to make it like a table layout and dynamical. Here is the example view of the table that I want to make:
https://ibb.co/q7zYM64
And this is my condition app :
https://ibb.co/JKXwthj
and this is my main_activity.java :
public class MainActivity extends AppCompatActivity {
private static final String URL_DATA = "http://sipermata.bappedawaykanan.com/api/admin/jumlah_penduduk_kabupaten/list?year=2019";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
loadRecyclerViewData();
}
private void loadRecyclerViewData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
new Response.Listener<String>() {
#Override
public void onResponse(String s) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("data");
//JSONArray array = new JSONArray(s);
for (int i = 0 ; i<array.length(); i++){
JSONObject o = array.getJSONObject(i);
String id = o.getString("id");
String kec = o.getString("kecamatan");
String avg = o.getString("avg");
//String tahun = o.getString("year");
JSONObject isitahun = o.getJSONObject("year");
Iterator<String>itr = isitahun.keys();
//for(int j = 0; j<=isitahun.length();j++){
while (itr.hasNext()){
String key = itr.next();
JSONObject a = isitahun.getJSONObject(key);
String tahun = a.getString("value");
ListItem item = new ListItem(
id, kec, avg, key, tahun
);
listItems.add(item);
}
}
adapter = new MyAdapter(listItems,getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), volleyError.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
You can set layout as horizontal.
recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(horizontalLayoutManager);
Related
I have created a Recycler view that is supposed to be created when the activity is created. Currently, when I click a button on my MainActivity, an intent launches the ListActivity which has my recyclerview but it doesn't load. I have used toast message to confirm that each method is getting called, and that I am getting the correct data from the API. If I reset the activity using the restart activity option in Android Studio the Recycler shows up and functions correctly. I don't know what I'm doing wrong.
Here is my ListActivity:
private RecyclerView myrecyclerview;
private RecyclerView.Adapter myadapter;
private RecyclerView.LayoutManager mylayoutmanager;
static RequestQueue listqueue;
static final private String url = "https://swapi.dev/api/people/";
static ArrayList<RecyclerItem> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
getSupportActionBar().hide();
listqueue = Volley.newRequestQueue(this);
myrecyclerview = findViewById(R.id.characterlist);
myadapter = new MyAdapter(list, this);
myrecyclerview.setAdapter(myadapter);
myrecyclerview.setHasFixedSize(true);
mylayoutmanager = new LinearLayoutManager(getApplicationContext());
myrecyclerview.setLayoutManager(mylayoutmanager);
parseJsonData();
}
public void parseJsonData(){
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
try {
JSONArray jsonarray = response.getJSONArray("results");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String height = jsonobject.getString("height");
String mass = jsonobject.getString("mass");
String eyecolor = jsonobject.getString("eye_color");
String birthyear = jsonobject.getString("birth_year");
//list.add(new RecyclerItem("darth vader", "200", "128", "1950", "red"));
list.add(new RecyclerItem(name, "Height: " + height, "Mass: " + mass, "Birth Year: " + birthyear, eyecolor));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
listqueue.add(request);
}
#Override
public void onCharacterClick(int position) {
String color = list.get(position).getEyecolor();
Toast.makeText(getApplicationContext(), color, Toast.LENGTH_SHORT).show();
}
} ```
Like I mentioned, once I reload the activity, it works correctly. But I want the recycler view to show when I navigate to the activity.
The problem is that the first time that the activity is create, list is empty and parseJsonData() is running on the background filling that list.
Once you reload the activiy, the list and adapter are filled therefore when you call
myadapter = new MyAdapter(list, this);
myrecyclerview.setAdapter(myadapter);
myrecyclerview.setHasFixedSize(true);
mylayoutmanager = new LinearLayoutManager(getApplicationContext());
myrecyclerview.setLayoutManager(mylayoutmanager);
the recycler view is show. Try to do this on your parseJsonData(); after the loop ends, then create the adapter and show the rv
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
....
}
myadapter = new MyAdapter(list, this);
....
myrecyclerview.setLayoutManager(mylayoutmanager);
I hope it help for u
Creat a List
private List< TipsList > tipsLists = new ArrayList<>();
SET UP RECYCLERVIEW LIKE THIS
RecyclerView tipsRv = findViewById(R.id.tips_rv);
TipsAdapter adapter = new TipsAdapter(tipsLists, this);
tipsRv.setAdapter(adapter);
tipsRv.setHasFixedSize(true);
tipsRv.setLayoutManager(new LinearLayoutManager(this));
getDATA
public void getWallis() {
String myJSONStr = method.loadJSON();
try {
JSONObject ROOT_OBJ = new JSONObject(myJSONStr);
JSONArray MAIN_ARRAY = ROOT_OBJ.getJSONArray("ff_api");
JSONObject TIPS_OBJ = MAIN_ARRAY.getJSONObject(3);
JSONArray TIPS_ARRAY = TIPS_OBJ.getJSONArray("Tips");
for (int i = 0; i < TIPS_ARRAY.length(); i++) {
TipsList tipsList = new TipsList();
JSONObject jsonObject = TIPS_ARRAY.getJSONObject(i);
tipsList.setId(jsonObject.getInt("id"));
tipsList.setTipsTitle(jsonObject.getString("tipsTitle"));
tipsList.setTipsDec(jsonObject.getString("tipsDec"));
tipsLists.add(tipsList);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I have a problem using Volley, I cant get data out from OnResponse mehod.
I need to use the List outside , for Fragment operations, but i couldn`t get it out from there. Maybe im doing something wrong, but i was unable to find a solution on other sites.
Can anyone help me find a solution please?
Here is my code:
public class MainActivity extends AppCompatActivity
{
private static final String JSON_URL = "http://Something/v1/Api.php?apicall=gettopics";
ListView listView;
List<Topic> topicList;
List<Topic> topicList2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listViewTopics);
topicList = new ArrayList<>();
loadTopics();
if(topicList.isEmpty())
{
TextView t = findViewById(R.id.text);
t.setText("Empty");
}
}
class TopicAdapter extends ArrayAdapter<Topic>
{
List<Topic> topicList;
public List<Topic> getTopicList() {
return topicList;
}
public TopicAdapter(List<Topic> topicList)
{
super(MainActivity.this, R.layout.layout_topic_list, topicList);
this.topicList = topicList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.layout_topic_list, null, true);
TextView textViewName = listViewItem.findViewById(R.id.textViewTitle);
final Topic topic = topicList.get(position);
textViewName.setText(topic.getTitle());
return listViewItem;
}
}
public void loadTopics() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray topicArray = obj.getJSONArray("topics");
for (int i = 0; i < topicArray.length(); i++) {
JSONObject topicObject = topicArray.getJSONObject(i);
Topic topic = new Topic(topicObject.getInt("id"),topicObject.getInt("ordering"),topicObject.getString("title"));
topicList.add(topic);
}
TopicAdapter adapter = new TopicAdapter(topicList);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//displaying the error in toast if occurrs
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
Firstly in onCreate():
topicList = new ArrayList<>();
listView = (ListView) findViewById(R.id.listViewTopics);
adapter = new TopicAdapter(topicList); //note:define 'adapter' as a class field as 'listView'
listView.setAdapter(adapter);
Then in onResponse():
topicList.clear();
try{
JSONObject obj = new JSONObject(response);
JSONArray topicArray = obj.getJSONArray("topics");
for (int i = 0; i < topicArray.length(); i++) {
JSONObject topicObject = topicArray.getJSONObject(i);
Topic topic = new Topic(topicObject.getInt("id"),topicObject.getInt("ordering"),topicObject.getString("title"));
topicList.add(topic);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
I am a beginner developer who is studying Android.
The function I want to develop is "Save the data entered in EditText as JSON, save it as SharedPreference, and output it to ListView".
To save it as SharedPreference is OK, but, To output it to ListView is not working now.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Button save_btn;
private ArrayList<List> data = new ArrayList<List>();
ListAdapter adapter;
String title="";
String info="";
int img = R.drawable.man;
String jsondata;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
save_btn = (Button) findViewById(R.id.button1);
loadArrayList(getApplicationContext());
adapter = new ListAdapter(this, R.layout.row, data);
listView.setAdapter(adapter);
registerForContextMenu(listView);
save_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View dlgview = View.inflate(MainActivity.this, R.layout.adds, null);
//adds.xml
final EditText et_title = (EditText) dlgview.findViewById(R.id.editText1);
final EditText et_info = (EditText) dlgview.findViewById(R.id.editText2);
ImageView img1 = (ImageView) dlgview.findViewById(R.id.imageView2);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("ADD");
dlg.setView(dlgview);
dlg.setNegativeButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
try{
jsonObject.put("title", et_title.getText().toString());
jsonObject.put("info", et_info.getText().toString());
jsonObject.put("image",img);
jsonArray.put(jsonObject);
} catch (JSONException e){
e.printStackTrace();
}
jsondata = jsonArray.toString();
saveArrayList();
adapter.notifyDataSetChanged();
}
});
dlg.setPositiveButton("Cancel",null);
dlg.show();
}
});
}//End of onCreate
private void saveArrayList(){
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("jsonData", jsondata);
Log.i("moi","get Data test : " + "jsonData");
editor.apply();
}
private void loadArrayList(Context context){
SharedPreferences sharedPrefs2 = PreferenceManager.getDefaultSharedPreferences(context);
int size = sharedPrefs2.getInt("appreciation_size",0);
String strJson = sharedPrefs2.getString("jsonData", "fail");
Log.i("moi","get SharedPreferences test : " + strJson);
if (strJson != "fail")
{
try {
JSONArray response = new JSONArray(strJson);
for (int i=0; i<size; i++)
{
JSONObject jsonobject = response.getJSONObject(i);
title = jsonobject.getString("title");
Log.i("moi","title test : " + "title");
info = jsonobject.getString("info");
Log.i("moi","info test : " + "info");
data.add(new List(title, info, img));
}
adapter = new ListAdapter(getApplicationContext(), R.layout.row, data);
listView.setAdapter(adapter);
} catch (JSONException e){
e.printStackTrace();
}
}
}
}//End of class
Because you just called loadArrayList() once in onCreate(). That means your data in adapter just changes once.
you call adapter.notifyDataSetChanged() but this method doesnt callloadArrayList()`.
so try to save new json into data in onClick()
Try to remove your code:
adapter = new ListAdapter(this, R.layout.row, data);
listView.setAdapter(adapter);
in CreateView.
I think you set adapter listview double.
i hope that helps you.. thanks
I know this question has been asked so many times, but none of the solutions answered there seems to solve my problem.
Most of the solutions suggested attaching an adapter (setadapter()) and I have done that. But the problem still seems to persist.
I also checked if my listitems are empty and it isn't.
I have attached the adapter in a function loadUserAccounts().
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_accounts);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Swipe to refresh
swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.userSwipeRefresh);
//Recycler View
recyclerView = (RecyclerView) findViewById(R.id.userRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(userAccountsActivity.this));
listItems = new ArrayList<>();
recyclerViewUserAdapter = new RecyclerViewUserAdapter(listItems, userAccountsActivity.this);
recyclerView.setAdapter(recyclerViewUserAdapter);
//Function to load the users to the recycler view
loadUserAccounts();
/*
* Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user
* performs a swipe-to-refresh gesture.
*/
swipeRefresh.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
Log.i("Swipe_log: ", "onRefresh called from SwipeRefreshLayout");
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
loadUserAccounts();
}
}
);
}
and this is my loadUserAccounts() function:
/**
* Function to load the user account
* into the recycler view from the log database
*/
public void loadUserAccounts(){
String tag_string_req = "req_user_acc";
clear();
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_USER_ACCOUNTS, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray jArray = new JSONArray(response);
for(int i = jArray.length()-1; i>=0; i--){
JSONObject log = jArray.getJSONObject(i);
String name = log.getString("name");
String cpfNumber = log.getString("cpfNumber");
String type = log.getString("type");
String created_at = log.getString("created_at");
Log.d("Adapter: ", name + cpfNumber + type + created_at);
ListItem_RecyclerView_User item = new ListItem_RecyclerView_User (name, cpfNumber, type, created_at);
listItems.add(item);
}
recyclerViewUserAdapter.updateDataList(responseList);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if(error!=null && error.getMessage() !=null){
Log.e(TAG,"User Accounts Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Something went wrong",Toast.LENGTH_LONG).show();
}
}
});
//Adding request to request queue
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
if (swipeRefresh == null)
return;
else {
if (swipeRefresh.isRefreshing()) {
swipeRefresh.setRefreshing(false);
}
}
}
My adapter class RecyclerViewUserAdapter
public class RecyclerViewUserAdapter extends RecyclerView.Adapter<RecyclerViewUserAdapter.ViewHolder> {
private List<ListItem_RecyclerView_User> listItems;
private Context context;
public RecyclerViewUserAdapter(List<ListItem_RecyclerView_User> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
public void updateDataList(List<ListItem_RecyclerView_User> newDatas) {
listItems.clear();
listItems.addAll(newDatas);
notifyDataSetChanged();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.users_recycler_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerViewUserAdapter.ViewHolder holder, int position) {
ListItem_RecyclerView_User listItem = listItems.get(position);
holder.txtUserName.setText(listItem.getTxtUserName());
holder.txtUserCpf.setText(listItem.getTxtUserCpf());
holder.txtAccountType.setText(listItem.getTxtAccountType());
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
date = df.parse(listItem.getTxtCreatedDate());
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String loginTime = sdf.format(date);
holder.txtCreatedDate.setText(loginTime);
} catch (ParseException e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtUserName;
private TextView txtUserCpf;
private TextView txtAccountType;
private TextView txtCreatedDate;
public ViewHolder(View itemView) {
super(itemView);
txtUserName = (TextView) itemView.findViewById(R.id.txtUserName);
txtUserCpf = (TextView) itemView.findViewById(R.id.txtUserCpf);
txtAccountType = (TextView) itemView.findViewById(R.id.txtAccountType);
txtCreatedDate = (TextView) itemView.findViewById(R.id.txtCreatedDate);
}
}
}
Try this...
modify that getItemCount() to like this.
#Override
public int getItemCount() {
return listItems.size();
}
Try this
Create your adapter with empty list data
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
// your normal code
recyclerView = (RecyclerView) findViewById(R.id.userRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(userAccountsActivity.this));
listItems = new ArrayList<>();
// create new adapter
recyclerViewUserAdapter = new RecyclerViewUserAdapter(new ArrayList<>(), userAccountsActivity.this);
recyclerView.setAdapter(recyclerViewUserAdapter);
//your normal code
}
In your response
#Override
public void onResponse(String response) {
try {
List<ListItem_RecyclerView_User> responseList = new ArrayList<>();
JSONArray jArray = new JSONArray(response);
for(int i = jArray.length()-1; i>=0; i--){
JSONObject log = jArray.getJSONObject(i);
String name = log.getString("name");
String cpfNumber = log.getString("cpfNumber");
String type = log.getString("type");
String created_at = log.getString("created_at");
Log.d("Adapter: ", name + cpfNumber + type + created_at);
ListItem_RecyclerView_User item = new ListItem_RecyclerView_User (name, cpfNumber, type, created_at);
responseList.add(item);
}
/*recyclerViewUserAdapter = new RecyclerViewUserAdapter(listItems, userAccountsActivity.this);
recyclerView.setAdapter(recyclerViewUserAdapter);*/
// write method to update your data
recyclerViewUserAdapter.updateDataList(responseList);
} catch (JSONException e) {
e.printStackTrace();
}
}
In your adapter create method
public void updateDataList(List<ListItem_RecyclerView_User> newDatas) {
listItems.clear();
listItems.addAll(newDatas);
notifyDataSetChanged():
}
i've added a column (named load_more) to the end of a recyclerview and each time i touch this column it should download ten more columns from a url. the first time that I touch load_more, it works properly but for the second time, program crashes.
this is the code of loading data to the recyclerview:
public class category extends AppCompatActivity {
private String url = "http://iranradiotherapy.ir/api/get_category_posts/?include=title,date&id=";
private RequestQueue requestQueue;
private ArrayList<HashMap<String, String>> data;
private RecyclerView recyclerView;
private adapter adapter;
private int page_counter;
private String page_number;
private boolean flag;
private int current;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Bundle b = getIntent().getExtras();
final String cat_id = b.getString("category_id");
url += cat_id;
Toast.makeText(category.this, url, Toast.LENGTH_SHORT).show();
data = new ArrayList<>();
flag = true;
page_counter = 1;
page_number= "&page=";
requestQueue = Volley.newRequestQueue(this);
get_data();
}
private void get_data() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url + page_number + page_counter, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray posts = response.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
HashMap<String, String> hashMap = new HashMap<>();
JSONObject temp = posts.getJSONObject(i);
String post_title = temp.getString("title");
String post_id = temp.getString("id");
String post_date = temp.getString("date");
hashMap.put("post_title", post_title);
hashMap.put("post_date", post_date);
hashMap.put("post_id", post_id);
data.add(hashMap);
}
final HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("load_more", "load_more");
data.add(hashMap);
recyclerView = (RecyclerView) findViewById(R.id.category_recycler_view);
adapter = new adapter(data);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
flag = true;
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), new RecyclerTouchListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
if (data.get(position).equals(hashMap)) {
load_more();
}
else {
Intent i = new Intent(category.this, post.class);
i.putExtra("post_id", data.get(position).get("post_id"));
category.this.startActivity(i);
}
}
}));
} catch (Exception e ) {
Toast.makeText(category.this, "error 1 : " + e.toString(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(category.this, "error 2 : " + error.toString(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
}
private void load_more() {
if (flag){
flag = false;
Toast.makeText(this, "load more data " + current, Toast.LENGTH_SHORT).show();
page_counter++;
get_data();
data.remove(data.size() - 1 );
}
}
}
and this is the adapter :
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if (position + 1 == data.size()) {
holder.title.setVisibility(View.INVISIBLE);
holder.date.setVisibility(View.INVISIBLE);
holder.load_more.setVisibility(View.VISIBLE);
} else {
HashMap<String, String> d = data.get(position);
holder.title.setText(d.get("post_title"));
holder.date.setText(d.get("post_date"));
holder.load_more.setVisibility(View.INVISIBLE);
}
}