Refresh List From Another Activity - android

I have 3 activities and 1 class;
MobilEpostaActivity (Main Activity) gives the datas from the Gmail server intent these datas to ListeleActivity (Fill and show the ListView) and ListeleActivity fills these datas to the ListView and when item onClick on ListView the item's click datas is intent to the GoruntuleActivity (Show the onclick item's information). And also I have a class "Baglantı" (means Connection) and it has all other methods which are necessary for each.
My problem is in here: when the user click on to the "Delete" button. I want to refresh the listview and show the next mail to the user. when user clicks to the delete button, I can show the user next mail with add 1 to the position and show it. But it is not work properly, because of its attachments are complicated.
Now starting with the MobilEpostaActivity; I am taking the username and password from the user and pass them to Baglantı class for connection to the server. Then I am gettin the result body, from, subject in arraylists. Then I pass these body, from and subject to the ListeleActivity with intent.
public void epostaListeleme() throws MessagingException, IOException
{
final Intent intent = new Intent(this, ListeleActivity.class);
intent.putStringArrayListExtra(bodylistesi ,(ArrayList<String>) getBodyList());
intent.putStringArrayListExtra(konulistesi ,(ArrayList<String>) getKonuList());
intent.putStringArrayListExtra(kimdenlistesi ,(ArrayList<String>) getKimdenList());
startActivity(intent);
}
In the ListeleActivity():
Getting the datas from the MobilEpostaActivity with Bundle. Filling the listview with "from" data. And waiting for listItemOnClick. If Onclick Action is happen then pass the datas to the GoruntuleActivity.**
In the GoruntuleActivity():
Getting the datas from the ListeleActivity with Bundle. And (Here is the important part) waiting for the Delete Button OnClick. If On Onclick Action is happen FIRST) delete the position from the ListView.
arrayAdapter.remove(arrayAdapter.getItem(position));
listBaglanti.RefreshedPositions(position);
arrayAdapter.notifyDataSetChanged();
SECOND) Delete the position's datas from the bodylist , fromlist and subjectlist:
public void RefreshedPositions(int position)
{
list.remove(list.get(position));
kimdenlist.remove(kimdenlist.get(position));
konulist.remove(konulist.get(position));
}
THIRD) Hold these "changed" lists in arraylists:
bodylistRefresh = baglan.list;
kimdenlisteRefresh = baglan.kimdenlist;
konulisteRefresh = baglan.konulist;
FOURTH) Set these "changed" lists to the MobilEpostaActivity's intent (the one of ListeleAcitivity's):
MobilEpostaActivity mobilObject = new MobilEpostaActivity();
mobilObject.setKonuList(konulisteRefresh);
mobilObject.setKimdenList(kimdenlisteRefresh);
mobilObject.setBodyList(bodylistRefresh);
As you think the process should start over again. But it doesn't start because with the Fourth step, I don't think so I made a refresh for the ListView.
Maybe I went a way which one is hard. But I hope, you tell me the right thing for refresh the listview and show the next mails. Also my screenshots;
When I first open the application : There is 3 mails from Merve. Positions : 0,1,2*
After Click the Delete Button : The ListView is refresh and the position=1's item is deleted. But it is not deleted on GoruntuleActivity's Screen.

It looks like you have too many lists involved. I think that you should be able to just remove the item from the main list like so:
arrayAdapter.remove(position);
listBaglanti.RefreshedPositions(position);
arrayAdapter.notifyDataSetChanged();
public void RefreshedPositions(int position)
{
list.remove(position);
kimdenlist.remove(position);
konulist.remove(position);
}
Just pass in the position as an int value instead of retrieving and then removing the object.

Related

How can I update the item of recyclerview when I come back to list in android

In my app I have an Activity and in this Activity, it shows a list of items with the help of RecyclerView, Adapter and fetches the items from a database.
In the database I have a table called Place with 7 Fields one of the fields is interest with Default Value 0 and Integer data type.
When I click an item in the list, this opens new Activity with all details of that item. One of the details is interest. The c
value of interest with Star icon. If the Value is 0 icon is Black otherwise it is Gold.
That part works fin. When I click the Star icon, the icon and the value of interest field in database successfully changes. However, my issue is :
When I come back to the Activity displaying the list of items and go back to that particular item, the changes of Star icon are not reflected. Example: first the icon is black, I change the icon by clicking on it to change to gold and update the value of interest field to 1. Only when I close the app completely and re-open again, the changes are shown correctly.
Any ideas?
You have to pass back changed Place object from All Details Activity to items list Activity and then notify and change the item in the adapter.
First, you have to start an activity with listening on results:
Intent intent = new Intent(this, AllDetailsActivity.class)
intent.putExtra("place", place);
startActivityForResult(intent, 1000)
When you change and confirm changes on All details screen you have to return changed object and result like this:
setResult(Activity.RESULT_OK, new Intent().putExtra("returned_place", place));
Then you have to implement onActivityResult inside Activity where you have a list of objects like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(request == 1000 && resultCode == Activity.RESULT_OK) {
adapter.changeItem(position, (Place) data.getExtras().get("returned_place"))
}
}
And adapter function for changing items should be something like this:
public void changeItem(int position, Place place) {
items.set(position, place);
notifyItemChanged(position);
}
You also have to save the position of the clicked Place object to know which cell you have to update.
You can call the notifyDataSetChanged() method from your adapter if you are not sure at what position of your data in the recyclerView you are in.
It is however better to call the notifyItemChanged(position) method from your adapter to only update the view for that specific recyclerView object.

Update list view from 2nd activity | Android

hi i have RecyclerView in that when user click on one row i am making changes in ui and updating view now i want when user click one one icon of that row it opening 2nd activity in that i am passing object of that position . and on 2nd activity if tick it i am not able to update view of that RecyclerView .. any way ? ya by doing static arreylist i can do it but cant do static as there is some problem .. part form static other way
icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent n = new Intent(mContext, PlayerInfo.class);
Players mData = (Players) v.getTag();
((Activity) mContext).startActivityForResult(n, 1111);
}
});
As you code shows,you can start second activity by call startActivityForResult and pass your data to it.
when data changed in 2nd activity,you save the new data in one object and when the activity finishes,you pass it to first activity,and you also need to add some code to handle the new object from 2nd activity in onActivityResult,just copy data to your old data for specific position.

Android - setting the background color of a listView's child in a Fragments onResume function gives a NullPointerException

Good day.
I am building an Android Application. In my main activity, I have a FragmentPageAdpater and what I basically do is I have this reusable Fragment class that I instantiate three times.
My fragment contains data for three different "pages". I use the current "Active Tab" in my action bar to be able to figure out and filter which data I should show the user. The data is basically a list (dogs, cats, birds) and the user can select a breed. The dog fragment starts first and when the user selects one, my application highlights the selected row and proceeds to the next one (cat fragments). The process repeats until the user is able to select a breed from all three categories.
In order to highlight the user selection, I do this in my onItemClickListener in my ListView
view.setBackgroundColor(Color.GREEN);
where view is the View viewargument in the onItemClickListener function.
However, what happens is when I get to the birds fragment and select then scroll/swipe back to the dog fragment, the green highlight disappears.
What I did try was to have an int to mark which breed index was selected, and then on the Fragment's onResume, fetch the appropriate int marker and then get my listView, get the child at that row, and then set the background colour. However, I am encountering a NullPointerException in my block of code as such:
This is my onResume function in the Fragment:
#Override
public void onResume() {
super.onResume();
//I get my MainActivity/the base one, get the view pager, and get the current item
int index = ((MainActivity)getActivity()).mViewPager.getCurrentItem();
Log.d("hi", "index is = " + index);
switch(index){
case 0:
//dogSelected is the index of the selected item in the dogFragment
if(((MainActivity)getActivity()).dogSelected > -1){
Log.d("hi","selected dog = " + ((MainActivity)getActivity()).dogSelected);
//I reinitialize the listView
listViewBreeds = (ListView) rootView.findViewById(R.id.listViewBreeds);
adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, DogBreeds);
//DogBreeds is an ArrayList of DogBreeds
listViewBreeds.setAdapter(adapter);
//check for null
if(listViewBreeds == null){
Log.d("hi", "hello, lvn NULL");
}
int pos = ((MainActivity)getActivity()).dogSelected; // + 1;
listViewBreeds.getChildAt(pos).setBackgroundColor(Color.GREEN);
}
break;
case 1:
//same as case 0
}
}
As you can see in my code, during the onResume of the Fragment, I check which fragment index is active so I know which list to look up in my switch-case statement. The index logged are correct. Then based on the index, I then look up my lists and the index saved in the base activity. The selected dog (or cat) is correct. After that I reinitialize the listView to make sure it's not null (the log statement does not show up). I then get the listView, get the child at the selected position and then set the background colour.
However, I am getting a NullPointer at the line:
listViewBreeds.getChildAt(pos).setBackgroundColor(Color.GREEN);
and I don't know which part of that is null.
Can anyone help me with this issue? I have a feeling I'm overlooking something simple and I can't figure it out.
Thank you for any help.
I actually got it to work already. The solution was to use a CustomListView Adapter and then create an object for the row entry. That object has a boolean isSelected, then in the Custom ListView Adapter, the background is set to a different color or is set back to white depending on the isSelected variable.

Getting a particular detail from XML file in android

I am working on a CRM app in android, in which, I am showing details of all contacts in a list view. Now, my requirement is when I click on a particular item in the list, it should only display the details about selected contact, such as, name, address, email, etc.. The data is coming from XML file, which I am parsing using SAX Parser. How can I query a XML to get selected data?
You are filling the ListView using Adapter right? Now you can get the item at the selected view inside the ListView and pass this item to an Activity.
E.g. inside your Adatper class implement the onItemClickListener:
public void onItemClick(AdapterView<?> a, View v, int position, long l) {
// Remembers the selected Index
Data item =getItem(position);
Intent intent = new Intent(getApplicationContext(), DetailedActivity.class);
intent.put("object",item);
startActivity(intent);
}
Note: the item "Data" class should implement the Parsable interface so it can be passed to the Activity in your DetailedActivity onCreate method get that object and update the UI based on its Values.

passing data from a view to an activity

I am making an android app in which i have an activity X that displays a list and a button. Activity X calls a listview to display that list. Each list item has a number(textview) and a checkbox. I used a setonclicklistener on the checkbox, so whenever the checkbox is checked i am storing the number associated with it in a string. Now i want that whenever i click the button the msg activity should start and the numbers to be sent are the ones that are checked.
I am using the following code to start the msg activity in my X activity.
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.fromParts("sms", msgnumbers, null));
msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(msgIntent);
Now "msgsnumbers" variable is present in my listview. How do I pass it to this activity X??
I found the same question here but with no appropriate solution.
-Thanks in advance
Intent in = new Intent(Quote.this, Purchase Ysn.class);
in.putExtra("price", salesprc);
public static String price = "price";
if (getIntent().getExtras().containsKey(price)) {
purces_nbcpy = getIntent().getExtras().getDouble(price);
}
onItemClickListener for ListView has a param position that tells you what position has been clicked.
so if you are using an ArrayList (for eg) to provide values for listItems in adapter you can use this inside onItemClickListener
MyBeanObject object=arraList.get(position);
//use getters of object to retrieve values and pass it as intent
//where arrayList may be your list of objects MyBeanObject

Categories

Resources