0

我想在我的 ListActivity 行的任意位置单击以继续进行另一个活动。目前,当单击 ImageView 时,我能够在日志中看到行位置,但单击未在行中的其他任何位置注册。此外,Toast 命令会使程序崩溃。对我既定目标的任何帮助将不胜感激。谢谢!

event_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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:contentDescription="@string/event_icon_desc"
        android:src="@drawable/event_icon" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/event_tv"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:textIsSelectable="true" />

        <TextView
            android:id="@+id/location_tv"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:singleLine="true"
            android:textIsSelectable="true" />

        <TextView
            android:id="@+id/date_tv"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textIsSelectable="true" />

        </LinearLayout>
</LinearLayout>

display_events_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/event_ll" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <TextView
        android:id="@+id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/tv_empty" />

</LinearLayout>

显示事件.java:

public class DisplayEvents extends ListActivity {

    private ProgressDialog progressDialog = null;
    private ArrayList<Event> events = new ArrayList<Event>();
    private EventAdapter eventAdapter;
    private Runnable viewEvents;
    //private final int ITEMS_PER_VIEW = 10;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.disp_events_activity);

        new EventTask().execute();

        this.eventAdapter = new EventAdapter(this, R.layout.event_row, this.events);
        setListAdapter(this.eventAdapter);

        viewEvents = new Runnable() {
            @Override
            public void run() {
                getEvents();
            }
        };
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;
    }

    private void getEvents() {
        runOnUiThread(adaptEvents);
    }

    private Runnable adaptEvents = new Runnable() {

        @Override
        public void run() {
            if (events != null && events.size() > 0) {
                eventAdapter.notifyDataSetChanged();

                for (int i=0; i<events.size(); i++)
                    eventAdapter.add(events.get(i));
            }

            progressDialog.dismiss();
            eventAdapter.notifyDataSetChanged();
        }
    };


    private class EventAdapter extends ArrayAdapter<Event> {
        private ArrayList<Event> events;

        public EventAdapter(Context context, int textViewResourceId, ArrayList<Event> events) {
            super(context, textViewResourceId, events);
            this.events = events;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v  = convertView;
            if (v == null) {
                LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(R.layout.event_row, null);
            }

            Event event = events.get(position);
            if (event != null) {
                TextView event_tv = (TextView) v.findViewById(R.id.event_tv);
                TextView loc_tv = (TextView) v.findViewById(R.id.location_tv);
                TextView date_tv = (TextView) v.findViewById(R.id.date_tv);

                if (event_tv != null)
                    event_tv.setText("Event: " + event.getEventName());
                if (loc_tv != null)
                    loc_tv.setText("Location: " + event.getPlace() + " - " + event.getCity());
                if (date_tv != null)
                    date_tv.setText("Date: " + event.getDate());
            }

            v.setOnClickListener( new OnClickListener() {
                public void onClick(View v) {
                    Log.v("text", "Image clicked, row %d"+position);
                    Toast.makeText(DisplayEvents.this, position, Toast.LENGTH_LONG).show();
                }
            });

            return v;
        }
    }


    public class EventTask extends AsyncTask<Void, Void, ArrayList<Event>> {

        @Override
        protected ArrayList<Event> doInBackground(Void... Void) {
            ArrayList<Event> dbEvents = new ArrayList<Event>();
            BuyTicketsConnection buyConnection = new BuyTicketsConnection();

            dbEvents = buyConnection.getEventsList();

            return dbEvents;
        }

        protected void onPostExecute(ArrayList<Event> dbEvents) {
            events = dbEvents;

            Thread thread = new Thread(null, viewEvents, "GetEventsBackground");
            thread.start();

            progressDialog = ProgressDialog.show(DisplayEvents.this, "Please wait...", "Retrieving events...", true);
        }
    }
}

编辑:

我现在有这个方法:

@覆盖

protected void onListItemClick (ListView l, View v, int position, long id){
    Log.v("text", "Image clicked, row %d"+position);
    Toast.makeText(DisplayEvents.this, position, Toast.LENGTH_LONG).show();
}

在我的 DisplayEvents (ListActivity) 类中,但仍然没有骰子。另外,我希望单击响应该列表项中的任何内容(请注意,该行布局中有许多视图应该继承单击事件处理程序)。

似乎问题是我应该如何让 event_row.xml 中的所有视图(ImageView 和 3 个 TextViews)与这个 OnListItemClick 处理程序相关联。在该 OnListItemClick 处理程序的上下文中如何识别整行(由 2 个 LinearLayout 中的 4 个视图组成)?

4

3 回答 3

1

使用setOnItemClickListener在您的DisplayEvents ActivitysetOnClickListener从您的EventAdapter.

于 2013-05-12T10:19:39.270 回答
0

问题是您的某些项目视图是可聚焦的和/或可点击的(因此它们会吃掉点击事件并导致 ListView 无法接收事件以知道您点击了列表项)。对于触发项目单击事件的列表,它必须能够接收触摸事件而不会被子项吃掉。

在您的项目布局中,您有 3 个TextView都具有textIsSelectable=true,这意味着它TextView本身将吃掉触摸事件。您需要禁用它才能触发 onListItemClick() 事件。

于 2013-09-25T19:25:44.473 回答
0

我遇到了这个问题并删除

安卓:textIsSelectable="真"

将解决它。

于 2015-03-26T05:38:26.770 回答