when extending listactivity it is must to have a #android:id/list
question 1> what if i want to implement 2 listviews then ids will be same #android:id/list.
question 2> in what scenario i should use #+id/listView1.
Thanks in advance.
Well, the ID is independant(unless you don't name them uniquely) so you'd use the "#+id/listView1" solution every time as far as I know. I did this on my own Android app and it worked just fine.
ListActivity is basically a 'convenience' class which simply extends Activity and has a single ListView plus some convenience methods for handling it - there's nothing particularly special about it.
If you want an Activity which has more than one ListView then it would be better to simply create your own from scratch.
If you create a Xml with let's say a relativelayout that contains a imageview on the top and a listview in the middle and a button in the bottom. Then you need to set an uniqe ID for your listview to gain access to it.
ex:
<ListView
android:"#+id/unique_listview"
android:layout_width="fill_parent"
android:layout_height="400px"
/>
Then you can customize and populate the listView with this:
ListView myList = (ListView) findViewById(R.id.unique_listview);
myList.setAdapter(new myListAdapter(this));
private class myListAdapter extends BaseAdapter {
....
}
Related
I am having a somewhat weird scenario building the UI for an Android application. Although I got to solve the error, I don't know the cause and I would like to understand why this is happening in order learn more about Android. I reproduced the scenario in a simple application:
Creating a new Hello World Android application adding this resources file under res/layout/footer_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Foo Bar" />
And using this Activity:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(android.R.layout.list_content);
ListAdapter adapter = new ArrayAdapter(this, android.R.layout.activity_list_item);
setListAdapter(adapter);
View footer = LayoutInflater.from(this).inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footer);
}
}
We get the following screen:
To my surprise I saw that the footer view, which is a TextView saying "Foo Bar", was missing. After some playing around I found out the solution was to move the "setListAdapter(adapter);" line to the end of the onCreate() method, or said differently, after adding the footer to the view.
Exploring a little further I found out the problem is the width, which is 0 if I have the adapter added before. Another solution was to indicate an explicit width other than match_parent.
Any clue why this happens? For me it'd be more intuitive that it didn't work if the adapter hadn't been added because the list may not be ready, but why would it not work if everything is prepared?
The reason is that internally, if there are headers or footers, the adapter is wrapped inside another adapter.
So if you call setAdapter before adding the header and footer views, then the wrapping adapter is not aware of them.
If you leave setAdapter to the end, then the adapter that wraps your adapter knows about the headers/footers since those have already been added to the list.
See the documentation:
Add a fixed view to appear at the bottom of the list. If addFooterView
is called more than once, the views will appear in the order they were
added. Views added using this call can take focus if they want.
Note: When first introduced, this method could only be called before
setting the adapter with setAdapter(ListAdapter). Starting with
KITKAT, this method may be called at any time. If the ListView's
adapter does not extend HeaderViewListAdapter, it will be wrapped with
a supporting instance of WrapperListAdapter.
I am trying to dynamically add information to a ListView. The information I am adding consists of a "Device Name" (the main item) and "MAC Address" (the sub item). An example from online is below. Note: I want to replace Item 1 with a device 1's name, sub item 1 with device 1's MAC address, and so on. This MUST be done dynamically because the list is being populated as devices are scanned for.
.
Before this is marked as a repeat, I have looked at the following questions and they have not helped me: Adding ListView Sub Item Text in Android, How to add subitems in a ListView, Adding Items and Subitems to a ListView
The conclusion I have come to through reading these questions is that I need to implement a custom ArrayAdapter and override the getView() method. I have created a custom layout with two text views in it:
cyan_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/main_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/cyan"/>
<TextView
android:id="#+id/sub_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/dark_cyan"/>
</LinearLayout>
I then try to create a custom ArrayAdapter in my Activity class, but I am lost as to what to put in my public View getView(final int position, View convertView, ViewGroup parent) method. Additionally, is creating a custom ArrayAdapter necessary if all I am trying to do is add a textview sub item?
The answer to your question is: NO, you don't need to create a custom ArrayAdapter if you just want to add items. I recommend, however, creating it if your layout is customized, as you'll gain so much control over the items you're displaying. You didn't add your code where you create your ArrayAdapter, but in your case I'd use this constructor. The important part is the third parameter: In your activity, you should store an ArrayList with the initial items you're adding to your ArrayAdapter, then, if you want to add a new item, you simply add it to the ArrayAdapter and call notifyDataSetChanged() on your adapter. Simply doing that, your item will be added to the layout and displayed. If you need to override the GetView method for your own ArrayAdapter, I recommend this link, it helped me understanding the whole thing.
are you searching some listview example in google like those tutorials :
http://www.vogella.com/tutorials/AndroidListView/article.html
http://www.mkyong.com/android/android-listview-example/
I think they explain step by step how to create a list adapter
You need to add getter method into your Adapter
YourAdapter ...{
List<Device> items = new ArrayList<Device>;
public List<Device> getItems(){
return items;
}
}
then change item that you need
...{
//for 1s item
Device item = getItems().get(0);
item.setTitle(macAdress)
}
and call notifyDataSetChanged for your adapter
...
yourListView.getAdapter().notifyDataSetChanged();
}
Thats it. Now you are able to change your list data.
And for your question, i think yes. Is better to create your own adapter in order to have simple possibility to exentd it later. And in your case (if you dont want to change your adapter after each title change) you deffinetly need custom one. Cheers
My main activity has a layout containing a ListView and several other items. I'd like to assign a ListAdapter to it. I followed the tutorials shown here: http://www.vogella.com/articles/AndroidListView/article.html
How can I let the adapter know that my activity's layout has a ListView in it, and assign it to it? When I try to run the app as-is it crashes with a runtime exception stating that my layout needs to have the default Android listview in it.
Thanks!
If your activity is inherited from ListActivity, you don't have to manually let the adapter know the ListView.
But in order to do this, ListView in your activity should look like this
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
because documentation says
your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)
When I try to run the app as-is it crashes with a runtime exception stating that my layout needs to have the default Android listview in it.
It sounds like you're using a ListActivity and calling setContentView(). In the layout that you pass to setContentView() you must have a ListView with the following attribute:
<ListView
android:id="#android:id/list"
... />
Then bind your Adapter to the ListView with setListAdapter().
I'm having some trouble figuring something out that should be quite easy and straight forward. I have an activity that has a few Views in it including a ListView that currently is not populated.
How do you programmatically get a reference to that ListView and then set the items, each item being a TextView with the content then being XML String-Array items?
Getting reference I suppose is like this:
ListView incomeList = (ListView) findViewById(R.id.incomeList);
Also I have a this code in my strings.xml(Want to populate list with this array):
<string-array name="testArray">
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
<item>Item4</item>
<item>Item5</item>
</string-array>
And then lastly I have this other layout xml file that I should have according to all the tuts out there. list_item.xml
<?xml version="1.0" ebcoding="utf-8">
<TextView xmlns:android="blablabla..."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
</TextView>
All of the tutorials I followed on the web though is helpful if a ListView is the only View in your activity. Whereas in my case I have quite a few Views including buttons, textviews etc etc.(If this might be helpful to know). Also do I extend my class with ListActivity or since I have other views in there, just extend normally with Activity?
You don't need to extend ListActivity. Just extend a regular activity.
Steps:
A) get your ListView. You're doing it right in your question.
B) create an ArrayAdapter. You must pass a Context (if you do it on the Activity, pass that Activity), an int representing a Layout which ONLY contains a TextView (Like the one in your question), and a String[] containing all the items you need.
Should look like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, getResources().getStringArray(R.array.testArray);
C) lv.setAdapter(adapter);
Pretty straightforward.
This is my list i want My problem is when i scroll list view then the check boxes(which are the items of this List ) are automatically checked
ex - if i checked first then 4 automatically being checked.
My first goal:
1. want to stretch my list to full i will wrap it into Scrollview how
2. i can prevent it to automatically checked
.
<ListView
android:id="#+id/ListViewProducts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_span="2"
android:clickable="true"
android:isScrollContainer="true"
android:saveEnabled="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarSize="10sp"
android:scrollbars="vertical" >
</ListView>
Create your own BaseAdapter.
Keep in mind, that ALL views in your listview you see are temporary. They will be recycled when you'll scroll away. The reason is - you can have >9000 elements in your list. So, the way you create views must depend on some kind of (!) data.
Here is nince tutorial on how to create your own list.
Make X-th checkbox depend on X-th boolean in the list. A bit confusing first time I know, but this is the best way.
class MyAdapter extends BaseAdapter{
List<boolean> myCheckBoxes;
boolean getItem(int arg0){
return myCheckBoxes.get(arg0);
}
View getView(int arg0, View arg1, ViewGroup arg2){
...
...//See article
myView.setChecked(getItem(arg0));
...
return myView;
}
And in your activity
ListView myListView;
...
myListView.setAdapter(new MyAdapger(...));
You can't put a listview into a scrollview, two views scrolling in the same direction will not work nicely. Just put the listview in your non scrolling layout (frame- , list-, relativelayout).
Use an Adapter that sets every listview's row's views according to the data to be displayed.
ListView already extends ScrollView and doesn't need to have another one to surround it.
try looking at this post on creating custom listView items. you can implement a checkBox in them and make is have android:checked="false"
ListVew already extends ScrollView no need to implement it on ListView
for AutoCheck follow this link:
Check box checked Automatically in listview when scrolling the list.
There's no need to implement scrollview in listview becoz it is already extends to scrollview.
I think your listview is not able to handle the recycling of items properly.So to solve this problem go through the below link.
Getting an issue while checking the dynamically generated checkbox through list view