Empty ListView with simple_list_item_multiple_choice layout - android

How to handle an empty ListView if I use simple_list_item_multiple_choice layout?
'Android Developers' say that I should use the android prefix
android:id="#android:id/empty
But cannot take in mind how to use it with simple_list_item_multiple_choice
protected ListView mFavList;
ArrayList<String> fakeFavs;
mFavList.setAdapter(new ArrayAdapter<String>(this.android.R.layout.simple_list_item_multiple_choice,fakeFavs);
if fakeFavs is null?

I solved my problem.
All I need - "empty" tag in main.xml layout and string of code:
mFavList.setEmptyView(findViewById(android.R.id.empty));
In my program fakeFavs is populating from text file.
In case of empty file, the empty list is properly handled.
Maybe it will be useful for somebody. Working example:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class HelloAndroid3 extends Activity {
/** Called when the activity is first created. */
static final String split_symbol = "\n";
protected ListView mFavList;
protected ArrayList<String> fakeFavs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fakeFavs = new ArrayList<String>();
this.mFavList = (ListView) this.findViewById(R.id.list_favorites);
refreshFavListItems();
}
private void refreshFavListItems() {
//put some code here that populates fakeFavs
//.......
mFavList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, fakeFavs));
mFavList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mFavList.setEmptyView(findViewById(android.R.id.empty));
//if fakeFavs will be empty, list shows "No Data"
}
}
main.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="match_parent" >
<ListView android:id="#+id/list_favorites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="No Data"/>

Related

what is the difference between notifyDataSetChanged and add(T object) or addAll(T... items) methods in array adapter?

I'm trying to learn List view and adapters, made a very basic app to learn in which I have a button that inserts a String object to the data source of an array adapter and the other view is list view which shows this data set. add() and addAll() method apends the data to the data set but when I do that list view shows it but I didn't call notifyDataSetChanged method, so why list view refreshed itself and if it did so then why do I need the method? Could anyone help me by giving some clarity? add(), addAll() and insert() methods already making the list view show the new added data but how? so what's the difference?
the app's only layout:
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="8dp">
<Button
android:id="#+id/insert_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="insert data"/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
the main activity code:
package famiddle.smart.apps.learningandroid;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button insertButton;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> data = new ArrayList<>();
data.add("hello world");
ListView listView = findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
insertButton = findViewById(R.id.insert_button);
insertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.add("hi there");
}
});
}
}

Listview widget isn't working in Android Studio

I'm new to Android Studio.For me there is a problem with listview. I've set all the ids properly and yet the problem occurs.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.prashanthwagle.myapplication.MainActivity">
<ListView
android:id="#+id/sample"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
</RelativeLayout>
Java:
package com.example.prashanthwagle.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] s={"asfasaf","bafaf","cbfbd"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.sample);
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s);
listView.setAdapter(a);
}
}
I Think that you should change
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s)
;
to
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R. android.R.layout.simple_list_item_1,s);
You have set the wrong layout for ArrayAdapter. Use this:
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,s);
The layout reference in the adapter initialization and assignment should not be the layout reference of your activity. Basically, this parameter tells the ListView what type of layout will each row of the list have. Read more about this from here: https://developer.android.com/guide/topics/ui/layout/listview.html

Showing error in ArrayAdapter ListView

I have written this code but it shows me error and i cannot identify where the problem is.My code is as follows-
package com.example.adapterexample;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listAdapter();
}
public void listAdapter() {
String[] myItems = { "Apple", "Orange", "Grape", "Watermilon" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.da_items, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
xml for da_items
<?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" >
</LinearLayout>
xml for activity_main
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
//tools:context="com.example.adapterexample.MainActivity"
//tools:ignore="MergeRootFrame" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
It shows me error: E/ArrayAdapter(30552): You must supply a resource ID for a TextView
I do not add any text view then why this error is coming?
JavaExperts need your valuable suggestion to solve this issue.
Check official docs of ArrayAdapter.
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1
Constructor
Parameters
context - The current context.
resource - The resource ID for a layout file containing a TextView to use when instantiating views.
objects - The objects to represent in the ListView.
You will notice that the array adapter is expecting that the second parameter is a resource id of a text view not a layout.
So, when you use this constructor:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.da_items, myItems);
R.Layout.da_items must be an xml layout where the first element must be a TextView, 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
/>
To be more sort of a custom textview, you can also use -
new ArrayAdapter<String>(this, R.layout.da_items,
R.id.the_id_of_a_textview_from_the_layout, myItems)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings.
The layout that you provide in the adapter - R.layout.da_items - should only contain a TextView because that's what android supports by default. So either use android.R.layout.simple_list_item_1 (this layout is provided by android) in place of your custom layout or make sure that your R.layout.da_items looks something like :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
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"
/>
Try this.
Add this in da_item.xml
<?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" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16sp" />
</LinearLayout>
And in MainActivity.java
listView = (ListView) findViewById(R.id.list1);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.da_item, myItems);
listView.setAdapter(adapter);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, myItems);
it will help you.
As you have used below code is creating problem
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.da_items, myItems);
So there is no need to make another layout and use it as R.layout.da_items
instead use
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, myItems);

LISTVIEW - onItemClick not working

I'm at my first steps into Android... I already read some of the answered question, and I tool a lot of tips, but I don't find how to solve my problem.
In the activity Categories.java I have a listview built starting from a database. The listview displys correctly, then I would like to start another activity from the OnItemClick, but I realized that I can't display even a toast.
I really hope some of you can help me I really don't find where is my error!! Thank you in advance!
Here is my code:
package com.example.myshopping;
import java.util.ArrayList;
import android.os.Bundle;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.app.Activity;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class Categories extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
final ListView lista= (ListView) findViewById(R.id.id_categorie);
MioDatabaseHelper myHelper;
myHelper= new MioDatabaseHelper(this);
final SQLiteDatabase db= myHelper.getReadableDatabase();
String [] columns_cat ={"nome_categoria"};
Cursor cursor= db.query("categorie", columns_cat, null, null, null, null, null);
int len= cursor.getCount();
cursor.moveToFirst();
final ArrayList <String> array_cat= new ArrayList<String>();
for (int i=0; i<(len); ++i){
array_cat.add(cursor.getString(0));
cursor.moveToNext();
}
ArrayAdapter<String> myadapter= new ArrayAdapter<String>(this, R.layout.listitem, R.id.listitemTextView, array_cat);
lista.setAdapter(myadapter);
lista.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(getApplicationContext(), "hello toast!!!!", Toast.LENGTH_LONG).show();
}
});
}
and here the xml file of the list items:
<?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"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/listitemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:includeFontPadding="false"
android:padding="5pt"
android:scrollHorizontally="false"
android:textSize="10pt" />
</LinearLayout>
and here the xml file of the categories.java file:
<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="wrap_content"
android:gravity="center_horizontal"
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=".Categories" >
<ListView
android:id="#+id/id_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textIsSelectable="true">
</ListView>
</RelativeLayout>
I see basic inconsistency in your code:
R.layout.categories//in set content view
and in array adapter:
ArrayAdapter<String> myadapter= new ArrayAdapter<String>(this, R.layout.listitem, R.id.listitemTextView, array_cat);
Change R.layout.listitem to R.layout.categories
or use a different constructor.
Is android:textIsSelectable="true" and android:descendantFocusability="blocksDescendants" attributes needed in your case? Using them wrong can cause focusing problems and the click listener doesn't work. Try removing both.
Also the list item contains only one child for the LinearLayout so it could just be TextView having width set to match_parent and height wrap_contentcombined with padding.
Also if you want to specyfy textView appearance Create XML Just with that textView without parent Noode, coz lists can be ricky sometimes :P try create another XML just to test that one thingChange this:<relativeLayout>
attributes..
<textView>
attributes..
</textView>
</relativeLayout> To<TextView>
attributes
</TextView>Basicaly you want stand alone TextView

How to get multiple selected items from a ListActivity in Android

I am basically trying to get which items have been selected within a multiple choice enabled ListActivity. I am sure that there are many beginning Android programmers that would love this to be clarified. This app runs fine until it gets to the FOR loop and tries to assign the values of the multi-choice items to ArrayList 'items'. I'm simply trying to use Toast to see if the multi-choice items are being assigned to the Arraylist 'items'. The app crashes when the button is clicked. testing is done in android 2.2 emulator
Here is my Main.xml layout
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Selected Items" />
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:choiceMode="multipleChoice"
android:textFilterEnabled="true" android:fastScrollEnabled="true" >
</ListView>
</LinearLayout>
and here is the code
package com.tests.TestCode;
import android.app.ListActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] list = {"wrenches","hammers","drills","screwdrivers","saws","chisels","fasteners"};
// Initializing An ArrayAdapter Object for the ListActivity
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ListView thelistview = (ListView) findViewById(android.R.id.list);
ArrayList<String> items = null;
SparseBooleanArray checkedItems = thelistview.getCheckedItemPositions();
if (checkedItems != null) {
for (int i=0; i<checkedItems.size(); i++) {
if (checkedItems.valueAt(i)) {
String item = adapter.getItem(checkedItems.keyAt(i)).toString();
items.add(item);
}
}
Toast.makeText(getBaseContext(), items.toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "checkedItems == null", Toast.LENGTH_LONG).show();
}
}
});
}
}
Much obliged to anyone that can provide a good answer as to the best way to go about accomplishing this ListActivity scenario, or anyone that can explain why the app crashes at the point of assignment to the ArrayList. Thanks!
Your items list is null and you try to use it before initializing it.
You need to use items = new ArrayList<String>(); somewhere before trying to use it, or you will keep getting NullPointerExceptions.

Categories

Resources