I have written the following code snippet in my App:
public Fragment getItem(int position) {
switch(position){
case 0:
LP_Events_Tab mEventsTab= new LP_Events_Tab();
mList= (ListView) findViewById(R.id.event_list);
String[] temp= {"1","2","3"};
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, temp);
return mEventsTab;
/*Other cases*/
}
return null;
}
For case 0, I am using an array of strings named temp to initialize the ArrayAdapter. However, Android Studio redlines the ArrayAdapter initialization arguments: (this,android.R.layout.simple_list_item_1, temp) and thus, I cannot proceed. I do not see any error with these arguments but Android Studio does and hence, I am stuck.
Replace the following line of code inside your Fragment -
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, temp);
Hope this helps, let me know if you need any more help.
The problem is with your Constructor of Adapter
ArrayAdapter(Context context, int resource, List<T> objects)
So make sure you are passing right parameters, in your case your first parameter is this which will not work other then in Activity.
So pass either getContext or getActivity.
Try to replace "this" with "MainActivity.this"(or whatever class name you have)
Related
I am using the swipe-to-dismiss library but the app crashes after after the list item is swiped away. I am using the following code :
todolist = (ListView)findViewById(R.id.todo_items);
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
todolist,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
#Override
public boolean canDismiss(int position) {
return true;
}
#Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mAdapter.remove(mAdapter.getItem(position));
}
mAdapter.notifyDataSetChanged();
}
});
todolist.setOnTouchListener(touchListener);
todolist.setOnScrollListener(touchListener.makeScrollListener());
The app gives a UnsupportedoperationException on this line :
com.example.todolist.ToDoListActivity$1.onDismiss(ToDoListActivity.java:81)
How do I fix this ? I am referring to this code sample :
https://github.com/romannurik/Android-SwipeToDismiss/blob/master/src/com/example/android/swipedismiss/MainActivity.java
Thanks !
You created your ArrayAdapter by either calling one of the constructors that takes an object array instead of a List, or you passed it an immutable List.
Try instead calling one of the ArrayAdapter constructors that takes a List and be sure to pass it one that is mutable. This will allow you perform the remove operation.
Internally, the ArrayAdapter constructors that take object array will create a List out of your object array by calling Arrays.asList(T... array) on it, but the List Arrays.asList returns is immutable.
As an example, consider the following data:
String[] FRUIT = {"Apple", "Banana", "Pear"};
Both of the following will create adapters that will throw the UnsupportedOperationException:
new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, FRUIT);
new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, Arrays.asList(FRUIT));
Whereas this one will allow you to modify the adapter's list:
new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(FRUIT)));
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);
}
I have ArrayAdapter with this items structure:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >
<TextView
android:id="#+id/itemTextView"
... />
</RelativeLayout>
And add this adapter so:
mAdapter = new ArrayAdapter<String>(this, R.layout.item,
R.id.itemTextView, itemsText);
All is fine but I want to update text in adapter's items. I found a solution
mAdapter.notifyDataSetChanged();
but do not understand how to use it. Help please.
upd
My code:
String[] itemsText = {"123", "345", "567"};
ArrayAdapter<String> mAdapter;
onCreate
mAdapter = new ArrayAdapter<String>(this, R.layout.roomitem,
R.id.itemTextView, itemsText);
setListAdapter(mAdapter);
itemsText = {"789", "910", "1011"};
onClick
mAdapter.notifyDataSetChanged();
//it's dont work
I think something like this
public void updatedData(List itemsArrayList) {
mAdapter.clear();
if (itemsArrayList != null){
for (Object object : itemsArrayList) {
mAdapter.insert(object, mAdapter.getCount());
}
}
mAdapter.notifyDataSetChanged();
}
Your problem is a typical Java error with pointers.
In a first step you are creating an array and passing this array to the adapter.
In the second step you are creating a new array (so new pointer is created) with new information but the adapter is still pointing to the original array.
// init itemsText var and pass to the adapter
String[] itemsText = {"123", "345", "567"};
mAdapter = new ArrayAdapter<String>(..., itemsText);
//ERROR HERE: itemsText variable will point to a new array instance
itemsText = {"789", "910", "1011"};
So, you can do two things, one, update the array contents instead of creating a new one:
//This will work for your example
items[0]="123";
items[1]="345";
items[2]="567";
... or what I would do, use a List, something like:
List<String> items= new ArrayList<String>(3);
boundedDevices.add("123");
boundedDevices.add("456");
boundedDevices.add("789");
And in the update:
boundedDevices.set("789");
boundedDevices.set("910");
boundedDevices.set("1011");
To add more information, in a real application normally you update the contents of the list adapter with information from a service or content provider, so normally to update the items you would do something like:
//clear the actual results
items.clear()
//add the results coming from a service
items.addAll(serviceResults);
With this you will clear the old results and load the new ones (think that the new results should have a different number of items).
And off course after update the data the call to notifyDataSetChanged();
If you have any doubt don't hesitate to comment.
Assuming itemTexts as String array or String ArrayList,where you are adding new items into itemsTextat that time after that you can call
mAdapter.notifyDataSetChanged();
If you did not get answer then please put some code.
I did something like this. And it works correctly.
Add method to the Adapter class:
public void updateList(ArrayList<ITEM> itemList){
this.itemList.clear();
this.adapterList = new ArrayList<ITEM>();
this.adapterList .addAll(itemList);
notifyDataSetChanged();
}
Call the method in the class you use the adapter:
itemList.add(item);
adapter.updateList(itemList);
can any one help me to display set of results in my app in a list view. i got the following
codeList<Result> results = response.results;
for (Result result : results) {
//Toast.makeText(this, result.fromUser, Toast.LENGTH_SHORT).show();
Toast.makeText(this, result.text, Toast.LENGTH_LONG).show();
//String[] ch=getResources().getString(result.text);//result.text;
//adapter = new ArrayAdapter<String>(this,R.layout.mainlist,result.text);
//SimpleArrayAdapter adapter = new SimpleArrayAdapter(this, result.text);
setListAdapter(adapter);
}
i want to show result.text in a listview for each value. i tried everything commented in the code, but not getting. please help me.
with this short snippet of code it is hard to know what the problem might be,
possibly this will help
http://developer.android.com/resources/tutorials/views/hello-listview.html
after setListAdapter(adapter); try adding adapter.notifyDataSetChanged() or alternatively try adapter.setNotifyOnChange(true) just after initializing the adapter this might solve your issue if it is what i think it is. else post more of your code
You can use **ExpandableListView**
Refer : http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList3.html
1) extend ArrayAdapter with your custom class:
class MyAdapter extends ArrayAdapter<Result>
2) create xml for layout of each row, eg. row.xml and set it in constructor of your adapter:
MyAdapter(Activity context, int resourceId, ArrayList<Result>
mResults) {
super(context, R.layout.row, mResults);
}
3) in getView method of your adapter fill apropriate field with your result.text
4) set list adapter to your custom adapter
MyAdapter adapter = new MyAdapter(this, 0, mResults);
setListAdapter(adapter);
I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.