Android Custom ListView not building with Volley - java

I am parsing a JSON array from a public source and inserting them into a custom ListView. I have 4 classes;
MySingleton for Volley Requests
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}}
GameListItem
public class GameListItem {
private String itemName, itemLongDescription, itemShortDescription, itemReleaseDate;
String releaseyear;
private int iconID, imageID;
public GameListItem(String itemName, String releaseyear, int iconID) {
this.itemName = itemName;
this.releaseyear = releaseyear;
this.iconID = iconID;
}
public String getItemName() {
return itemName;
}
public String getReleaseyear() {
return releaseyear;
}}
CustomListAdapter
public class CustomListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private List<GameListItem> mDataSource;
public CustomListAdapter(Context context, List<GameListItem> items) {
mContext = context;
mDataSource = items;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mDataSource.size();
}
#Override
public Object getItem(int position) {
return mDataSource.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = mInflater.inflate(R.layout.gamelist_layout, parent, false);
TextView titleTextView = (TextView) rowView.findViewById(R.id.gameTitle);
TextView subtitleTextView = (TextView) rowView.findViewById(R.id.gameDescription);
titleTextView.setText(mDataSource.get(position).getItemName());
subtitleTextView.setText(mDataSource.get(position).getReleaseyear());
return rowView;
}}
MainActivity
public class MainActivity extends AppCompatActivity {
final String TAG = this.getClass().getSimpleName();
RequestQueue requestQueue;
private CustomListAdapter adapter;
private List<GameListItem> gameList;
private GameListItem gameItem;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cache cache = new DiskBasedCache(getCacheDir(), 1024*1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
listView = (ListView) findViewById(R.id.gameListView);
gameList = new ArrayList<>();
//This works and gives me a ListView with 5 rows
gameList.add(new GameListItem("Test 1", "2019", R.mipmap.ic_launcher));
gameList.add(new GameListItem("Test 2", "2092", R.mipmap.ic_launcher));
gameList.add(new GameListItem("Test 3", "3243", R.mipmap.ic_launcher));
gameList.add(new GameListItem("Test 4", "2323", R.mipmap.ic_launcher));
gameList.add(new GameListItem("Test 5", "2123", R.mipmap.ic_launcher));
//This doesn't work...
String url = "http://api.androidhive.info/json/movies.json";
JsonArrayRequest stringRequest = new JsonArrayRequest(url, new Response
.Listener<JSONArray>(){
#Override
public void onResponse(JSONArray jsonArray) {
if (jsonArray == null) {
Toast.makeText(MainActivity.this, "jsonArray is Null", Toast.LENGTH_LONG).show();
} else {
try {
Toast.makeText(MainActivity.this, "ATTEMPTING PARSING", Toast.LENGTH_SHORT).show();
Log.e("This is the json array", jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String title = jsonObject.getString("title");
int releaseYear = jsonObject.getInt("releaseYear");
gameList.add(new GameListItem(releaseDate, "" + releaseYear , R.mipmap.ic_launcher));
//this prints all the Titles and Release Dates correctly
Log.e("This is a title", title);
Log.e("This is a year", "HERE " + releaseYear);
}
} catch(JSONException e){
Log.i("JSON exception", e.toString());
Toast.makeText(MainActivity.this, "JSON ERROR", Toast.LENGTH_LONG).show();
}
requestQueue.stop();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("This is an error", ""+error.getMessage()+","+error.toString());
}
});
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
adapter = new CustomListAdapter(this, gameList);
listView.setAdapter(adapter);
}}
As commented in main activity, when i manually add a row (with manual data) the ListView builds and displays, but the parser isn't showing anything.
I checked countless of tutorials and everything seems in order, I even looked here and followed most of the answered I found yet nothing.
Can someone see the bug causing the list not to build/print?
PS. the Log.e prints as well (which is inside the parser)
Thank you!

If the request success, you must notify data change to adapter. In your case adapter.notifyDataSetChanged(); before catch block inside onResponse.

Add the following line after your requestQueue.stop statement:
adapter.notifyDataSetChanged();
At first, when you set the adapter, the list has no data, because the Volley request will be made asynchronously, that's why you need to let the adapter know when its data has changed.
From the documentation:
notifyDataSetChanged()
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

Related

How can I refresh RecyclerView after adding new data?

How can I refresh the RecyclerView after adding a new item, Note that I'm using a hub connection to send and receive messages from the back-end side, and I'm trying to use notifyItemRangeChanged to refresh the RecyclerView but it does not work,
Here is the adapter class:
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
public static final int MSG_TYPE_LEFT = 0;
public static final int MSG_TYPE_RIGHT = 1;
public static int i = 0;
private Context mContext;
private List<Chat> mChat;
private String imageurl;
public MessageAdapter(Context mContext, List<Chat> mChat, String imageurl) {
this.mContext = mContext;
this.mChat = mChat;
this.imageurl = imageurl;
}
#NonNull
#Override
public MessageAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if(viewType == MSG_TYPE_RIGHT){
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_right, parent, false);
return new MessageAdapter.ViewHolder(view);
}else{
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_left, parent, false);
return new MessageAdapter.ViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull MessageAdapter.ViewHolder holder, int position) {
Chat chat = mChat.get(position);
holder.show_message.setText(chat.getMessage());
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
holder.setItem_on_click_listener(new item_on_click_listener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(mContext, "Clicked", Toast.LENGTH_LONG).show();
}
});
}
#Override
public int getItemCount() {
return mChat.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
public TextView show_message;
public ImageView profile_image;
private item_on_click_listener item_on_click_listener;
public ViewHolder(View itemView) {
super(itemView);
show_message = itemView.findViewById(R.id.show_message);
profile_image = itemView.findViewById(R.id.profile_image);
}
public void setItem_on_click_listener(item_on_click_listener item_on_click_listener){
this.item_on_click_listener = item_on_click_listener;
}
#Override
public void onClick(View view) {
item_on_click_listener.onClick(view, getAdapterPosition(), false);
}
#Override
public boolean onLongClick(View view) {
item_on_click_listener.onClick(view, getAdapterPosition(), false);
return true;
}
}
#Override
public int getItemViewType(int position) {
i = position;
if(mChat.get(position).getSender() == "Sender Name"){
return MSG_TYPE_RIGHT;
}
else{
return MSG_TYPE_LEFT;
}
}
#Override
public void onAttachedToRecyclerView(#NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
recyclerView.refreshDrawableState();
}
}
Here is the main-activity class:
public class MainActivity extends AppCompatActivity {
Button sendBtn;
ArrayList<String> messagesList = new ArrayList<>();
HubConnection hubConnection;
EditText edt_text;
TextView txt_received;
MessageAdapter messageAdapter;
List<Chat> mChat;
RecyclerView recyclerView;
public void readMessages(String sender, String receiver, String message){
Chat chat = new Chat(sender, receiver, message);
mChat.add(chat);
messageAdapter = new MessageAdapter(MainActivity.this, mChat, "default");
recyclerView.setAdapter(messageAdapter);
messageAdapter.notifyItemRangeChanged(messageAdapter.getItemCount(), mChat.size());
}
#SuppressLint("WrongThread")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
mChat = new ArrayList<>();
messageAdapter = new MessageAdapter(MainActivity.this, mChat, "default");
recyclerView.setAdapter(messageAdapter);
sendBtn = findViewById(R.id.send_btn);
edt_text = findViewById(R.id.edt_text);
try {
hubConnection = HubConnectionBuilder.create("URL").build();
hubConnection.start();
AtomicReference<String> state = new AtomicReference<>("");
hubConnection.start().subscribe(() -> {
state.set("Connect");
},
error -> {
state.set("error");
});
hubConnection.on("SendMessage", (param1, param2, param3, param4) -> {
readMessages("Sender Name", "Receiver Name", "Message");
}, String.class, String.class, String.class, Integer.class);
}
catch (Exception e){
String exMessage = e.getMessage();
}
sendBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if(edt_text.getText().toString().length() > 0){
if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED){
hubConnection.start();
}
hubConnection.send("Method Name", "param1", "param2", "param3", "param4");
hubConnection.on("ReceiveMessage", (param1, param2, param3 , param4) -> {
readMessages("Sender Name", "Receiver Name", "Message");
}, String.class, String.class, String.class, Integer.class);
}
else{
edt_text.setHint("Can not be empty");
}
}catch (Exception ex){
String message ;
message = ex.getMessage();
}
}
});
}
}
Can anyone help me, please?
notifyItemRangeChanged will work if you add/remove the data in same instance of Adapter or any other notify method for that matter. The problem here is you are creating a new Adapter every time inside your readMessages method.
Create adapter only once and notify it as needed .. Since you already created the messageAdapter inside onCreate you can use the same instance.
public void readMessages(String sender, String receiver, String message){
Chat chat = new Chat(sender, receiver, message);
mChat.add(chat);
messageAdapter.notifyItemRangeChanged(messageAdapter.getItemCount(), mChat.size());
}

Refresh RecyclerView from BroadcastReceiver When TelephonyManager.EXTRA_STATE_RINGING

I have a tabLayout in which there is Log Fragment tab it store all the incoming calls details in LogsData List Object and set .
New callLogs is updated after App close and restart but I want to refresh my RecyclerView when the call is in ringing state.
My LogFragment
public class LogsFragment extends Fragment {
private RecyclerView mRecyclerView;
private ListAdapter mListadapter;
ArrayList data;
TinyDB tinyDB;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_call_log, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
tinyDB = new TinyDB(getContext());
data = tinyDB.getListObject("LogData", LogsData.class);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(layoutManager);
data = tinyDB.getListObject("LogData", LogsData.class);
mListadapter = new ListAdapter(data);
mListadapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mListadapter);
return view;
}
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(Object o) {
/* Refresh your adapter */
data = tinyDB.getListObject("LogData", LogsData.class);
mListadapter = new ListAdapter(data);
mListadapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mListadapter);
};
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private ArrayList<LogsData> dataList;
public ListAdapter(ArrayList<LogsData> data) {
this.dataList = data;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewNumb;
TextView textViewTime;
public ViewHolder(View itemView) {
super(itemView);
this.textViewName = itemView.findViewById(R.id.cName);
this.textViewNumb = itemView.findViewById(R.id.number);
this.textViewTime = itemView.findViewById(R.id.time_stamp);
}
}
#Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.log_list, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.textViewName.setText(dataList.get(position).getName());
holder.textViewNumb.setText(dataList.get(position).getNumber());
holder.textViewTime.setText(dataList.get(position).getTime());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Item " + position + " is clicked.", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return dataList.size();
}
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(Object o) {
/* Refresh your adapter */
data = tinyDB.getListObject("LogData", LogsData.class);
mListadapter = new ListAdapter(data);
mListadapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mListadapter);
};
}
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
}
And my PhoneStateReceiver Class
public class PhoneStateReceiver extends BroadcastReceiver {
public final static String TAG = "SSD";
String incomingNumber;
String state;
DateFormat timeFormat;
Context context;
String date;
String cName;
ArrayList data;
#SuppressLint("UnsafeProtectedBroadcastReceiver")
#Override
public void onReceive(Context context, Intent intent) {
TinyDB tinydb = new TinyDB(context);
data = tinydb.getListObject("LogData", LogsData.class);
try {
state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
cName = getContactName(incomingNumber, context);
timeFormat = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
date = timeFormat.format(Calendar.getInstance().getTime());
Log.d(TAG, date);
String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
data.add(new LogsData(cName, incomingNumber, date));
tinydb.putListObject("LogData", data);
Toast.makeText(context, date + " " + incomingNumber + " " + cName, Toast.LENGTH_SHORT).show();
}
EventBus.getDefault().post(new Object());
} catch (Exception e) {
e.printStackTrace();
}
}
public String getContactName(final String phoneNumber, Context context) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
String contactName = "";
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
contactName = cursor.getString(0);
}
cursor.close();
}
return contactName;
}
}
I update my codes please see it.
Use EventBus to do it
In your LogsFragment prepare subscriber like this:
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(Object o) {
/* Refresh your adapter */
data = tinyDB.getListObject("LogData", LogsData.class);
mListadapter = new ListAdapter(data);
mListadapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mListadapter);
};
And in your PhoneStateReceiver call your subscriber like this:
EventBus.getDefault().post(new Object());
Do not forget to register and unregister your subscriber in LogsFragment
You can read more about EventBus in here

How to change text in Fragment (outside recyclerVIew) from within RecyclerView viewHolder onClick

My cart
This is what i need.
Total value need to be updated when one item removed.
I managed to remove the item and get the total value as json respose in adapter viewholder . don't know how to set the update the text in Fragment.
this is my cart fragment
Cart.java
public class Cart extends Fragment {
public Cart() {
// Required empty public constructor
}
Context context;
Activity activity;
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
String GET_JSON_DATA_HTTP_URL = "http://192.168.0.106/slbros/index.php/get/cart?p_d_id=12&lan=en";
String Total_URL = "http://192.168.0.106/slbros/index.php/get/total?p_d_id=12";
String JSON_CDID = "cart_dtl_id";
String JSON_IMG_URL = "img_url";
String JSON_QTY = "qty";
String JSON_NAME = "name";
String JSON_UNIT = "unit";
String JSON_PRICE = "price";
String JSON_P_ID = "product_id";
JsonArrayRequest jsonArrayRequest;
RequestQueue requestQueue;
ProgressBar progressBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_cart, container, false);
TextView total_tv = (TextView)v.findViewById(R.id.total_payment_value);
//recycler view
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) v.findViewById(R.id.rv_cart_list);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar2);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(recyclerViewlayoutManager);
progressBar.setVisibility(View.VISIBLE);
JSON_DATA_WEB_CALL();
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Cart");
}
public void JSON_DATA_WEB_CALL() {
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.GONE);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getContext(), "some error....", Toast.LENGTH_SHORT).show();
}
}
);
requestQueue = Volley.newRequestQueue(getContext());
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(60000, 0, 1));
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImg_url(json.getString(JSON_IMG_URL));
GetDataAdapter2.setName(json.getString(JSON_NAME));
GetDataAdapter2.setPrice(json.getInt(JSON_PRICE));
GetDataAdapter2.setProduct_id(json.getInt(JSON_P_ID));
GetDataAdapter2.setCart_dtl_id(json.getInt(JSON_CDID));
GetDataAdapter2.setProduct_qty(json.getInt(JSON_QTY));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapterCart(GetDataAdapter1, getContext());
recyclerView.setAdapter(recyclerViewadapter);
}}
this is my adapter
RecyclerViewAdapterCart.java
public class RecyclerViewAdapterCart extends RecyclerView.Adapter<RecyclerViewAdapterCart.ViewHolder> {
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
String baseURL = "http://192.168.0.106/slbros/index.php/";
Context context;
Activity activity;
List<GetDataAdapter> getDataAdapter;
ImageLoader imageLoader1;
public RecyclerViewAdapterCart(List<GetDataAdapter> getDataAdapter, Context context) {
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImg_url(),
ImageLoader.getImageListener(
Viewholder.product_image_view,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.product_image_view.setImageUrl(getDataAdapter1.getImg_url(), imageLoader1);
Viewholder.product_name_TextView.setText(getDataAdapter1.getName()+" - "+String.valueOf(getDataAdapter1.getProduct_qty()));
Viewholder.product_price_qty_TextView.setText(getDataAdapter1.getPrice()+".00 Rs X "+String.valueOf(getDataAdapter1.getProduct_qty()));
Viewholder.product_t_price_TextView.setText(String.valueOf(getDataAdapter1.getProduct_qty()*getDataAdapter1.getPrice())+".00 Rs");
Viewholder.product_id_TextView.setText(String.valueOf(getDataAdapter1.getProduct_id()));
Viewholder.cart_dtl_id_TextView.setText(String.valueOf(getDataAdapter1.getCart_dtl_id()));
}
#Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public NetworkImageView product_image_view;
public TextView product_name_TextView;
public TextView product_price_qty_TextView;
public TextView product_t_price_TextView;
public TextView product_id_TextView;
public TextView cart_dtl_id_TextView;
public Button p_cancel_btn;
public ViewHolder(View itemView) {
super(itemView);
product_image_view = (NetworkImageView) itemView.findViewById(R.id.item_image1);
product_name_TextView = (TextView) itemView.findViewById(R.id.product_name);
product_price_qty_TextView = (TextView) itemView.findViewById(R.id.p_qty_price);
product_t_price_TextView = (TextView) itemView.findViewById(R.id.p_t__price);
product_id_TextView = (TextView) itemView.findViewById(R.id.product_id_tv);
cart_dtl_id_TextView = (TextView) itemView.findViewById(R.id.cart_dtl_id_tv);
p_cancel_btn = (Button)itemView.findViewById(R.id.p_cancel_btn);
// set item view
p_cancel_btn.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
String str = product_name_TextView.getText().toString();
//Toast.makeText(v.getContext(), str, Toast.LENGTH_SHORT).show();
String cart_dtl_id = cart_dtl_id_TextView.getText().toString();
String url = baseURL + "delete/select?cart_dtl_id="+cart_dtl_id;
jsonArrayRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//progressBar.setVisibility(View.GONE);
Toast.makeText(v.getContext(), "response -- " + response, Toast.LENGTH_LONG).show();
// here, I have to change the total TextView in Fragment
// Total_TextView.setText("1000rs")
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(v.getContext(), "error", Toast.LENGTH_LONG).show();
}
}
);
requestQueue = Volley.newRequestQueue(v.getContext());
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(60000,0,1));
requestQueue.add(jsonArrayRequest);
getDataAdapter.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
notifyItemRangeChanged(getAdapterPosition(),getDataAdapter.size());
}
}}
Thanks in Advance
add to ReyclerView.
private ItemsChangedListener itemsChangedListener;
public interface ItemsChangedListener {
void onItemsChanged(int sum);
}
public void setItemsChangedListener(ItemsChangedListener listener) {
this.itemsChangedListener = listener;
}
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//progressBar.setVisibility(View.GONE);
// Calc here the Value if you can.
if(listener != null) listener.onItemsChanged(sum);
}
Fragment
public class Cart extends Fragment implements ItemsChangedListener
recyclerView.setItemsChangedListener(this);
#override
public void onItemsChanged(int sum) {
//Update TextView
}

App works on WIFI but not on 3G

There are two activities: Main and Detail Activity.
Main Activity is basically a GridView.
Detail Activity is basically shows the clicked item's detail information. I am passing selected item's id (pid) from the Main to the Detail Activity.
I am facing an issue as follows. Initially, I have 3G connection (cellular connection) and clicked on the first item and see the corresponding item detail in the Detail Activity, it works perfectly fine, and go back to the Main Activity, then clicked on the second item, then unfortunately it still shows me the first item in the DetailActivity that I clicked initially.
I switched from 3g to wifi while app is on the active and open. No matter what I click, it still shows me the first item that I clicked initially.
But when I delete the app and reinstall it and get either wifi access only, the app works perfectly fine.
In the following implementation, Connection URL (PRODUCT_DETAIL_URL) is http, not https. I am using Volley library for the network connection.
DetailActivity.java
private void productDetailInit() {
// it is http
StringRequest postRequest = new StringRequest(Request.Method.POST, Config.PRODUCT_DETAIL_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
jsonObject = response;
loadJsonData();
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("id", productID);
return params;
}
};
RetryPolicy policy = new DefaultRetryPolicy(1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postRequest.setRetryPolicy(policy);
CustomVolleyRequest.getInstance(this).getRequestQueue().add(postRequest);
}
CustomVolleyRequest.java
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
private class BitmapCache implements ImageLoader.ImageCache {
private LruCache<String, Bitmap> mCache;
public BitmapCache() {
mCache = new LruCache<>(20);
}
#Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
// scaling bitmap for avoiding too much big images
Bitmap scaled = ImageUtil.getInstance().scaleBitmap(bitmap);
mCache.put(url, scaled);
}
}
public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
Adapter.java
class ProductMainAdapter extends ArrayAdapter<ImageRecord> {
private ImageLoader mImageLoader;
private String jsonObject;
ProductMainAdapter(Context context) {
super(context, R.layout.grid_item);
mImageLoader = CustomVolleyRequest.getInstance(this.getContext()).getImageLoader();
}
#NonNull
#Override
public View getView(final int position, View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
convertView.setBackgroundResource(R.drawable.round_gridview);
holder.priceTagImage = (ImageView) convertView.findViewById(R.id.priceTag_IV);
holder.textView = (TextView) convertView.findViewById(R.id.text);
holder.imageView = (NetworkImageView) convertView.findViewById(R.id.picture);
holder.priceTagRL = (RelativeLayout) convertView.findViewById(R.id.priceTag_RL);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
ImageRecord imageRecord = getItem(position);
holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openProductDetail(position);
}
});
holder.textView.setText(imageRecord != null ? imageRecord.getTitle() : null);
holder.priceTagRL.setRotation(0);
return convertView;
}
private class ViewHolder{
TextView textView;
ImageView priceTagImage;
NetworkImageView imageView;
RelativeLayout priceTagRL;
}
private void openProductDetail(int position) {
try {
ImageRecord imr = getItem(position);
String productID = imr != null ? imr.getId() : "0";
Intent intent = new Intent(getContext(), ProductDetailActivity.class);
intent.putExtra("pid", productID);
getContext().startActivity(intent);
} catch (Exception e) {
Log.e("openProductDetail", "exception", e);
}
}

Images in gridview fetched from database are not showing in fullscreen upon Clicking by using onItemClickListener

I am using GridView for Displaying my images from mysql Databse.
But, when I try to display the images in FullScreen upon clicking using OnItemClickListener the app does not crash, however it does not display my UI.
here is the code for MainActivity:-
public class MainActivity extends Activity implements OnClickListener {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://eventassociate.com/wedding/photomania";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private GridView gridView;
private CustomListAdapter adapter;
Button blackcapture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallary_activity_main);
overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
gridView = (GridView) findViewById(R.id.list);
blackcapture = (Button) findViewById(R.id.bottom_button);
blackcapture.setOnClickListener(this);
adapter = new CustomListAdapter(this, movieList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Movie m5 = movieList.get(position);
Intent i = new Intent(getApplicationContext(),
FullImageActivity.class);
i.putExtra("movieobject", m5);
startActivity(i);
}
});
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("No"));
movie.setThumbnailUrl(obj.getString("flinks"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
Code for CustomAdapter:-
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
#Override
public int getCount() {
return movieItems.size();
}
#Override
public Object getItem(int location) {
return movieItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.gallary_list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
// ...............................................
TextView title = (TextView) convertView.findViewById(R.id.title);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
return convertView;
}
Code for Movie Class:-
public class Movie implements Parcelable{
private String title,thumbnailUrl;
public Movie() {
// TODO Auto-generated constructor stub
}
public Movie(String name, String thumbnailUrl
) {
this.title = name;
this.thumbnailUrl = thumbnailUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = "http://eventassociate.com/wedding/"+thumbnailUrl;
}
// Parcelling part
public Movie(Parcel in){
String[] data = new String[2];
in.readStringArray(data);
this.title = data[0];
this.thumbnailUrl = data[1];
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {this.title,
this.thumbnailUrl,
});
}
}
And Code for FullImageActivity:-
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
Movie myParcelableObject = (Movie) i.getParcelableExtra("movieobject");
String alp = myParcelableObject.getThumbnailUrl();
Toast.makeText(getApplicationContext(), alp, Toast.LENGTH_LONG).show();
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
}
}
There are some issues in your FullImageActivity
You are initializing CustomListAdapter int the FullImageActivity why? It is not required here.
You got the position from the previous activity. It is better to get the id of the image from the bundle.
If you get the id from the bundle then hit the database and get the image based on the id and assign the image to imageview.

Categories