updating listviews android - android

I am very new to android! I've created a vertical linearlayout with two listviews inside my listfragment and I just want every of this listviews show simple array of strings which I can update during runtime. I it seems very difficult for me to realize how to do this. Here are my files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
<ListView
android:id="#+id/trainslist"
android:layout_width="240dp"
android:layout_height="wrap_content" >
</ListView>
<ListView
android:id="#+id/timelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
This is a fragment XML file, and here is a fragment file:
public class ToFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] countries = {
"Japan",
"USA"
};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, countries);
setListAdapter(adapter1);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Look, here I try to make one of listviews show what I need and it works. But I don't understand how to thange it's content during runtime (not from onCreateView?) from Activity. It's also unclear for me what is android.R.layout.simple_list_item_1? Why, when I write it, android sends my array to the first listview, not second?
The second issue is I can't change second listview content even in this way, even not during runtime! Please help me deal with this! I really find it so difficult...

Related

Custom ListView in ListFragment or Fragment?

I am working on a simple app, so that the main activity has two fragments that user can swipe to go to. On one of them, I want to have custom list. For example, something like this https://github.com/JakeWharton/SwipeToDismissNOA, where each item in the list can be deleted by swiping. I was able to get a regular list to work inside the ListFragment, but can't get this custom list to work. As I understand, ListFragment needs to have a simple .xml with one , but the one that I want to use, has a bit more stuff in it. Something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:listSeparatorTextViewStyle"
android:text="ListView" />
<ListView
android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
So when I am trying to getListView() using this .xml, it either returns null or when I am trying to do something like this:
View view = inflater.inflate(R.layout.fragment_section_dummy, null);
ListView ls = (ListView) view.findViewById(android.R.id.list);
It throws this error:
10-15 20:25:28.895: E/AndroidRuntime(8081):
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView.
Thanks.
For me, is easiest work with my own IDs, so I would change your ListView ID to another one. For my example, I choose android:id="#+id/myList".
In your fragment, in your onCreateView method, try inflating your layout and getting your view by ID.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourDummyLayout, container, false);
//You should get your ListView there
ListView yourListView = rootView.findViewById(R.id.myList);
[......]
return rootView;
}
Try with this ;) Good luck !

Android: ListFragment: How to refresh list

I have a list view which is shown on a fragment. I have a button at the bottom of the screen in which when pressed, will call a webservice to reteive any additional data. If there is additional data, I need to add it to the list view. I have searched this forum and so many other web sites to try and find how to do it, but I have had no success. Any help is much appreciated.
I am now thinking do I need to add the fragment dyncamically instead of having it defined on the following XML layout.
I am using a ListFragment to inflate a list view on the screen.
I have a screen with two fragments on it. The XML for the screen is below: -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<fragment
android:name="atos.mobilereporting.com.ReportList"
android:layout_width="323dp"
android:layout_height="match_parent" />
<fragment
android:name="atos.mobilereporting.com.ReportDetail"
android:layout_width="958dp"
android:layout_height="match_parent" />
</LinearLayout>
<Button
android:id="#+id/getReports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh Mobile Reports" />
</LinearLayout>
The code to inflate the view is below: -
public class ReportList extends ListFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get list of reports...
repList = ReportDefinitionFactory.buildReportList(3);
activity = getActivity();
ListAdapter la = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
ReportDefinitionFactory.getReportDescriptions(repList));
setListAdapter(la);
}
}
This code shows a simple list view with 3 rows on it.
It is at this point I must stop as I do not know were to go from here. I have the code which will add an additional row to the array that is used to initially build the list view, but I do not know how I can invoke a refresh on the list view.
Thanks
Martin
You need to call la.notifyDataSetChanged() to force refresh of the list once the data has changed.
Use the ArrayAdapter notifyDataSetChanged() function.
This could be added in the ArrayAdapter, if that is the location where the data is updated. Otherwise, add it to the same area that calls la.add().

Actionbarsherlock IcsSpinner - width always set to widest items

I'm using Actionbarsherlock and want to use a custom actionbar layout, but still want a navigation list. As a result I am including an IcsSpinner widget. However, the width is always as big as the largest item, and this is not desired. I broke it out of the actionbar to debug and still got the same results:
Fragment Code
public class TestFragment extends SherlockFragment {
private final String[] testStrings = {"Short","Not so long text"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.test_layout, container, false);
IcsSpinner spinner = (IcsSpinner)view.findViewById(R.id.test_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.spiner_test_row, R.id.spinner_text, testStrings);
spinner.setAdapter(adapter);
return(view);
}
}
test_layout.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" >
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="#+id/test_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
R.layout.spinner_test_row
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
Changing to standard Spinner (on gingerbread emulator) works fine:
Any Suggestions?
This appears to be the default behavior of the IcsSpinner component within ActionBarSherlock, even when running Honeycomb+ version of Android - e.g. Square Wallet on my ICS device.
Jake has suggested not using the IcsSpinner component, but without it there is no way simple way to use a custom actionbar layout and maintain the Navigation List UI. Unless Jake can suggest and alternative I will continue to use it, making sure do full testing upon future releases of his great library.

need help regarding ActionBar And Fragmentation

I am using actionbarsherlock library to have action-bar in my application,
I am also using view-pager to switch between the page,
Now the problem is i want only one tab with two fragment to display data regarding each other.
What could be the possible way to do this?
I have been going through like the one below but unsuccessful
In main fragment i am inflating a xml like this
<code>
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.album_view_main, container, false);
return fragView;
}
</code>
xml fie is look like this
<code>
<?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="horizontal" >
<fragment
android:id="#+id/fragment_album_view"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="3"
class="com.nix.music.activity.fragments.AlbumFragMent" />
<FrameLayout
android:id="#+id/frame_list_song"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="2" >
</FrameLayout>
</LinearLayout>
</code>
but the problem is i cannot find "frame_list_song" in AlbumFragment class it appears null
what could be the way around ?
thank you
This layout should be set in the parent activity using setContentView(). Then, you would be able to do something like getActivity().findViewById(R.id.frame_list_song) from your AlbumFragment. The better way would be to use an interface and transfer control to the main activity, and not access other fragments directly.

Android: failed to setContentView when switching to ListActivity

[update] I got the error, which says "Your content must have a ListView whose id attribute is 'android.R.id.list'". Appearently nothing in my xml is ListView. But is that required?
This is an follow-up issue on my previous question
android: which view should I use for showing text and image?
I read the article about creating ListView for LinearLayout. However, my following code failed at the setContentView() function when I changed "extends Activity" to "extends ListActivity", any idea why?
private TextView mSelection;
//private ImageView mImages;
static final String[] keywords = new String[]{"China", "Japan", "USA", "Canada"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactLayout);
mSelection = (TextView)findViewById(R.id.ContactNames);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.contactlayout, R.id.ContactNames,keywords);
setListAdapter(adapter);
}
My Layout is from this article: http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/ContactNames"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
I think you misunderstood the other posts I showed you in the previous question. They were explaining how to use a custom layout for each row in your list, not how to define the entire layout file for the activity. You need something like this:
(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:id="#android:id/list">
</ListView>
Note the very important line android:id="#android:id/list". You must have that in your ListView as that's what tells Android where your list is. The cacheColorHint is useful if your background isn't black - see this post for more details about that.
With the above lines you can give your activity a list that will be recognised properly. Here's a basic example:
public class TestProject extends ListActivity {
final static String[] ITEMS = {"blah", "floop", "gnarlp", "stuff"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listrow, R.id.textview, ITEMS);
setListAdapter(adapter);
}
}
Then the listrow layout is just this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview"/>
</LinearLayout>
This is a really simple layout. If you want to get something more complicated, changes are you'll have to use a BaseAdapter, as that gives you calls getView(...) before each row. In that you can use different layouts depending on the contents of each row. However, BaseAdapter looks scary when you first try it, so be warned! :)
Yes, if you are using a ListActivity, you need to have a ListView who's id is android.R.list in your layout file.
If you aren't using a ListView in your layout, and I don't see one in there, then switch to using a regular Activity.
Actually, your (custom) layout doesn't need a ListView when using a list activity. The easy way to solve this is just remove the setContentView() line altogether. In simple terms, when you do it, Android "assumes" the layout you're using to contain a single full-screen ListView, and provides it for you.
If you want a different (richer) interface for the Activity though, you must code the XML and use the informed ID for Android to know how to show the list implied by the activity being a ListActivity after all. Note that the layout for an item isn't the same as the list, and although I haven't tried that, I assume you can have a custom item layout without having an explicit ListView in the activity layout.

Categories

Resources