Show static data on listview - android

I want to show some data on listview in android,I am using setlistadapter for this,but when I get the list "getlist" in oncreate it gives me null pointer exception,
pls help me in this regard
Thanks
Here Is my source code:
public class WOWActivity extends ListActivity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wow);
String[] newsFeed = getResources().getStringArray(R.array.Feeds);
listView = getListView();
this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wow, menu);
return true;
}
}
Activity_wow:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".WOWActivity" >
<ListView
android:id = "#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false" >
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
List_wow:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/url"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
String Value:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Feeds">
<item >Apples versus Oranges </item>
</string-array>
</resources>

Change by this.
public class WOWActivity extends ListActivity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
String[] newsFeed = getResources().getStringArray(R.array.Feeds);
this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));
}
EDIT:
Remove this from TextView
android:visibility="gone"

just change the following in WOWActivity.java
change listView = getListView(); to listView = (ListView)findViewById(R.id.list);

You needn't extends your class from ListActivity, just get listView as:
listView = (ListView)findViewById(R.id.list);
Next set your adapter to ListView
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));

Related

Android: Use different layouts in ListFragment using ArrayAdapter

Hello everyone and thank you for reading my question!
I'm currently developing an android app that has the following hierarchy:
One FragmentActivity is divided by three ListFragments.
One of these ListFragments uses a custom ArrayAdapter to display a List<> with a xml template on a ListView
This List<> is filled with dataset which are given id's. Datasets with id's lower than 10 are flagged "important", the other ones are flagged "not so important"
This is what I am looking for:
I have two layout templates, and I want to display the "important" datasets with one layout and the "not so important" with the other layout.
I have searched for quite a while now, but it looks like noone has asked a question like this before.
I tried to use a second ListView and apply a second custom ArrayAdapter on it, but unfortunately, there is a restriction: Since I'm using ListFragment, I'm forced to use findViewById like this.
ListView list;
list = (ListView) rootView.findViewById(android.R.id.list);
When I tried to write the second custom ArrayAdapter, it showed an error here:
ListView list;
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
Here is the entire ListFragment
public class LeaguePage extends ListFragment{
Global global_var;
ListView list, list_flat;
List <League> leagues = null;
ListAdapter adapter = null;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.league_page, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
try {
leagues = Leagues_Parser.parse(getActivity().getAssets().open("league_raw.xml"));
} catch (IOException e) {
e.printStackTrace();
}
adapter = new LeagueAdapter (getActivity(), R.layout.list_row, leagues);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Global.selectedTeam = Integer.valueOf(leagues.get(arg2).getInternalID());
Global.mViewPager.setCurrentItem(1, true);
}
});
}
}
This is the xml file I pass on to my ArrayAdapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/liga_flat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingBottom ="10dip"
android:textColor="#040404"
android:textSize="25sp"
android:textStyle="normal"
android:typeface="serif" />
</RelativeLayout>
This is the xml layout file for the ListFragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:divider="#null">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<ListView
android:id="#+id/android:list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp" >
</ListView>
</RelativeLayout>
</RelativeLayout>
You shouldn't give a ListView an ID like this :android:id="#+id/android:list2", the ID of your ListView should be
<ListView android:id="#id/android:list"
...... ></ListView>
or
<ListView android:id="#+id/sampleList"
...... ></ListView>

ListView android: I can't see my list items

I'm trying to show a ListView in my android application. (I know that I'm not extending of ListActivity, it can be the error?) My code:
public class Home extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
startLocation();
createMenuTabs();
final String[] datos = new String[]{"Lista 1","Lista 2","Lista 3","Lista 4","Lista 5"};
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datos);
ListView urban_lines_list = (ListView)findViewById(R.id.urban_lines_list);
urban_lines_list.setAdapter(adaptador);
}
}
The XML is:
<LinearLayout android:id="#+id/Urbano"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/urban_lines"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/list_of_lines"/>
<ListView android:id="#+id/urban_lines_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Thanks for the help!
Your TextView is filling the whole screen, simply change its height to wrap_content.
<TextView android:id="#+id/urban_lines"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/list_of_lines"/>

How to make custom ListView

I've got a TextView and it's getting a list in it.
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
//Own row layout
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));
//listView.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
Now I want a small grey description of the list item, like here:
I read these tutorials: 1 and 2 but I have to admit, I don't understand what they say.
Can someone explain how to use them? Do I have to remove my TextView/List I have now? Is there another way to make a little description, without using a custom ListView?
Here's my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="2dp"/>
I don't really use the listview "list", just for testing purposes.
You need to understand that the following line
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));
means that you want to use layout.activity_home.xml for each item's view of your list.
So I don't see why there is a ListView inside it...
By default the ArrayAdapter uses one single text field per item. If you want a more complex item layout, you need to override the getView() method of your adapter.
Here is the simplest solution I can think of:
In Home.java:
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, Liste) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String item = getItem(position);
TextView subTitleView = (TextView) view.findViewById(R.id.subtitle);
subTitleView.setText("Subtitle of " + item);
return view;
}
});
}
}
And layout/list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:padding="10dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
First use the setContentView and call the layout.. From the layout call the LISTVIEW. then using the adapter put the LISTE inside the list view use your data.

Item and SubItem in ListView

I want to show Item and corresponding subitem in list view using Arrayadapter and android.R.layout.simple_list_item_2. I am able to list items with following code
XML file:
<?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"
android:paddingLeft="8dp" >
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:id="#android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty set" />
java file:
public class Lists extends ListActivity {
String[] listItems = {"Item1", "Item2 ", "Item3", "Item4"};
String[] SubItems = {"SubItem1", "SubItem2", "SubItem3", "SubItem4"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListAdapter adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adp);
}
}

android-actionbar in a ListActivity

I'm trying to make the android-actionbar plugin working in my ListActivity,
Here's my Class :
public class DisciplinesController extends ListActivity {
private DisciplinesModel dbHelper;
private Cursor cursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_action);
ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
dbHelper = new DisciplinesModel(this);
dbHelper.open();
if (dbHelper.fetchDisciplineCount() == 0) {
dbHelper.fetch();
}
cursor = dbHelper.fetchAllDisciplines();
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this, R.id.list,
cursor, new String[] { "name" }, new int[] { R.id.discipline_name});
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
}
and my XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.markupartist.android.widget.ActionBar
android:id="#+id/actionbar" style="#style/ActionBar" />
<LinearLayout android:id="#+id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/discipline_name"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical" android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
</LinearLayout>
I always have this exception :
Your content must have a ListView whose id attribute is 'android.R.id.list'
Do you have an idea ??
Thanks !
In your XML layout you must include a ListView element with the #android:id/list id.
<ListView
android:id="#android:id/list"
...
/>
Basically, you need to rip out the section LinearLayout into a new file (e.g. list_item.xml). Replace this with a listview in your list_action.xml
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
andrdoi:layout_height="wrap_content" />
Then change R.id.list in your SimpleCursorAdapter constructor to R.layout.list_item.

Categories

Resources