Android custom ListView not working - android

I am trying to create a custom listview using an ArrayAdapter with each row displaying an Image & TextView. I am using the following code but the application fails to run:
MainActivity.java
package demo.android.listviewadapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
String[] buzzWords = {"Buzz-1", "Buzz-2", "Buzz-3", "Buzz-4", "Buzz-5", "Buzz-6", "Buzz-7"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setListAdapter (new ArrayAdapter<String>(this, R.layout.customlayout, R.id.txtName, buzzWords));
}
#Override
public void onListItemClick (ListView parent, View v, int position, long id) {
Toast.makeText(this, "You Clicked: " + buzzWords [position], Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
customlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgName"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/txtName"
/>
</LinearLayout>
activity_main.xml is the layout file which maps to the MainActivity. However, I use the custom_layout.xml file to customize the layout when assigning the adapter. Please can someone tell what am I doing wrong? Thanks in advance.

For using android ListActivity your ListView should have the id
android:id="#android:id/list"
Please refer Link

missing
setContentView(R.layout.activity_main);
into the onCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter (new ArrayAdapter<String>(this, R.layout.customlayout, R.id.txtName, buzzWords));
}
and also..
into activity_main.xml replace #+id/android:list to #android:id/list

Change your custom layout as
customlayout.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="fill_parent"
android:textSize="30sp" >
</TextView>
And change this
setListAdapter (new ArrayAdapter<String>(this, R.layout.customlayout, R.id.txtName, buzzWords));
to this in OnCreate method
setListAdapter (new ArrayAdapter<String>(this, R.layout.customlayout,buzzWords));

Related

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);

Getting a False return from listview

I am getting a false return when implementing a listview within a framelayout/tab layout. For the life of me I can't seem to figure out why I am getting a false return on my app when implementing a listview. The code works on a external project but I cant seem to intergrate it into this one. I will list the main activity groups used within the project and the xml file for the page that the string is meant to appear on. Sorry its alot of code but this has been driving me insane over the last few days cause it should be relatively simple.
The xml layout files are held in framelayout coding within the main.xml(which is not shown) so have I got the ID wrong when referencing? Any help to solve this problem would be amazing as I know its a really simple solution that Im just missing. Thanks guys.
If I havnt explained it well a picturee of the error is below
View the page here http://i41.tinypic.com/do04ee.png
Their are no log errors.
Main Activity
(Please note I have removed code to make it more relavant and clear and to show the links from activity to activity)
package workout.fitty;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class WorkoutFittyActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// More
**intent = new Intent().setClass(this, MoreActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("More",
res.getDrawable(R.drawable.tab_more))
.setContent(intent);
tabHost.addTab(spec);**
tabHost.setCurrentTab(1);
}
}
MoreActivity
package workout.fitty;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MoreActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.more_layout);
Intent Intent = new Intent(this, MyListActivity.class);
}
}
MyListActivity
package workout.fitty;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "BMI", "Body Measurements", "Logs",
"Feedback", "How To Use", "About" };
// Use your own layout
**ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.more_layout, R.id.label, values);
setListAdapter(adapter);**
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
And the XML file
<?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" >
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
Main XML File with container and framelayout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
change your:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.more_layout, R.id.label, values);
in
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.more_layout, values);

Item clic with ListView Android don't work correctly

I can not get the correct screen open.
I have the following code, and I also added the corresponding class in the manifest.
Please, if you can give me some idea.
Thank you.
listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
ListAdapter adp = (ListAdapter)parent.getAdapter();
//lista.get(position);
Seccion s = lista.get(Integer.parseInt(adp.getItem(position).toString()));
if (s.getTitle().contains("Estudiar")) {
//sel.setText("Seleccionado el del if"+ s.getTitle());
Intent i = new Intent(LiverpoolguideActivity.this,tipoListado.class);
startActivity(i);
}
}
});
The code tipoListado.class :
package jorgechu.com.liverpoolguide;
import java.util.ArrayList;
import jorgechu.com.liverpoolguide.Secciones.Seccion;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class tipoListado extends Activity {
public class LiverpoolguideActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listadotipo);
}
}
}
And the code screen 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"
android:orientation="vertical"
>
<TextView
android:id="#+id/textViewListadoTipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tipo:"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ListView
android:id="#+id/listViewType"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Focusable views inside a ListView item will disable the ability to select ListView items. Applying android:focusable="false" to the TextView will allow OnItemClick to work again.

Empty ListView with simple_list_item_multiple_choice layout

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"/>

Simple TodoList Application

I'm trying to create a simple to-do list application from a book on Android development. When I try to run it, I get a "this application has stopped working" message. I have absolutely no clue why, except for the fact that it has something to do with a fatal error at setContentView(R.layout.main). Can someone help me fix it?
package com.test.tadalist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class TadaList extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listView = (ListView)findViewById(R.id.listView);
final EditText editText = (EditText)findViewById(R.id.editText);
final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
listView.setAdapter(adapter);
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
todoItems.add(0, editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
return true;
}
return false;
}
});
}
}
layout/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="fill_parent">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Entry"
/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
You're missing a lot in your layout 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">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Entry"
/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
A stack trace would help, but I imagine it's likely because you don't have a container for your two views. Try enclosing your two views within something like a LinearLayout.

Categories

Resources