I'm developing an Android app which looks like Facebook. FeedsActivity.java has a ListView which show user's feeds. When the ListView item is clicked, it starts the activity ViewActivity.java
ViewActivity shows one targeted feed. If user clicks the like button in ViewAcitivity, like count will be updated and then if the user clicks the back button, FeedsActivity will be shown.
I want to update ListView item's like count without refresh.
Here is my logic
FeedsActivity.java
onCreate(){
// http request to get json feeds
// parsing json
// setAdapter
}
setOnItemClickListener(){
// intent.putExtra() // FEED DATA
// startActivity
}
ViewActivity.java
public void goBack(View v){
// intent.putExtra() // FEED DATA INCLUDE UPDATE LIKE COUNT
// startActivityForResult()
}
When I call startActivityForResult() in ViewActivity.java, onCreate() of "FeedsActivity.java" is called so my ListView gets updated. (request json feeds and setAdapter) :(
I want to update only like counts, such as Facebook app.
Is there any other good way?
please help me.
have a look at notifyDataSetChanged() of your adapter. this method basically tells the adapter that the provided data has changed and that it should update the ListView. so this will makes your ListView refresh and display new data
Related
I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks
I have a cart program, in activity is simple, there was a recyclerview and a button. In recycler view I can edit its stock.
Now, I want to get that product_name and product_stock from that I've ever clicked and make changes.
Now, when every stock has been clicked, I want click button on activity, so I want that data I've ever clicked on recyclerview stored to array, so that button can do their action / function. Can you guys lead me, how to import data from recyclerview to its activity itself.
It was Android program to do shopping cart, so I click the data in recycler view it stored to activity array.
I don't have any idea how to store that from recyclerview to activity's array.
My expected result, should be, when I click buy button on activity, every changed stock on recyclerview is shown.
You can simply implement an interface in your adapter like this for getting the values from clicked position in recycleview.
public interface FetchRecyclerViewItems{
void getItems(String product_name,String product_stock);
}
And simply create a setter method for this interface in adapter like this,
private FetchRecyclerViewItems fetchRecyclerViewItems;
public void setFetchRecyclerViewItems(FetchRecyclerViewItems fetchRecyclerViewItems){
this.fetchRecyclerViewItems = fetchRecyclerViewItems;
}
Then set the values like this on your click like this in OnBindViewHolder,
your_view.setOnClickListener(new View.SetOnClickListener)....{
....
fetchRecyclerViewItems.getItems(product_nameFromPosition,product_stockFromPosition);
}
And implement this interface in your activity and you will get the product_name,product_stock values there.
Make sure to initialize the FetchRecyclerViewItems in your activity.
I have a cart where user adds some products to his cart and if he want to leave from products page I kept an alert dialog saying your cart will be cleared! if u like it u can proceed and if user clicks ok his cart will be cleared.
Here problem is mCartList is my list that is in another activity and alert dialog which I used to clear mCartList is in another activity,whenever shopping cart activity on Create method is called the products that were previously added to cart were appearing again after clearing also.
What should I do to clear that list permanently.Here is the piece of code
shoppingcart.mCartList.clear();
shoppingcart.mProductAdapter.notifyDataSetChanged();
First, check the onCreate() method you were talking about, if you are adding some elements to the List there. Don't add objects there, just initialize the List and add objects only when some event is triggerd (ex. when user clicks some button or image to add an item to cart). If you're already doing this then try creating new List object
shoppingcart.mCartList = new List<...>();
instead of
shoppingcart.mCartList.clear();
You should provide a method like this inside you activity with list.
public void clearCartList(){
list.clear();
adapter.notifyDataSetChanged();
}
and call this method from another activity.
Scenario :
I am having a list fragment which displays a list of friends. When an list item is pressed, I'm showing the details of the friend in a dialog fragment.
Problem:
In the dialog fragment, I'm having an option to delete the friend from my friend list (in a remote server) . I'm using Async tack to perform this. After returning from the dialog fragment, how should I update the 'old' friend list. How do I trigger a fragment 'reload'. Since the async task is not aware of the custom list adapter being used in the list fragment.
Sorry, I will add the code ASAP. If anyone have met this scenario before kindly provide your suggestions.
Edit: After successfully deleting a friend from the friend list in a remote server, I want to reload the list being displayed in the list fragment. Is there a way to trigger the dataSetChanged notification in the list fragment itself.
So you will be populating the ListView with an ArrayList.
Delete the Item from the ArrayList with index = ListViewItemIndex
clicked.
call notifyDataSetChanged on the Adapter you are using.
No problem. I'm guessing the ListFragment is reading from some data source - be it an ArrayAdapter or a SimpleCursorAdapter (or something similar) - and your task is also modifying that source. When the AsyncTask finishes, you should just be able to get that fragment and update the underlying data. For example:
// ...
void onPostExecute(Void result) {
ListFragment lFrag = (ListFragment) getFragmentManager().findFragmentById(R.id.yourListFragment);
BaseAdapter adapter = (BaseAdapter) lFrag.getListAdapter();
adapter.notifyDataSetChanged();
}
I am developing a new app for android 2.2(Froyo), I need to know how to forcefully display a leyout before loading it with dynamic data. I have a LinearLayout with a empty List, wen the appropriate menu is selected I load it with dynamic data which takes some time to load, but wat happens is the screen is empty till the layout is filled with data. I need to display the empty layout which has ly title and show a ProgreesDialog till the list is filled with data. Here is my Activity class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deals);
loadDeals();//fills the list with data(URL call)
}
You can user AsyncTask for loading data. During which you will be able to show ProgressDialog as well
http://developer.android.com/reference/android/os/AsyncTask.html
Use AsynchronousTask Class and call loadDeals() from doInBackground Method.Just capture the data on that method.Then work ur UI in postExecute method.It will solve your problem