1

I have the following XML layout:

enter image description here

At the moment I can only click the 'Add' button below the EditText, even though I have implemented and overridden the 'OnListViewClick' method from the extend ListActivity super class.

I believe this is a focus problem when using a button with a ListView.

Hopefully someone can tell me how I can set the XML layout so I can click both the button and ListView.

OnItemListClick method code:

package com.example.flybase2;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ShoppingList extends ListActivity implements OnClickListener {

Button AddItem;
ListView showItems;
SimpleCursorAdapter cursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.shoppinglistlayout);

    AddItem = (Button) findViewById(R.id.btnAddItem);

    showItems = (ListView)findViewById(android.R.id.list);

    AddItem.setOnClickListener(this);

    setList();

}

@Override
public void onClick(View clickedAdd) {


    AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);

        builder.setTitle("Enter Item Details:");

                            LinearLayout layout = new LinearLayout(this);
                            layout.setOrientation(LinearLayout.VERTICAL);

                            final EditText titleBox = new EditText(this);

                            titleBox.setHint("Item Name:");
                            layout.addView(titleBox);

                            final EditText quantityBox = new EditText(this);

                            quantityBox.setHint("Item Quantity");
                            layout.addView(quantityBox);

                            final EditText priceBox = new EditText(this);

                            priceBox.setHint("Item Price.");
                            layout.addView(priceBox);

                            builder.setView(layout);


                            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            try
                            {

                                Editable valueItem = titleBox.getText();
                                Editable valueAmount = quantityBox.getText();
                            Editable valuePrice = priceBox.getText();

                                String itemDescription = valueItem.toString();
                                String s = valueAmount.toString();
                                int itemAmount = Integer.parseInt(s);
                                String a = valuePrice.toString();
                                int itemPrice = Integer.parseInt(a);

                            DBHandlerShop addItem = new DBHandlerShop(ShoppingList.this, null, null);
                            addItem.open();
                            addItem.insertItems(itemDescription, itemAmount, itemPrice);
                            addItem.close();

                            }
                            catch(Exception e)
                            {
                            Dialog e1 = new Dialog(ShoppingList.this);
                            e1.setTitle("Item unsuccesfully added");
                            TextView txt = new TextView(ShoppingList.this);
                            txt.setText("Success");
                            e1.setContentView(txt);
                            e1.show();

                            }
                            finally
                            {
                                Dialog e = new Dialog(ShoppingList.this);
                            e.setTitle("Item succesfully added.");
                            TextView txt = new TextView(ShoppingList.this);
                            txt.setText("Success");
                            e.setContentView(txt);
                            e.show();

                        setList();
                            }

                              }

                            });

                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int whichButton) {

                              }
                            });

                            builder.show();

}

@Override
protected void onListItemClick(ListView l, View v, int position, long idd) {
    super.onListItemClick(l, v, position, idd);

final CharSequence[] items = {"Edit Appointment Details", "Delete Appointment"};

    Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingList.this);

    alertDialogBuilder.setTitle("Appointment Options:");

    alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {

            if (items[item].equals("Edit Appointment Details")) {

            }

            else if (items[item].equals("Delete Appointment")) {

            }

            }  

        });

    alertDialogBuilder.show();

}


private void setList() {

    DBHandlerShop DBShop = new DBHandlerShop(this, null, null);

    DBHandlerShop searchItems = new DBHandlerShop(this, null, null);

    searchItems.open();

    Cursor cursor = searchItems.getItems();

    startManagingCursor(cursor);

    String [] from = new String [] {DBShop.KEY_ITEMSHOP, DBShop.KEY_ITEMNUM, DBShop.KEY_ITEMPRICE};
    int [] to = new int [] {R.id.txtSetItem, R.id.txtSetAmount, R.id.txtSetPrice};

    cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist, cursor, from, to);
    showItems.setAdapter(cursorAdapter);

}

}

This is my XML code:

<?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
        android:layout_width="match_parent"
        android:layout_height="84dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imgLink"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/viewcon" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:gravity="center"
            android:text="Shopping List"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="35sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/txtItemDes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item To Add To The List:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/inputAppointName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter A Name" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnAddItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
user1352057
  • 3,162
  • 9
  • 51
  • 117
  • The Button shouldn't block touch events from reaching the ListView. Please post the code for your `onListItemClick()` method. (Also `wrap_content` is extremely inefficient for a ListView's height, consider using `match_parent`.) – Sam Feb 11 '13 at 18:27
  • @Sam I have added the onListItemClick code. – user1352057 Feb 11 '13 at 18:29
  • +1 for a fast response, -1 for poor indentation and so much whitespace... – Sam Feb 11 '13 at 18:30
  • @Sam I've tidied the code up. When you say about the button blocking the touch events, how is my XML doing this? – user1352057 Feb 11 '13 at 18:32
  • I don't see the problem yet. Could you post more code? – Sam Feb 11 '13 at 18:38
  • @Sam I have added the whole class. I have not implemented any sort of onClickListener for the listview. I have never had to do this before on my other listviews, but then I have not been extending the onClickListner class. – user1352057 Feb 11 '13 at 18:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24334/discussion-between-sam-and-user1352057) – Sam Feb 11 '13 at 18:55

1 Answers1

1

In chat you posted the XML for the rows in your ListView. One of your LinearLayouts in the rows layout is clickable (android:clickable="true"), this will consume the touch event preventing it from reaching onListItemClick(). Simply remove this line.


Addition
I suggest a slightly different approach. It is clear that you want a custom row layout that is Checkable (by clicking anywhere on the row). I posted a detailed answer to: CheckedTextView checkmark in ListView row not showing up, a while back and there is a vague tutorial here (you need to click on the link to the "Begemot" library.)

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • that is a great post, many thanks. After your help I managed to sort my issue by making sure the checkbox within my XML layout was set to focusable:false. This now allows me to click the add button....the row in the listview (which loads my dialog) and also the checkbox within the row in any order. Again many thanks. – user1352057 Feb 11 '13 at 19:50