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
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
I want to hide the edit text and button field initially in list view and show that edit text and button for a particular raw in list view when that raw is clicked.So I tried to set the height to 0 in layout xml and then set it to some other value when user clicks on a raw, but it is not working I think my list view click event is not working.
In the Android layout that I have list view there are Image view as a button, edit text field and list view also. Like follows
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainPortal" >
<ListView
android:id="#+id/employeeListView"
android:layout_width="650dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:clickable="true">
</ListView>
<EditText
android:id="#+id/empPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/iv_start_journey_btn"
android:layout_marginBottom="56dp"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/employeeListView"
android:ems="10"
android:inputType="number"
android:hint="#string/add_emp_pin_hint" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/iv_start_journey_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/employeeListView"
android:layout_alignRight="#+id/empPin"
android:layout_marginBottom="84dp"
android:onClick="startJourney"
android:src="#drawable/start" />
</RelativeLayout>
I have used following custom lay out for the list view
<?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:orientation="vertical" >
<ImageView
android:id="#+id/emp_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="#drawable/person" />
<TextView
android:id="#+id/emp_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/emp_avatar"
android:layout_toRightOf="#+id/emp_avatar"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/empPin"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBottom="#+id/emp_avatar"
android:layout_alignRight="#+id/emp_number"
android:layout_toRightOf="#+id/emp_avatar"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/empAdd"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBaseline="#+id/empPin"
android:layout_alignBottom="#+id/empPin"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/empPin"
android:text="#string/emp_add_btn" />
Here I post the code of Activity.java.
public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
populateEmployeeList();
//populsteListView();
ListView list = (ListView) findViewById(R.id.employeeListView);
ArrayAdapter<Employee> adapter = new MylistAdapter();
list = (ListView) findViewById(R.id.employeeListView);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//I ADDED ON CLICK IMPLEMENTATION HERE, BUT THIS IS NOT WORKING
Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();
}
});
private void populateEmployeeList() {
...
}
private class MylistAdapter extends ArrayAdapter<Employee>{
public MylistAdapter(){
super(MainPortal.this,R.layout.item_view,employees);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
Employee currentEmployee = employees.get(position);
//Avatar
ImageView imageView = (ImageView) itemView.findViewById(R.id.emp_avatar);
imageView.setImageResource(currentEmployee.getAvatarId());
//Employee number
TextView emp_id = (TextView) itemView.findViewById(R.id.emp_number);
emp_id.setText(currentEmployee.getId());
et = (EditText) itemView.findViewById(R.id.empPin);
return itemView;
}
}
}
Above I have posted some codes that I think important.
Can any one please help me to do this task.
Thanks in advance.
Add the following attributes to your button
android:focusable="false"
android:focusableInTouchMode="false"
ListView setOnItemClickListener not working by adding button
Is your edittext taking focus on click of list item? If so remove the focus on edittext also.
Here your edittext inside the listview is having all the focus, so this listview onClick is not working. Remove the focus from the editext box and it will start working
// try this
public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
populateEmployeeList();
//populsteListView();
list = (ListView) findViewById(R.id.employeeListView);
ArrayAdapter<Employee> adapter = new MylistAdapter();
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//I ADDED ON CLICK IMPLEMENTATION HERE, BUT THIS IS NOT WORKING
Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();
}
});
}
}
In my Case setOnItemClickListener works fine in Android Versions Upto 5.1.1
But Having On Android 6.0.1. At last I found the solution
Try this
Remove these Lines from Your XML Layout
android:orientation="vertical"
tools:context=".ManageRequest"
android:contextClickable="true"
Works for Me in Android Version 6.0.1
Hope Your Problems Solved ..!!! Happy Coding :)
I am new to Android, and the first project I am working on is putting players onto a team. I have a listview that grabs all of the players from a database. Each name is supposed to have a checkbox next to them and then there is a button at the bottom that is supposed to update the database. So far this is what I am getting.
http://dyp.im/W79JdmfIk
Here is my activity
package com.example.sqllitetest;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class Tester extends ListActivity {
MyAdapter mListAdapter;
PlayersDBAdapter mDbHelper;
private Button button;
private Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new PlayersDBAdapter(this);
mDbHelper.open();
Cursor myCur = mDbHelper.fetchAllPlayers();
startManagingCursor(myCur);
setContentView(R.layout.playertoteam);
// registerForContextMenu(getListView());
// ((Tester) context).setContentView(R.layout.playertoteam);
//getListView().addFooterView(button);
mListAdapter = new MyAdapter(Tester.this, myCur);
setListAdapter((ListAdapter) mListAdapter);
Button button = (Button) findViewById(R.id.alertBox);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.playertoteam, cur);
}
#Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.playertoteam, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvListText = (TextView)view.findViewById(R.id.code);
CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.checkBox1);
tvListText.setText(cur.getString(cur.getColumnIndex(mDbHelper.KEY_NAME)));
cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(mDbHelper.KEY_ID))==1? false:true));
}
}
}
And here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/confirm" />
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/alertBox"
/>
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
</LinearLayout>
I would like to know how to remove the buttons from the rows and the check box that is underneath the main confirm button. Also I would like to know if this is the best way to do something like this, because, once again, I am new and I don't know how to search for the correct methods.
ResourceCursorAdapter creates views ,for every row of the ListView, that are inflated in newView() method. You are getting button in every row because of this reason.
Soln: Create a separate xml defining the row layout for the ListView and inflate this new layout in the newView() method. And have only the button and ListView in main layout i.e. playertoteam.xml
playertoteam.xml
<?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="wrap_content"
android:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/confirm" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/alertBox" />
</RelativeLayout>
row_layout.xml
<?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="wrap_content">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"/>
</RelativeLayout>
You can find example for ListView here
you can use addFooterView method of listview.
View footerView =
((LayoutInflater)
ActivityContext.
getSystemService
(Context.LAYOUT_
INFLATER_SERVICE)).inflate
(R.layout.footer_
layout, null, false);
ListView.addFooterView
(footerView);
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...