this is my first time using the ListView and have have tried out many ways to make this workable. Firstly I used within the xml file under the to specify the "android:entries" with a string-array, it displayed but I could not activate the onClickListener. The other method were similar to the one that I have, some were where I used "extend Acitvity" instead. Thanks in advance for helping me.
Basically, I want to use a customized textView row for each item on the list and show a toast for each clickable row. My problem is that the application would crash the moment I try to display this activity. I am not sure where the problem is as there isn't any syntax error. The error messages is ( Sorry, I would place a picture but I can't - new user ) :
FATAL EXCEPTION: main
java.lang.RuntimeException:Unable to start activity ComponentInfo(edu.....)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.access$600(ActivityTHread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Loooper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4974)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygotaInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by:java.lang.UnsupportedOperationException: addView(View, LayoutParams)...
at android.widget.AdapterView.addView(AdapterView.java:471)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:489)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:396)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindows.setContentView(PhoneWindow.java ...)
at android.app.Activity.setContentView(Activity.java:1892)
at edu.nyp.wonderful.Menu.onCreate(Menu.java:14)
at android.app.Activity.performCreate(Activity.java:4538)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityTHread.java:2158)
... 11 more lines ( I cannot show more to type out )
Menu.java v
package edu.nyp.wonderful;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Menu extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Log.d("Menu:", "after set content view");
String[] items = { "Set Pins", "View Pins", "List of Pins", "Email Summary", "Edit Session's Info" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menu_row, R.id.menu_name, items);
setListAdapter(adapter);
Log.d("Menu:", "after setting adapter");
}
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "Clicked position: " + position, Toast.LENGTH_SHORT).show();
}
}
menu.xml v
<?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:background="#drawable/background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="54dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:src="#drawable/logo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Menu"
android:textColor="#DC1700"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:typeface="serif" />
</LinearLayout>
<View android:id="#+id/separator1"
android:background="#000000"
android:layout_width = "fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp" />
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#000000"
android:dividerHeight="1dp" >
<TextView android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nothing" />
</ListView>
</LinearLayout>
*menu_row.xml v*
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="23dp"
android:shadowRadius="5"
android:shadowColor="#000000"
android:shadowDy="3"
android:shadowDx="3"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" />
Yeah, this is the problem.
android:id="#+id/android:list"
It should be
android:id="#android:id/list"
Fix it and check, report back if the problem persists.
Update: I can't believe I didn't see it earlier :P
You have a TextView inside the Listview. This is not possible. If you intend to use the "Empty View" Have them both inside a LinearLayout/FrameLayout. But the Listview cannot enclose the TextView, they should lie next to each other. Then, make sure you set the "Empty View" property in the "ListView" to the id of the Textview in this case #android:id/empty (please correct this as well)
Try changing your code like this
public class Menu extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Log.d("Menu:", "after set content view");
String[] items = { "Set Pins", "View Pins", "List of Pins", "Email Summary", "Edit Session's Info" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menu_row, R.id.menu_name, items);
ListView lv = getListView();
lv.setAdapter(adapter);
Log.d("Menu:", "after setting adapter");
}
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "Clicked position: " + position, Toast.LENGTH_SHORT).show();
}
}
Tell me if it works...
Related
I am trying to make a ListView with a custom adapter and add an onClickListener and a drag-and-drop functionality to sort the list.
I was looking at a lot of examples and similar problems, but I couldn't make it work, so I think my problem may be specific, because I use a whole layout as an item of the list, but I am not sure.
If I can provide any further information, please tell me.
You can find the whole project here
The implementation of my ListActivity is:
package com.jimdo.welcomeghosts.cipherrsa;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TabKeyActivity extends ListActivity {
//Defining an adapter which will handle the data of the listview
KeyAdapter adapter;
private ListView list;
//dummy data field TODO fill this shit up by using ArrayList
Key key_data[] = new Key[]
{
new Key(1, R.drawable.iconpair1, "This"),
new Key(2, R.drawable.iconpair2, "This'"),
new Key(3, R.drawable.iconpair1, "That"),
new Key(4, R.drawable.iconpair2, "That'")
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keymanager);
//Setting an ADD button as last item of the ListView
list = (ListView) findViewById(android.R.id.list);
list.addFooterView(getLayoutInflater().inflate(R.layout.footerview,null));
//Setting adapter for adding Items to ListView
adapter = new KeyAdapter(this, R.layout.key, key_data);
setListAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
This is my Key.xml (a row element in the List)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"><RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/checkBox"
android:layout_toStartOf="#+id/checkBox"
android:id="#+id/keyRow">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="50px"
android:layout_height="50px"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:longClickable="true"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/imgIcon"
android:paddingLeft="5dp">
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing the shit."
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textSize="#dimen/abc_text_size_headline_material" />
</FrameLayout>
</RelativeLayout>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_margin="5dp" />
</RelativeLayout>
Drag-Sort Listview is what you need.
Have a look at the following thread.
I'm asking my user for a postcode via an editText then i use a button to fetch and display store locations in a listView using a simpleCursorAdapter.
Now i want to be able to launch a new activity by selecting an item in the listView and passing along some string information.
I can't get the onClickListener to register my clicks.
Is it because i'm using an Activity instead of a ListActivity?
I'd rather have both the input (the EditText) and the results (ListView) both in the same activity.
activity_pcentry:
<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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/enter_post" />
<EditText
android:id="#+id/etPostCode"
android:imeOptions="actionSearch"
android:inputType="textCapCharacters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/search_button"
android:onClick="DoPostSearch" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
stockrow_group:
<?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:clickable="true"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/branch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
<TextView
android:id="#+id/post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
<TextView
android:id="#+id/telephone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
</LinearLayout>
PostCodeEntryActivity.java:
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class PostCodeEntryActivity extends Activity{
private MyDatabase stockistsDB;
public EditText enteredCode;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pcentry);
stockistsDB = new MyDatabase(this);
}
public void DoPostSearch(View v) {
enteredCode = (EditText)findViewById(R.id.etPostCode);
String postalCode = enteredCode.getText().toString();
Cursor results = stockistsDB.getStockistsFromPostCode(postalCode);
if (results != null && results.getCount() > 0) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.stockrow_group,
results, new String[]{"fld_BranchName","fld_PostCode","fld_Tel"},
new int[]{R.id.branch,R.id.post,R.id.telephone},
0);
listView = (ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(PostCodeEntryActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
listView.setAdapter(adapter);
} else {
Toast.makeText(PostCodeEntryActivity.this, "No results found, please check your postcode", Toast.LENGTH_SHORT).show();
}
}
}
Do you have any clickable elements on the rows themselves? Please post the XML from R.layout.stockrow_group.
If you have buttons on that layout for example you might need to add the following to those buttons:
android:focusable="false"
android:focusableInTouchMode="false"
This will make sure the click on the list item is not getting blocked by the clickable item on the row itself.
The actual solution:
Do not make the rows of the listview clickable. This will "eat" up the click event by registering an empty click handler. Remove the following line from the rows of the list view:
android:clickable="true"
Implement AdapterView.onItemClickListener in your activity`
Change
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
To
listView.setOnItemClickListener(this)
Add unimplemented methods
My activity contains a ListView that is populated from an SQLite Database. However I find that the items on the ListView are not clickable. They are not clickable at all in the emulator or on the device. I know there are a lot of similar questions and I tried every possible one of them but to no avail. Please help me make the items clickable.
My Activity Code :
package com.tintin.scheduler_3;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
public class List_Course extends ListActivity {
DatabaseHelper db;
SimpleCursorAdapter dataAdapter;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.course_list);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(List_Course.this, Add_Course.class));
}
});
Log.v("Button", "On Click Done");
db = new DatabaseHelper(List_Course.this);
Log.v("Button", "On Click Done2");
displayList();
Log.v("Button", "On Click Done3");
db.close();
}
public void onResume(){
Cursor newCursor = db.getAllCourses();
dataAdapter.changeCursor(newCursor);
super.onResume();
db.close();
}
public void displayList(){
Cursor cursor = db.getAllCourses();
String from [] = new String[] {db.colName,db.colDisplay};
int to[] = new int[] {R.id.textView1, R.id.textView2};
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem, cursor, from, to, 0){
public View getView(int position, View convertView, ViewGroup parent){
final View row = super.getView(position, convertView, parent);
if(position % 2 == 0)
row.setBackgroundColor(Color.parseColor("#D8D8D8"));
else
row.setBackgroundColor(Color.parseColor("#D0D0D0"));
return row;
}
};
ListView lv = getListView();
lv.setAdapter(dataAdapter);
registerForContextMenu(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v("Click","Anything "+arg2+" "+arg3);
}
});
cursor.close();
db.close();
}
}
The Layout of this Activity :- (NOTE: I found that without the TextView the list populated from bottom up even though android:stackfromBottom was not set. So I put in the TextView and put the ListView below that)
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#D0D0D0" >
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Add a Course" />
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="1dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" >
</TextView>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv"
android:layout_above="#+id/add"
android:layout_alignParentLeft="true"
android:divider="#FFFFFF"
android:clickable="true"
android:dividerHeight="2dp" >
</ListView>
</RelativeLayout>
And the listitem.xml that defines how rows are to be shown in the listview :-
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
EDIT:: - I noticed that the items on my ListView are actually clickable. The problem however is that on clicking the list items the orange selection color is not shown. So unless the log is checked, it is not apparent which row was clicked on.However if I remove the part where I am overriding the getView method of the SimpleCursorAdapter while defining dataAdapter, on clicking the items there is an orange selection color that appears in the background of the clicked row.
In other words I am not getting any color for the focused row in list view.
Please run it in one of AVDs to understand what I am trying to say here. I would appreciate if anyone can help me get that color while also overriding the getView method.
I am almost 100% sure that your problem is that some of the views rendered inside the ListView's items is avoiding the ItemClick event. In order to solve this, start by removing the follwing attributes from the TextView:
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAppearance="?android:attr/textAppearanceSmall"
Try to override onListItemClick() instead of setting OnItemClickListener. Subclasses of ListActivity should override this method.
http://developer.android.com/reference/android/app/ListActivity.html
I think all problems are in your xml's code.
Change your listitem.xml as..
?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="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
And from code behind use below code.Ofcourse use this in your onCreate() method
ls1 = (ListView) findViewById(R.id.yourListviewName);
ls1.setOnItemClickListener(new OnItemClickListener()
{
#Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
}
});
Alternatively see similar issues here..
android-how-to-programmatically-click-select-tap-a-listview-item/
how-to-click-a-listview-item-in-android
I write the program but when i run this program, the program has runtime error :(
The error in LogCat is :
your content must have a listview whose id attribute is 'android.r.id.list'
I set android:id="#android:id/list" in ListView in activity_main.xml file.
I write my code, Please read this and help me .
MainActivity.java:
public class MainActivity extends ListActivity {
private TextView text;
private static final String[] items={"test", "test1" , "test2" , "test3" , "test4" , "test5" , "test6"};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this,R.layout.row,R.id.label,items));
text=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,Long id){
text.setText(items[position]);
}
}
activity_main.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/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>
row.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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/pic"
android:padding="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pic"/>
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Cheers.
Please extend Activity instead of ListActivity,
Then get its view from layout file
ListView listView = (ListView) findViewById(R.id.listView);
Then call listView.setAdapter(/*agruments*/); method
instead of calling setListAdapter().
compare your code nothing difference....
mainActivity
package com.example.testdemo;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private TextView text;
private static final String[] items = { "test", "test1", "test2", "test3",
"test4", "test5", "test6" };
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_);
setListAdapter(new ArrayAdapter<String>(this, R.layout.lable,
R.id.label, items));
text = (TextView) findViewById(R.id.selection);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
text.setText("" + items[position]);
super.onListItemClick(l, v, position, id);
}
}
}
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" >
<TextView
android:id="#+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
</LinearLayout>
row.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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dip"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
you have to get the instance of a listview like
ListView lv=getListView();
Change this line
android:id="#android:id/list"
to
android:id="#+id/list"
My question is very basic, as is my experience! Sorry about the length of the post!
I am trying to create an AutoCompleteTextView with a listener so that I can do "stuff" when an item is selected.
The AutoCompleteTextView works fine, in so far as I can type in 2 characters and then select an item from the list which appears.
But the application never executes the listener code.
All my code, xml and log trace is here, so what is the simple piece that I am missing?
I've searched all the questions and it seems to be a common problem but none of the (very few) answers work for me.
Any help will be appreciated.
SOURCE
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
public class A_New_StartActivity extends Activity {
private final String LOG_TAG = "CJM";
private static String result = "No result";
private static final String[] myitems = {
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(LOG_TAG, "Entered onCreate");
ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(this, R.layout.destination_item, myitems);
AutoCompleteTextView itemView = (AutoCompleteTextView) findViewById(R.id.autocomplete_items);
itemView.setAdapter(itemAdapter);
itemView.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
result = "Selected";
Log.d(LOG_TAG, "Showing Result 1");
TextView result_view = (TextView) findViewById(R.id.result_view);
result_view.setText(result);
View scroll_visibility = findViewById(R.id.result_scroll);
scroll_visibility.setVisibility(View.VISIBLE);
View result_visibility = findViewById(R.id.result_view);
result_visibility.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView<?> parent) {
Log.d(LOG_TAG, "In Nothing Selected");
result = "Nothing";
Log.d(LOG_TAG, "Showing Result 2");
TextView result_view = (TextView) findViewById(R.id.result_view);
result_view.setText(result);
View scroll_visibility = findViewById(R.id.result_scroll);
scroll_visibility.setVisibility(View.VISIBLE);
View result_visibility = findViewById(R.id.result_view);
result_visibility.setVisibility(View.VISIBLE);
}
});
Log.d(LOG_TAG, "completed onCreate");
}
}
MAIN.xml in R.layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
</TextView>
<TextView
android:id="#+id/item_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/request_item"
android:visibility="visible" />
<AutoCompleteTextView
android:id="#+id/autocomplete_items"
android:completionThreshold="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:visibility="visible" />
<ScrollView
android:id="#+id/result_scroll"
android:layout_width="220dp"
android:layout_height="290dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:visibility="invisible" >
<TextView
android:id="#+id/result_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:scrollbars="vertical"
android:text="#string/result"
android:visibility="invisible" />
</ScrollView>
<Button
android:id="#+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="125dp"
android:text="#string/button_text" />
DESTINATION_ITEM.xml in R.layout
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
STRINGS.xml in R.values
<resources>
<string name="app_name">A_New_Start</string>
<string name="request_item">Please enter the first 2 letters of the item</string>
<string name="result">Initialised</string>
<string name="button_text">OK</string>
LOG output
05-26 10:19:05.550: D/CJM(269): Entered onCreate
05-26 10:19:05.570: D/CJM(269): completed onCreate
You are setting wrong Listener for AutoCompleteTextView. You have to set addTextChangedListener() and do the processing in the override methods of addTextChangedListener.