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);
}
}
Related
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));
I keep getting this error:
Unable to start activity ComponentInfo{com.Nostradamus/com.Nostradamus.Contactenlijst}: java.lang.NullPointerException
And I don't know what I do wrong.
It has something to do with the last code:
lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
This is my java code:
public class Contactenlijst extends Activity {
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] {"voornaam", "achternaam", "geboortedatum", "adres", "postcode", "woonplaats", "email", "telefoon"};
int[] to = new int[]{R.id.voornaam, R.id.achternaam, R.id.geboortedatum, R.id.adres, R.id.postcode, R.id.woonplaats, R.id.email, R.id.telefoon};
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Log.d("test","test2");
// Get the data (see above)
JSONObject json = Database
.getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");
Log.d("test","test3");
try {
JSONArray contactinfo = json.getJSONArray("contactlijst");
Log.d("test","test4");
// Loop the Array
for (int i = 0; i < contactinfo.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = contactinfo.getJSONObject(i);
map.put("voornaam", e.getString("staff_name"));
map.put("achternaam", e.getString("staff_lastname"));
map.put("geboortedatum", e.getString("staff_dateofbirth"));
map.put("adres", e.getString("staff_address"));
map.put("postcode", e.getString("staff_address_postal"));
map.put("woonplaats", e.getString("staff_address_city"));
map.put("email", e.getString("staff_email"));
map.put("telefoon", e.getString("staff_phone"));
mylist.add(map);
Log.e("test",map.toString());
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
}
}
And these are my xmls:
Contactenlijst
<?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">
<TextView android:id="#+id/voornaam"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/achternaam"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/geboortedatum"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/adres"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/postcode"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/woonplaats"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/email"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/telefoon"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
Contactview
<?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">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data">
</TextView>
</LinearLayout>
and Listitem
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
you didn't set the layout using
setContentView(R.layout.yourlayout);
so without add layout you are using your listview that's why you getting NullPointerException.
add this above line after
super.onCreate(savedInstanceState);
or before the use listview object
You need to call setContentView(...) before lv = (ListView) findViewById(R.id.list);
setContentView(...) inflates the XML file for your layout. If you don't do this, then lv will be null.
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.
I'm stumped as to how to properly implement an adapter that can pull a column of data from my sqlite database and add it to a listview (based on the android-provided multiple checkbox). All of the existing examples use the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","World","Foo","Bar"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
However, I don't want a string. Here's my code:
public class ListGroups extends Activity {
/** Called when the activity is first created. */
private ExpensesDbAdapter mDBHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
ListView lv = (ListView)findViewById(R.id.list1);
mDBHelper = new ExpensesDbAdapter(ListGroups.this);
mDBHelper.open();
Cursor c = mDBHelper.fetchAllGroups();
startManagingCursor(c);
String[] from = new String[] {ExpensesDbAdapter.KEY_GROUPNAME};
int[] to = new int[] {R.id.groupname};
ListAdapter ladapter = new SimpleCursorAdapter(ListGroups.this, R.layout.grouprow, c, from, to);
lv.setAdapter(ladapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
Here's the view.xml file:
<?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"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
And, because the SimpleCursorAdapter asks for it, here's the grouprow.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>
If I use a ListActivity and don't invoke the layout on Creation, I can get it to work, but I need the wrappers around that ListView, and I can't make the .addHeader and .addFooter methods work for me. Any help on creating the proper adapter would be appeciated.
Answered it with some help. Several things were wrong, mostly in the list.xml:
<?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"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_around"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
This allows you to tie the ListView to that element in the xml. You don't have a name for it, but you can use getListView() in the onCreate method to access it.
I am writing an Android app that uses an AutoCompleteTextView just like the API example. The problem is that my text is too long and needs to wrap when the dropdown occurs. I have a custom list_item.xml, but only one line of text displays. The list_item.xml layout wraps to 2 lines if used with a spinner, but not with the AutoCompleteTextView.
main.java
public class main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
textView.setAdapter(adapter);
}
static final String[] COUNTRIES = new String[] {
"This is the first line that is too long and it needs to wrap content",
"Albania", "Algeria", "American Samoa", "Andorra",
"This is a second line that also needs to wrap its content",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium"
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country" />
<AutoCompleteTextView android:id="#+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
>
</TextView>
Wrap the TextView in a layout and use that for your ArrayAdapter
<?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" >
<TextView
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
/>
</LinearLayout>
And the new calls for the ArrayAdapter:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.item, COUNTRIES);
textView.setAdapter(adapter);
easy way :
adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.two_line_list_item,android.R.id.text1,....);
android.R.layout.two_line_list_item this is easy change.