Listview adding to the same line - android

I have an android app that lists connected clients in a listview, but whenever someone connects, it just adds them to the same line, this is the code I use to add connected client. I am new to listview, and not sure how to do this properly, I looked at the android docs but hard to say what needs to be used. If anyone can help me out that would be great.
remoteip += socket.getInetAddress();
ArrayList<String> addclientlist = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
addclientlist.add(remoteip);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter)

I think you need to use this constructor
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this, android.R.layout.simple_list_item_1, addclientlist);
Also no need to set adapter always just set it once and each time you have to add new item to adapter you can use either of these as in java Array objects are pass by reference, and then call notifydatasetchanged
adapter.add(remoteip);
//or addclientlist.add(remoteip);
adapter.notifyDataSetChanged(); // Dont forget this

You are updating the adapter initialization list, which is futile.
Instead - update the actual adapter:
adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
listview.setAdapter(adapter)
...........
adapter.add(remoteip); // <----- instead of addclientlist.add()
adapter.notifyDataSetChanged();

Related

How to update adapter?

I have several spinners in a class.
For the first spinner I have set of data.
Other spiners will download data from server according to the selection of the fist spinner.
However, after downloading data, it does not update the spinner adapters.
Adapter for the second spinner:
sectionField = new String[] {"Error"};
adapterSection = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, sectionField);
adapterSection
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
section.setAdapter(adapterSection);
Code sample from fisrts spinner onItemSelected, which I believe should update the adapter:
sectionField = new String[data.length()];
sectionField = data;
section.setVisibility(View.VISIBLE);
adapterSection.notifyDataSetChanged();
You need to clean your adapter, then add items and notify.
adapterSection.clear();
adapterSection.addAll(data);
adapterSection.notifyDataSetChanged();
Hope it's help.

Update ListView even if its adapter is null

I want to update my list even if the adapter is null, but I dont know how to do it.
When I execute the following code my app crashes:
ProductAdapter adapter = new ProductAdapter(context, R.layout.listrow, yal);
if(adapter.getCount()>0){
lv.setAdapter(adapter);
}else{
lv.setAdapter(null);
}
adapter.notifyDataSetChanged();
lv.invalidateViews();
The adapter is sort of the data source for the list. It provides the individual list items. You can't have a list without an adapter, since then you won't have rows in the list.
Check out the ListView documentation for more information.
Don't nullify the adapter - the listView always needs one. Instead, nullify the dataset the adapter works with and make sure getCount() returns 0 if the dataset is null (or empty). Or don't nullify the dataset, but make it an empty list or array.
If your data changes, update the adapters dataset and call notifyDatasetChanged() on the adapter. Do not create a new adapter for the ListView when you get new data. This is important for several reasons; e.g. the listViews position won't jump to the top but stay where it is.
Example:
ProductAdapter adapter = new ProductAdapter(context, R.layout.listrow, null);
listView.setAdapter(adapter);
later
adapter.setData(newData);
adapter.notifyDatasetChanged();

Adapt ArrayList into ListView

I have a XML File and I had parsed the data into the textView successfully now I want to bind that data into the ArrayList or List and display it in ListView.
But I don't know how to bind arraylist data into the ListView.
I have added all data into the arraylist successfully as mentioned in the below code.
List al = new ArrayList();
al.add(parser.getAttributeValue(null, "firstnames"));
Kindly please help me with the code syntax for the above issue.
Regards .
Thanks in advace
please have a look at the sample at http://codinglookseasy.blogspot.in/2012/07/android-list-view-sample.html
Instead of this
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, months);
setListAdapter(aa);
use this in your case
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, al);
setListAdapter(aa);
You need to use an Adapter to bind a List to a ListView, like this:
List<String> list = new ArrayList<String>();
// add data to list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
Notice that the subtype of List (which is String) matches the subtype of the ArrayAdapter (also String). The layout android.R.layout.simple_list_item_1 defines how the String is displayed in every row. You can look up the specifics of this layout in your SDK, if you want you can also use you own layout. Hope that helps, good luck learning Android!

listview not updating with notifydatasetchanged() call

This is my code
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
adapter = new CustomAdap(this, temp);
listview.setAdapter(adapter);
The above code works fine.But when i change my code to this
listview =(ListView) findViewById(R.id.lv1);
adapter = new CustomAdap(this, temp);
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
Listview does not show anything.what is the problem?
It looks like you're changing the collection that you initialized adapter with. I would change your code in this way:
// initial setup
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
adapter = new CustomAdap(this, Monday);
listview.setAdapter(adapter);
// change your model Monday here, since it is what the adapter is observing
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Note that if your CustomAdap was a subclass of ArrayAdapter, you could also have done
// change your array adapter here
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
adapter.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
EDIT: I understand more what you want to do now thanks to your comment. You'll probably want to have the adapter replace its contents with that your different ArrayLists then. I would make your CustomAdap be a subclass of ArrayAdapter.
Then you can utilize it this way:
// replace the array adapters contents with the ArrayList corresponding to the day
adapter.clear();
adapter.addAll(MONDAY);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Why it works in first code ?
--- Because you are setting the values to temp List and passing it the adapter and it shows it into listview.
Why not work in second code ?
--- Because you are setting temp to adapter far before you set value into temp
second,your adapter class might not getting the updated value when you set new value to temp ..that because temp is not public or not at class level or not static..
Put the temp declaration at root level and try.
And please show your full code as much as required and Logcat if you getting any warnings than also.
Check for a link to your referenced view in the proper xml file. Or at least check for the existence of said xml file.
What adapter are you using? It is clearly a case where your adapter is not getting updated after u set the data in your temp variable.

How do you add array to listview

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.

Categories

Resources