Error when adding a List to custom adapter - Android - android

I have a class that takes 4 parameters for a List. The class is called Item. I created a List variables that is an ArrayList . I then create a new ListAdapter. I set this adapter to the main ListView. I then add 10 inputs into the the ArrayList. This all works but when I try to add the ArrayList Items to the ListAdapter is gives me an error saying to add a Cast to the method.
Here is the code that I am talking about
final List <Item> tempList = new ArrayList <Item>();
mainListViewListAdapter = new ListAdapter(MainActivity.this, R.layout.main_list_item_layout, tempList);
// set mainListAdapter
mainListView.setAdapter(mainListViewListAdapter);
for (int x = 0; x < 10;x++){
tempList.add (new Item ("Example1","Example2","Example3","Example4"));
}
***HERE IS WHERE I GET THE ERROR AT addAll();***
mainListViewListAdapter.addAll(tempList);
mainListViewListAdapter is set to ListAdapter, and mainListView is set to ListView.
Why would I add a cast to mainListViewListAdapter? I have run this code on another project and I didn't have to use a cast.

Need destroy adapter - after using and configure each time use - new StringArray of Values for Adapter and new Adapter.
Use about this constructionale:
String[] values1 = new String[] { "Sunny", "Sealand", "Lengord" };
makeListItem(values1);
public void makeListItem(String[] addValues){
MySimpleArrayAdapter lvAdapter1 = new MySimpleArrayAdapter(this, addValues);
ListView1.setAdapter(lvAdapter1); lvAdapter1 = null; }

Related

Adding an object to List View

So far no luck. Been trying to add an object to my listview with no luck. After I add it should just out put the name of the assignment even though I set the object with other parameters such as grade and category. This is code is placed into a fragment java class that outputs the listview
private void populateListView(){
TeamArrayList = new ArrayList<CourseWorkItem>();
CourseWorkItem myItem = null;
myItem.setName("First Assignment");
myItem.setCategory("Homework");
myItem.setGrade(60);
TeamArrayList.add(myItem);
//creates an adapter
itemArrayAdapter = new ArrayAdapter<CourseWorkItem>(this,R.layout.individualview,TeamArrayList);
ListView v = (ListView) findViewById(R.id.courseListXML);
v.setAdapter(itemArrayAdapter);
}
Have you overriden the toString method of CourseWorkItem to display the name?
you are using default array adapter then you should pass the value as String[] ,otherwise you should create a custom adapter for this
try this for default array adapter
String[] strings=new String[]{"First Assignment","Second Assignment"};
//creates an adapter
itemArrayAdapter = new ArrayAdapter<CourseWorkItem>(this,R.layout.individualview,TeamArrayList);
ListView v = (ListView) findViewById(R.id.courseListXML);
v.setAdapter(itemArrayAdapter);

updating Listview when sqlite updated

Hi guys is it possible that everytime I add an item in the database it will notify and update the listview? For example I have two fragments in one activity then When I send a data in the database from the Fragment A the Listview in Fragment B will update also without using interface.
There is a function of ArrayAdapter called - notifyDataSetChanged()
//getting the list view
ListView userListView = (ListView) findViewById(R.id.users_ListView);
//creating an array to represnt what ever you want
ArrayList<String> users = new ArrayList<>();
for (int i = 0; i < 10 ; i++) {
//populate the array
String s = "user " + i;
users.add(s);
}
//setting up adapter to the array (this is 1 row with 3 arguments)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
users);
//connecting the list view to get the data to show from the adapter
userListView.setAdapter(adapter);
//changing the arrayList by adding or subtracting
String s = "another user";
users.add(s);
//update the adapter and list view with this command
adapter.notifyDataSetChanged();
Share your code if you are having any trouble.
good luck =)

ArrayAdapter and ListView never updating

I'm attempting to get method to handle updates to the ListView when SetWeatherData is called. Nothing ever shows up in my listview below. Any ideas? _rootView points to the right root and ListView comes back not null. m_weatherdata has a couple string elements in it.
Note the initial set of data does not show up either. Just blank.
I'm thinking it should be easier to setup a generic method to update a ListView when the data changes using straight up code.
private ArrayList<String> m_weatherdata;
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata = _weather;
UpdateWeatherUI();
return;
}
ArrayAdapter<String> m_adapter = null;
private void UpdateWeatherUI()
{
if ( m_adapter == null ) {
m_adapter = new ArrayAdapter<String>(
this.getContext(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
m_weatherdata);
View _rootview = this.getLayoutInflater(null).inflate(R.layout.fragment_main, null, false);
ListView _listview = (ListView) _rootview.findViewById(R.id.listview_forecast);
_listview.setAdapter(m_adapter);
}
else
{
m_adapter.notifyDataSetChanged();
}
}
You are assigning a new ArrayList to your dataset.
m_weatherdata = _weather;
Instead add items to the dataset. Like this
m_weatherdata.addAll(_weather);
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata.addAll(_weather);//change here
UpdateWeatherUI();
return;
}
When you set an adapter there is an observer attached to the
underlying data. So notifyDatasetChanged() only works if you only
modify the data in it.
If you want to clear all data from your dataset before adding new items to it, use the clear() method of ArrayList
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata.clear();//change here
m_weatherdata.addAll(_weather);//change here
UpdateWeatherUI();
return;
}
m_weatherdata = _weather; // updates the local variable with new set of data. but adapter doesn't know about the changes made as you have created the instance of the adapter with array list by the following line of code.
m_adapter = new ArrayAdapter<String>(this.getContext(), R.layout.list_item_forecast,R.id.list_item_forecast_textview,m_weatherdata);
In you want to updated the data either call
m_adapter.addAll(newsetofstringtobeadded);
or
Create new adapter
This will update your list.

When I add a new item in Listview previous values ​​are deleted

When I add a new item in my ListView it is losing the values ​​that existed. It appears only the new value that was added. The goal is to add a new value without losing existing ones.My Code:
List<string> list = new List<string>();
void btnAddItem_Click(object sender, EventArgs e)
{
ArrayAdapter<string> adapter;
string edttxNewItem = FindViewById<EditText>(Resource.Id.newItem).Text;
ListView listItems = FindViewById<ListView>(Resource.Id.ListOfItems);
list.Add(edttxNewItem);
adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);
listItems.Adapter = adapter;
listItems.DeferNotifyDataSetChanged();
}
I am unfamiliar about xamarin,but it seems that you are trying to create listview instance each time on btnAddItem_Click.
So initialise listItems in oncreate method as
listItems = FindViewById<ListView>(Resource.Id.ListOfItems);
and define listItems as class variable.
And also set adapter outside of btnAddItem_Click,let say in OnCreate method.
adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);
In btnAddItem_Click just add edttxNewItem in listItems and do notifydatasetchanged equivalent in xamarin.
Every time you click in the button you are instantiating a new arraylist (List list = new List();). So, every previous values in the array are deleted

Android null pointer exception on ArrayList for ListView

I have a listview that shows the contents of an arraylist. I'm using a simple adaptor to make this possible like so.
public static ArrayList<String> homeScreenContacts = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
The second line is giving me a null pointer exception. I thought about it and I decided it was because the arrayList is empty. So I added the following line between the arraylist declaration and the arrayadaptor declaration...
NewContact.homeScreenContacts.add("A Contact");
This solved the problem and my code worked fine but Now the list view shows "A Contact" and I dont want it to. Is there anyway to get rid of the null pointer exception problem but still have the arraylist empty? Because I want to populate it with user made contacts, not hard-coded, random strings. Thank you.
EDIT: Sorry, The arraylist is located in another class called NewContact, also, I am very beginner Android Programmer I just started.
Simple solution just don't initialize the ListView if there is no element in the ArrayList or the ArrayList is null.
if(NewContact.homeScreenContacts != null && NewContact.homeScreenContacts.size() > 0){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
listView.setAdapter(adapter);
}
Also you need to remember that if you you haven't initialize Adapter then dont initialize the ListView and before any operation on list view you should check is it null or not.
As you have said that you want to populate when user add some contact in the application then on add event only you need to populate or update the ListAdapter.
Hope this solution will resolve your problem.
Try this code
public class YourActivity extends Activity
{
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this, you already have yours)
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
lv.setAdapter(arrayAdapter);
}
}
Works fine for me:
if(arrayList.isEmpty())
{
listView.setAdapter(null);
}
else
{
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(listAdapter);
}

Categories

Resources