Question

I am working on a simple app where user can see a list of notes (just description) in listview and when he clicks on some note, it opens the whole note in a new activity where he can edit it etc. The problem occurred when I decided to add a delete button to each list item. After this, the row became unclickable whereas the button is clickable. I read several similar topics, but non of them helped me. My question is how to make both button and listview "row" clickable? I am not also sure about efficiency etc. If you think this is not a good way how to do that, let me know and possibly give me better solution, otherwise I am going to stick with this one.

user_notes.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

list_note.xml:

    <TextView
        android:id="@+id/list_note_description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="17dip"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/ibtn_delete_note"
        android:onClick="test"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:layout_weight="0.2"
        android:focusable="false"
        android:scaleType="fitXY"
        android:src="@drawable/delete_note_icon" />

public class UserNotes extends ListActivity{



    private ProgressDialog progressDialog;
    private ImageButton ibtn_delete_note;

    private UserData userData;
    private DBFacade dbFacade;
    private JSONArray jsonArray;
    private ArrayList<HashMap<String, String>> userNotes;

    private static final String URL_LOAD_NOTES = "";
    private static final String URL_UPDATE_NOTE = "";
    private static final String TAG = "UserNotes";

    //Drawer attributes
    String[] drawer_menu_titles;
    private ListView drawer_list;


    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_notes);


        drawer_menu_titles = getResources().getStringArray(R.array.drawer_menu_array);
        drawer_list = (ListView)findViewById(R.id.left_drawer);

        drawer_list.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawer_menu_titles));
        drawer_list.setOnItemClickListener(new DrawerItemClickListener());

        userData = UserData.getInstance();
        dbFacade = DBFacade.getInstance();
        userNotes = new ArrayList<HashMap<String,String>>();

        new loadUserNotes().execute();



        ListView listView = getListView();


        listView.setItemsCanFocus(true);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //int noteId = Integer.valueOf(((TextView) view.findViewById(R.id.list_note_id)).getText().toString());
                Intent i = new Intent(UserNotes.this, DisplayNote.class);
                i.putExtra("index", position);
                Log.d(TAG, "Index: " + String.valueOf(position));
                startActivityForResult(i, 100);
            }
        });

    }

    public void test(View v){
        Log.d(TAG, "Clicked");

    }

class loadUserNotes extends AsyncTask<String, String, String>{

        private JSONObject jsonObject;

        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            progressDialog = new ProgressDialog(UserNotes.this);
            progressDialog.setMessage("Loading notes...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            progressDialog.show();
        }


        @Override
        protected String doInBackground(String... arg0) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("logged_in_user_id", String.valueOf(userData.getUserId())));

            jsonObject = dbFacade.makeHttpRequest(URL_LOAD_NOTES, "GET", params);
            Log.d(TAG, jsonObject.toString());
            return null;
        }

        protected void onPostExecute(String file_url){

            progressDialog.dismiss();

            try{
                if(jsonObject.getInt("success") == 1){
                    jsonArray = jsonObject.getJSONArray("notes");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject o = jsonArray.getJSONObject(i);

                        Note tempNote = new Note(o.getInt("note_id"), o.getString("description"), o.getString("data"));
                        userData.addNote(tempNote);

                        HashMap<String, String> hashMap = new HashMap<String, String>();
                        hashMap.put("id", o.getString("note_id"));
                        hashMap.put("description", o.getString("description"));
                        userNotes.add(hashMap);
                        //Log.d(TAG, userNotes.get(0).toString());

                    }

                }
                Log.d(TAG, "Test: " + userNotes.get(0).toString());
                Toast.makeText(UserNotes.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
            }
            catch (JSONException e){
                e.printStackTrace();
            }

            runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    UserNotesAdapter listAdapter = new UserNotesAdapter(UserNotes.this, 
                            userNotes, 
                            R.layout.list_note, 
                            new String[] {"description"},
                            new int[] {R.id.list_note_description});

                    /*
                    ListAdapter listAdapter = new SimpleAdapter(UserNotes.this, 
                            userNotes, 
                            R.layout.list_note, 
                            new String[] {"description"},
                            new int[] {R.id.list_note_description});
                    */
                    setListAdapter(listAdapter);
                }

            });
            }

        }

    public class UserNotesAdapter extends SimpleAdapter{

            public UserNotesAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
                super(context, data, resource, from, to);

            }

            public View getView(int position, View convertView, ViewGroup parent) {

                View view = super.getView(position, convertView, parent);
                view.setClickable(true);
                view.setFocusable(true);
                view.setBackgroundResource(android.R.drawable.menuitem_background);     
                return view;
            }

        }

Thanks in advance!

Was it helpful?

Solution

add android:descendantFocusability="blocksDescendants" in your listview item (row) layout.this will enable both row item controls click as well as list item click.

 <?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:layout_gravity="center"  android:descendantFocusability="blocksDescendants">
<TextView
        android:id="@+id/list_note_description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="17dip"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/ibtn_delete_note"
        android:onClick="test"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:layout_weight="0.2"
        android:focusable="false"
        android:scaleType="fitXY"
        android:src="@drawable/delete_note_icon" />
</LinearLayout>

once try this ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top