0

I created my own ListView but there is a problem that for my own list is dont work the click event handler(onListItemClick).

Source code:

MainActivity.java

    package com.example.notes;

    import java.util.ArrayList;

    import org.xmlpull.v1.XmlPullParser;

    import android.app.ListActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    public class MainActivity extends ListActivity {

        public static final String EXTRA_DATE = "Date";
        public static final String EXTRA_SHORT = "ShortMessage";
        public static final String EXTRA_LONG = "LongMessage";

        private ArrayList<NoteItem> list;

        //String [] names = {"Test1", "Test2", "Test3"};

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



        //ArrayAdapter<String> my = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);


        list = new ArrayList<NoteItem>();

        try {
            XmlPullParser parser = getResources().getXml(R.xml.notes);

            while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
                if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("note")) {
                    list.add(new NoteItem(parser.getAttributeValue(0), parser.getAttributeValue(1), parser.getAttributeValue(2)));
                }
                parser.next();
            }
        } catch(Throwable t) {
            Toast.makeText(this, getResources().getString(R.string.errMsg), Toast.LENGTH_SHORT)
                    .show();
        }


        NoteArrayAdapter adapter = new NoteArrayAdapter(this, R.layout.note, list);
        setListAdapter(adapter);

        Toast.makeText(this, "oncreate", Toast.LENGTH_LONG).show();
        //setListAdapter(my);

    }



    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        Log.d("ListItem", "LIST ITEM CLICKED!!!");
        Toast.makeText(this, "Item clicked!", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

NoteArrayAdapter.java

package com.example.notes;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class NoteArrayAdapter extends ArrayAdapter<NoteItem> {

    private Context context;
    private List<NoteItem> list;

    public NoteArrayAdapter(Context context, int resource,
            List<NoteItem> objects) {
        super(context, resource, objects);
        this.list = objects;
        this.context = context;
    }

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

        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) 
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.note, parent, false);
        }



            TextView textDate = (TextView) rowView.findViewById(R.id.textDate);
            TextView textShort = (TextView) rowView.findViewById(R.id.textShort);

            textDate.setText(list.get(position).getDate());
            textShort.setText(list.get(position).getShortMessage());

            return rowView;

            //return super.getView(position, convertView, parent);
        }
    }

NoteItem.java

package com.example.notes;

public class NoteItem {

    private String date;
    private String shortMessage;
    private String longMessage;

    public NoteItem(String date, String shortMessage, String longMessage) {
        this.date = date;
        this.shortMessage = shortMessage;
        this.longMessage = longMessage;
    }

    public String getDate() {
        return date;
    }

    public String getShortMessage() {
        return shortMessage;
    }

    public String getLongMessage() {
        return longMessage;
    }

}

note.xml

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <ImageButton
            android:id="@+id/editButton"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:src="@android:drawable/ic_menu_edit"/>

        <TextView
            android:id="@+id/textDate"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="test"
            android:gravity="left|center"/>

        <ImageButton
            android:id="@+id/deleteButton"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@android:drawable/btn_dialog"/>

        </LinearLayout>

    <TextView
        android:id="@+id/textShort"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Test"/>

</LinearLayout>

notes.xml

<?xml version="1.0" encoding="utf-8"?>
<Notes>

    <note
        date="26.08.1991"
        shortMessage="birthday"
        longMessage="Maxim Lazarenko birthday"/>

    <note
        date="04.01.1988"
        shortMessage="birthday"
        longMessage="Svetlana Rudaya birthday"/>

    <note
        date="26.08.1991"
        shortMessage="birthday"
        longMessage="Maxim Lazarenko birthday"/>

    <note
        date="04.01.1988"
        shortMessage="birthday"
        longMessage="Svetlana Rudaya birthday"/>

    <note
        date="26.08.1991"
        shortMessage="birthday"
        longMessage="Maxim Lazarenko birthday"/>

    <note
        date="04.01.1988"
        shortMessage="birthday"
        longMessage="Svetlana Rudaya birthday"/>

    <note
        date="26.08.1991"
        shortMessage="birthday"
        longMessage="Maxim Lazarenko birthday"/>

    <note
        date="04.01.1988"
        shortMessage="birthday"
        longMessage="Svetlana Rudaya birthday"/>

</Notes>
3
  • 1
    Could you please clarify what was expected and what went wrong? Commented Dec 26, 2013 at 9:50
  • By clicking on the item it should pop-up message handler but does not work as if ignored Commented Dec 26, 2013 at 9:53
  • Not fire the click event handler Commented Dec 26, 2013 at 9:54

2 Answers 2

1

Set android:focusable="false" for all clickable components in your note.xml. You are not getting callback to onListItemClick(), as these components(ImageButton) is taking the click event. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to use this:

    @Override
     protected void onListItemClick (ListView l, View v, int position, long id) {
   Toast.makeText(this, "Clicked row " + position, Toast.LENGTH_SHORT).show();
    } 

For More Details Go to this link :

Android ListActivity row click

2 Comments

It doesn't need because I use ListActivity
@lazexe check it out my edited answer. Does it works for you??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.