Android, issues with custom listview - android

I'm using a custom listview I found here:
http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_9.html
Seems to be valid in Eclipse, and looks good in the preview tab. It's just a listview that has a button on the bottom. So i've added it as R.layout.buttonlist
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/testbutton"
android:text="#string/hello" android:layout_alignParentBottom="true" />
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/list"
android:layout_alignParentTop="true" android:layout_above="#id/testbutton" />
</RelativeLayout>
Unfortunately when I run it, i get a pop up window that says Android has closed unexpecitdly:
setListAdapter(new ArrayAdapter<String>(this, R.layout.buttonlist , data));
When I try using a built in list view:
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
everything works fine. I dont see any errors or warnings in logcat, so I'm not sure how to pinpoint the problem. Does anyone have any ideas? Thanks
Edit: adding activity
public class TestActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> data = new ArrayList<String>();
data.add("hello");
data.add("world");
setContentView(R.layout.buttonlist);
//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
}
}

Hoofamon, I would like to correct you here. You are not creating a custom ListView but a custom layout with a ListView. Also, I believe that you have not completely understood what the setListAdapter is doing here.
This line that you have is telling the listview to consume 'android.R.layout.simple_list_item_1' as the content of its layout. This layout comes pre-defined in the Android SDK. It would just contain text in each item of a listview. The third attribute 'data' indicates the content of each listview item.
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
So, as Mike L. has suggested, if your intent is to have a listview with only text (having the default format), then the line above would serve the purpose well. You can set 'R.layout.buttonlist' as the layout of your activity using
setContentView(R.layout.buttonlist);
However, if you are planning to include additional content in the listview (read images) or want to change the styling of the text, you would have to define a custom layout for the listview. We can direct you to appropriate sources if you want to know how that can be done.
EDIT: A possible way of loading data into a normal ListView
TestActivity.java
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonlist);
List<String> data = new ArrayList<String>();
data.add("hello");
data.add("world");
ListView mListView = (ListView)findViewById(R.id.list);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , data));
}
}
buttonlist.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/testbutton"
android:text="#string/hello"
android:layout_alignParentBottom="true" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
android:layout_alignParentTop="true"
android:layout_above="#+id/testbutton" />
</RelativeLayout>
This is how it should look like on the emulator:

I don't think you can have a listview in the layout of an adapter. The passed in layout should just describe a row in the listview. So buttonlist should just contain the xml for the button. The listview needs to be in a separate layout file. If this is a list activity then you don't need another layout file, just call setListAdapter like you are doing.

If you want to use your R.layout.buttonlist to fill up your listview,you can do it as follows(your TestActivity should extend Activity,not ListActivity):
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonlist);
String data[]=new String[]{"Item_1","Item_2","Item_3"}
Button b=(Button)findViewById(R.id.testbutton);
ListView lv=(ListView)findViewById(R.id.list);
ArrayAdapter aa=new ArrayAdapter(context,android.R.layout.simple_list_item_1, data);
lv.setAdapter(aa);
//Your code...
}
Now if you want to create custom listitem to be displayed in the listview,then you need to do like this:
Create your custom listitem xml file.
Ex: custom_listitem.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/icon"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/text"
/>
Create custom ArrayAdapter:
Ex. CustomArrayAdapter.class
public class CustomArrayAdapter extends ArrayAdapter<String> {
String[] array;
LayoutInflater mInflater;
public CustomArrayAdapter(Context context, int textViewResourceId,
String[] objects)
{
super(context, textViewResourceId, objects);
array=objects;
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.custom_listitem, null);
holder = new ViewHolder();
holder.text=(TextView)convertView.findViewById(R.id.text);
holder.img=(ImageView)convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.text.setText(array[position]);
if(position==0)
holder.img.setImageResource(R.drawable.img1);
else if(position==1)
holder.img.setImageResource(R.drawable.img2);
else if(position==2)
holder.img.setImageResource(R.drawable.img3);
return convertView;
}
static class ViewHolder
{
TextView text;
ImageView img;
}
}
Use this custom adapter class in your main activity to fill up listview:
Be sure,this main activity extends Activity and not ListActivity
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=getApplicationContext();
lv=(ListView)findViewById(R.id.listview);
CustomArrayAdapter aa=new CustomArrayAdapter(context,R.layout.custom_listitem, new String[]{"item_1","item_2","item_3"});
lv.setAdapter(aa);
// other lines of code
.
.
.

Related

Not able to use custom layout in ArrayAdapter

I'm new to android, I want to use my custom layout file instead of simple_list_item_1 but its not allowing to do so. I want to add an backgroud image(which is defined in layout->custom_view.xml) to each items in the list OR help me to set the backgroud for the whole page, In the below code where should I use the setContentView Method. Also let me know if any changes are to be made in the below .xml file.
public class Planet extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
//setContentView(R.layout.custom_view); Tried to set the view from here but the application is crashing...
String[] planet_names = new String[] {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Sun"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planet_names);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
Intent myIntent = new Intent(Planet.this, Galaxy.class);
startActivityForResult(myIntent, 0);
}
}
});
}
custom_view.xml file is as follows,
<?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:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Simply use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.yourcustomview_withonetextview,R.id.yourcustomlayouttextviewid, planet_names);
You don't set the contentview in a ListActivity. The ListActivity already does that for you. Use an Activity, if you want to customize your layout.
You need to call super.onCreate() before setContentView(). And if you are going to use a custom layout in a ListActivity, you need to give the ListView the id #android:id/list.

switching from a CustomAdapter ListView to an standard ArrayAdapter ListView fails

I recently got interested in Android coding and working my way through several tutorials. however, right now i m stuck for a day and hope you guys can help me out.
basically i want to create a main menu and a submenu for every main menue entry. (right now this is only the case for the first entry)
For the main menu I used a ListView with different icons (realized with a custom adapter). Selecting one of the menu entries works fine in general, but if I choose to configure the onitemclick to start a new activity which is another (simple) ListView which uses the ArrayAdapter it breaks after switching into the submenu activity.
Strangely if i use the ArrayAdapter in both ListViews it is no problem; also when using CustomAdapter in both.
Here is the code, thanks in advance for any help ;)
main menu:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array contentmenu
String[] contentmenu = getResources().getStringArray(R.array.mainmenu_cats);
// storing icons in Array
TypedArray iconarray = getResources().obtainTypedArray(R.array.mainmenu_icons);
// Binding resources Array to ListAdapter
this.setListAdapter(new ImageAndTextAdapter(this, R.layout.list_item, contentmenu, iconarray));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
switch( position )
{
case 0: Intent newActivity0 = new Intent(getApplicationContext(),SubActivity.class);
startActivity(newActivity0);
break;
case 1: Intent newActivity1 = new Intent(getApplicationContext(),SubActivity2.class);
startActivity(newActivity1);
break;
}
}
});
}
submenu:
public class SubActivity extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] submen_1 = getResources().getStringArray(R.array.submenu_1);
// Binding resources Array to ListAdapter
setListAdapter(new ArrayAdapter<String>(ctx, R.layout.list_item1, R.id.submen1, submen_1));
ListView sublv1 = getListView();
// listening to single list item on click
sublv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent newActivity = new Intent(getApplicationContext(),Page1.class);
// sending data to new activity
newActivity.putExtra("position",position);
startActivity(newActivity);
}
});
}
xml main menu:
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/option_icon"
android:layout_width="48dp"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
sub menu:
list_item1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/icon1"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:src="#drawable/ic_action_google_play" />
<TextView
android:id="#+id/submen1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
Currently you are not defining Hight and Width for LinearLayout in both list_item.xml and list_item1.xml layouts . add height-width as :
In list_item.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- PUT YOUR ImageView or other xml elements here -->
</LinearLayout>
and do same for list_item1.xml layout

ArrayAdapter requires ID to be a TextView error

I am trying to create a nice layout for my list items, but my code only works when it is simplified like this:
<?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>
When I add a little bit more it compiles and runs but it force closes on start and gives me the error ArrayAdapter requires ID to be a TextView:
<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/icon1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon1"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Some more information" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon1"
android:gravity="center_vertical"
android:text="Some Information" />
<ImageView
android:id="#+id/icon2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
and
public class FirstLoginActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] testcontacts = getResources().getStringArray(
R.array.testcontacts_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
testcontacts));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
I am pretty sure I'm doing this right, I've been through numerous tutorials and I've found that the fastest and most efficient way is to create a static ViewHolder class. One of the tutorials tried accessing the data directly which is what I was trying to do. I'm still a little confused on how to do so.
public class FirstLoginActivity extends ListActivity {
Context mContext;
List mList;
String[] testcontacts = getResources().getStringArray(
R.array.testcontacts_array);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
testcontacts));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
public View getView(int position, View convertview, ViewGroup parent) {
ViewHolder holder;
View v = convertview;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) LayoutInflater
.from(mContext);
v = inflater.inflate(R.layout.list_items, null);
holder = new ViewHolder();
holder.firstLine = (TextView) v.findViewById(R.id.firstLine);
holder.secondLine = (TextView) v.findViewById(R.id.secondLine);
holder.icon1 = (ImageView) v.findViewById(R.id.icon1);
holder.icon2 = (ImageView) v.findViewById(R.id.icon2);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.firstLine.setText(testcontacts[position]);
holder.secondLine.setText(testcontacts[position]);
// holder.icon1.setImageBitmap((position & 1) == 1 ? mIcon1: mIcon2);
//call the images directly?
return v;
}
static class ViewHolder {
TextView firstLine;
TextView secondLine;
ImageView icon1;
ImageView icon2;
}
}
You are probably using something like this (here the doc):
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.layout_1, values);
in that case your layout must be a simple layout with a TextView.
If you wanna use your own layout you need to write a custom adapter.
Build your own ArrayAdapter, then you can make the layout work however you would like.
The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:
<?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"
// other attributes of the TextView
/>
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout, this.file)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.
You seem to be on the right lines. I'm not sure the exact issue with your code as I've not compared closely, but it works in this example.
The full tutorial is here: Android tutorial for beginners - 97 - ListView with Custom Layout
This avoids the error ArrayAdapter requires ID to be a TextView which I was also getting.

Display contents from ListView defined in regular Activity Android

I wanted to create a search view like the one Google uses. For this I created the following XML layout, which basically is a search bar and a button in the upper section of the screen and a ListView at the bottom of it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutSearch"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FF394952">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<EditText android:layout_height="wrap_content" android:id="#+id/searchTextBar" android:layout_width="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/searchButton" android:text="Buscar"></Button>
</LinearLayout>
<ListView
android:id="#+id/searchResultList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0" />
</LinearLayout>
And this is the code of the textViewResource that the ArrayAdapter demands on its constructor:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
Now, this is the code of the activity. So far, I just want to display the view with the contents (that's why I'm using a static String array for now).
public class SearchActivity extends Activity{
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item, COUNTRIES);
ListView lv = (ListView)this.findViewById(R.id.searchResultList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
However, when I run the activity I see the search bar but it doesn't display the ListView.
I've tried changing the extension of SearchActivity to ListActivity, but the program just crashes when I try to start it. I'm also aware of the existence of the Search Interface, but I just want to see this particular method work.
Why it doesn't display the contents of the ListView? Is there a way to fix this?
Thanks in advance
If you are going to use ListActivity you should be aware that ListActivity already has a ListView instance. You need to call its setListAdapter method to set the adapter for its ListView instead of instantiating your own ListView and setting the adapter on it. You can call getListView to get a handle on ListActvity's ListView and then set the click listener on that.
If you want to extend ListActivity then you must have a ListView with id #android:id/list. Change the id of your ListView, that should fix the crash when extending ListActivity.

Android ListView Adapter how to detect an empty list?

I have a ListView in my Activity which takes values from an extension class of SimpleAdapter.
This one is correctly working when I add or delete some data in it.
But my problem is, when my item list is empty, I would like to replace the listView by a message like "No item inhere" or something like that.
I tried to do it, this way:
class FavAdapter extends SimpleAdapter{
Activity context;
ArrayList<HashMap<String,String>> data;
String[] items;
int[] champs;
int layout;
FavAdapter(Activity _context, ArrayList<HashMap<String,String>> _data, int _layout, String[] _items, int[] _champs){
super(_context, _data, _layout, _items, _champs );
this.data = _data;
this.context = _context;
this.items = _items;
this.layout = _layout;
this.champs = _champs;
}
public View getView(int pos, View convertView, ViewGroup parent){
//Recycling
View row = convertView;
//Else get from XML
if(row == null){
LayoutInflater infla = context.getLayoutInflater();
row = infla.inflate(layout, null);
}
//If there are data
if(data.size() > 0){
/**** Here I am correctly constructing row *****/
...
}
//Else
else{
TextView txt = new TextView(ActivityFavoris.this);
txt.setText("Not data inhere");
LinearLayout ll = (LinearLayout) row.findViewById(R.id.result_lyt_principal);
ll.addView(txt);
}
return row;
}
Problem is: my list has never a size of 0. in fact when the data is empty the adapter is not called.
Can someone explain me how I can do it by another way?
Thanks in advance.
Edit : Using your answers I succeed on that. My solution is : setContentView has to be used with findViewById(android.R.id.empty), but if like me you have two different listView in your activity, which need different empty views, you will have to find it inside different layout. This way android will automatically call the right view when the listview is empty, and nothing have to be done in the adapter when data.size is 0.
example: in Activity :
public void onCreate(Bundle b){
super.onCreate(b);
lv1 = (ListView) findViewById(R.id.listviewone);
lv2 = (ListView) findViewById(R.id.listviewtwo);
//****set listview adater***//
...
//set empty view
lv1.setEmptyView(findViewById(R.id.layoutone).findViewById(android.R.id.empty));
lv2.setEmptyView(findViewById(R.id.layouttwo).findViewById(android.R.id.empty));
}
with that xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layoutone"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listviewone"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pas de favoris enregistrées" />
</LinearLayout>
<LinearLayout
android:id="#+id/layouttwo"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listviewtwo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pas de favoris enregistrées" />
</LinearLayout>
Use the method setEmptyView which sets the view to show if the adapter is empty.
The above answer sounds fine, but for the record the reason your method doesn't appear to be called is probably because your implementation of getCount() returns something like data.size() - meaning 0. This means the ListView will never call getView at all.
Instead you could do something like this:
public int getCount() {
if (data.size()==0) {
return 1;
}
return data.size();
}
I hope that's helpful.
You are calling this adapter in some activity. there the adapter is set to any listview. Before setting the adapter to the list view you can check what is the size of arraylist. if the size is zero then you can add "No item inhere" message to the arraylist by make a hashmap
Create a listview with the 'no items found' entry by default. When the first item is added, reinit the list and remove the default entry.

Categories

Resources