Update ListView on item delete or rename - java

I have the following code in a tabbed Fragment:
private SetRowsCustomAdapter adapter;
private ArrayList<SetRows> rowsArray = new ArrayList<SetRows>();
for (Map.Entry<String, String> current_file:filesInFolder.entrySet()) {
rowsArray.add(new SetRows(R.drawable.ic_launcher, current_file.getKey().toString(), current_file.getValue().toString()));
}
adapter = new SetRowsCustomAdapter(getActivity(), R.layout.customlist, rowsArray);
dataList = (ListView) mFrame3.findViewById(R.id.lvFiles);
dataList.setAdapter(adapter);
}
getActivity().deleteFile(txt + ".trp");
adapter.remove(adapter.getItem(info.position));
adapter.notifyDataSetChanged();
startActivity(new Intent(getActivity(), MainActivity.class).putExtra("tab", 2));
which deletes a row within my ListView and starts a new activity to show the changes. Although I have the adapter.notifyDataSetChanged(), it doesn't display the changes once a row is deleted without the last line on the code above.
Can someone please help me fix that so it shows the changes without starting a new activity.

Use of custom adapter is better for it, these link will help you for it
Remove ListView items in Android
How to remove arraylist item and then update listview
I think you require to delete from arraylist then call adapter setNotifyDataChange(), b'cos when you call this function it reload the adapter from arraylist.

Related

When press back button listview going top. How to fix that?

In my codes there is two fragment. List page and detail page. There is lot of alphabetic item on list page. And when i clicked some item opening the detail page. But when i press back button, the list doesn't stay same position. It goes to the top. I searched some solution but i think that's not common issue. I couldn't find different answers. Is there anyone to help me?
At first glance, it looks like it's one of your calls to: dictionaryFragment.resetDatasource(source);
Which is resetting the Adapter on your ListView to a brand new ArrayAdapter. This will reset the ListView to the top because the adapter is an entirely new object. The ListView no longer has any idea about the old ArrayAdapter and thus resets to the top.
public void resetDatasource (ArrayList<String> source){
mSource=source;
adapter = new ArrayAdapter<String>(getContext(),R.layout.kelimelistesi, mSource);
dicList.setAdapter(adapter);
}
Rather than calling
adapter = new ArrayAdapter<String>(getContext(),R.layout.kelimelistesi, mSource);
dicList.setAdapter(adapter);
You should do more like this:
public class DictionaryFragment ... {
private Adapter mAdapter;
public View onCreateView(...) {
// Init the layout here
// Put some other ArrayList here if you have one initially, else an empty one to start
mAdapter = new ArrayAdapter<String>(getContext(), R.layout.kelimelistesi, new ArrayList<String>());
dicList.setAdapter(mAdapter);
}
public void resetDatasource(ArrayList<String> source) {
mAdapter.clear();
mAdapter.addAll(source);
mAdapter.notifyDataSetChanged();
}
...
}
This difference now is that the same mAdapter instance is bound to dicList the whole time, but only the underlying data to display changed. Think of a ListView/RecyclerView's Adapter as a class that takes some input data and binds it to some UI elements. In this case, each String in the ArrayAdapter is displayed in some UI element like a TextView or what not.
ALSO important: note that we call notifyDataSetChanged() AFTER we have cleared the old data and added all the new data. This will help avoid flickering and scrolling issues from trying to elements one at a time.

Application crashing when click on any item in ListView

I am a beginner in Android studio and I am having hard time to debug this issue.
when any item in my ListView gets clicked the application crashes, here is the class for the OnItemClick. The objective is to change the ListView from the Categories to the items inside the categories. The Categories and items are stored in a HashMap <String, String[]>, so passing the value of cat.toString() should return the string containing the category. Lastly using myList (Stored outside of the function) should return an array of string containing the items. However, when I click any of the items the application instantly crashes. Thanks for your help!
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Choosen category
TextView cat = (TextView) view;
ArrayList<String> tempItems = new ArrayList<String>();
//Toast.makeText(this, cat.getText().toString(),Toast.LENGTH_SHORT).show();
listAdapter.clear();
for(String val : myList.get(cat.getText().toString())){
tempItems.add(val);
}
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,tempItems);
listAdapter.notifyDataSetChanged();
}
Variables
ListView myViewList;
Map<String, String[]> myList = new HashMap<String, String[]>();
ArrayAdapter<String> listAdapter;
Populating the ListView
//Creating a list of all categories
Set<String> myListKeys = myList.keySet();
ArrayList<String> categories = new ArrayList<String>();
for(String val : myListKeys){
categories.add(val);
}
//String[] categories = myListKeys.toArray(new String[myListKeys.size()]);
//Populating list
myViewList = (ListView) findViewById(R.id.groceryList);
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,categories);
myViewList.setAdapter(listAdapter);
myViewList.setOnItemClickListener(this);
You are getting exception because you are using listAdapter.clear(); which tries to clear the List inside the adapter. But apparently you have passed an array (String[] categories) which cannot be cleared. You should convert the array to ArrayList, (not just List) by iterating through it and pass it into the adapter.
You should create the adapter by passing ArrayList
What do you think you're doing by creating a new adapter inside the onItemClick of old adapter?
Inside the onItemClick, don't create a new adapter. Instead, after clearing the adapter and creating a temp ArrayList, add all the temp ArrayList to the adapter.
listAdapter.addAll(tempItems);
Also to get text from a TextView, use cat.getText().toString(); instead of cat.toString();

how to refresh Material Spinner in activity?

Using API through I was adding data in spinner that are added but that are not displayed in spinner at a time, so how can I refresh activity and newly added data to show at a time? i can used this link ....https://github.com/jaredrummler/MaterialSpinner/blob/master/README.md
(Schoolschool:userDetails.getSchools()) {
schoolList.add(school.getSchoolName());}
schoolDropDown.setItems(schoolList);
schoolDropDown.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
#Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
}
});
Use this method
adapter.notifyDataSetChanged();
You need to define the Adapter using setAdapter(...), for the Spinner, too. This defines the layout to be used for each individual item. i.e.
schoolDropDown.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_dropdown_item_1line, schoolList));
You only need to call setAdapter() once and you call adapter.notifyDataSetChanged() to update the data.
You need to set adapter to the spinner for displaying data
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, schoolList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schoolDropDown.setAdapter(dataAdapter);
After data change you need to call
dataAdapter.notifyDataSetChanged();
Try like this:
cityAdapter = new ArrayAdapter<>(frgmActivity, android.R.layout.simple_spinner_dropdown_item, listCityNames);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCitys.setAdapter(cityAdapter);
Or
Try below two lines :
String[] schoolsArr = schoolList.toArray(new String[schoolList.size()]);
schoolDropDown.setItems(schoolsArr);
Instead of this line: schoolDropDown.setItems(schoolList);

How to invalidate ListView for new data when using ListAdapter

I am making use of a default listview in a fragment to show data that is being called from a URL. There is a SwipeRefreshLayout where the ListView is contained. However when I refresh, the old data is still in the listview and the new data is just added at the top of the list. Is there a way to invalidate the old data?
I have made use of a ListAdapter which is created from the SimpleAdapter class like so:
ListAdapter adapter = new SimpleAdapter(getActivity(), newsItemList, R.layout.list_item, new String[]{TAG_TITLE, TAG_DATE, TAG_OWNER}, new int[]{R.id.news_title, R.id.news_date, R.id.news_owner});
lvGeneralNews.setAdapter(adapter);
I have tried to make the adapter null or use the invalidate() method that comes with the listview. The adapter does not have a notifyDataSetChanged() method.The newsItemList is an ArrayList that has the data from the URL. It is declared like so:
ArrayList<HashMap<String,String>> newsItemList;
the important is :
do you clear ArrayList> newsItemList when you get new data?
if u do
just repeat:
ListAdapter adapter = new SimpleAdapter(getActivity(), newsItemList, R.layout.list_item, new String[]{TAG_TITLE, TAG_DATE, TAG_OWNER}, new int[]{R.id.news_title, R.id.news_date, R.id.news_owner});
lvGeneralNews.setAdapter(adapter);
Try using adapter.clear(); put it in the method that is executed when you perform a refresh!

How to I add an image to list view items in Android?

I'm trying to add a different icon to each of my list items but I'm having trouble. The idea was to have each of the list view items together to make editing easier but adding an image is proving to be more complicated than I thought.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "title", "description" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Title 1", "Description 1"));
list.add(putData("Title 2", "Description 2"));
list.add(putData("Title 3", "Description 3"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
This is if not impossible, then at least quite troublesome at your current setup.
As the name states a SimpleAdapter is a basic class which provides you with basic functionality. That is, usually - a list with text views.
When you create the adapter you specify an exact layout for every single item in it (that's your android.R.layout_simple_list_item_2). You cannot push any additional items there (unless you're really stubborn).
What you need:
A custom made adapter (preferably)
A custom made layout for the adapter
A data source in the adapter which will map specific icons to every element.
Here is a nice demo: http://hmkcode.com/android-custom-listview-titles-icons-counter/
you can use custom adaptor in listview . so can customize your row.
check this
Currently using simple_list_item_2.xml for ListView. this layout contains only two TextView's. so it's not possible to show image in ListView row's using simple_list_item_2.xml layout
How to I add an image to list view items in Android?
Should create a custom adapter :
1. Creating a custom layout with required views which want to show in each listview row like with TextView,ImageView,...
2. Create a custom adapter class by extending SimpleAdapter class to change behavior of getView method for showing images and textview from data-source
See following tutorial for reference :
ListView with Images and Text using Simple Adapter in Android

Categories

Resources