Android ListView setOnItemClickListener not registering click with custom adapter

戏子无情 提交于 2019-12-10 12:21:49

问题


Well, I am not sure what is up. I've been through many SO "answers" without any results. I have a custom adapter running on my listview. I want to be able to click on the list item to "see more" but I cannot even get the click to register.

Here is my activity:

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.util.XmlDom;

public class MainActivity extends SherlockActivity {

    private AQuery aq;
    private ProgressDialog dialog;
    private static final String TAG = "INCIWEB";
    private ListView lv;
    protected Object IncidentAdapter;
    EditText inputSearch;
    String url;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setSubtitle("Incidents across the USA");
        aq = new AQuery(this);
        dialog = new ProgressDialog(this);
        dialog.setCancelable(true);
        dialog.setInverseBackgroundForced(false);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setMessage("Fetching Latest...");

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.USStates, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner s = (Spinner) findViewById(R.id.stateSpinner);
        s.setAdapter(adapter);

        s.setPrompt("Select a location...");

        final String USStates = s.getSelectedItem().toString();
        Log.e("STATE", USStates);

        s.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView,
                    View selectedItemView, int position, long id) {
                try {
                    getFeed(position);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {

            }

        });

    }

    public void getFeed(int num) {
        if (num == 0) {
            // latest updates - front page
            url = "http://inciweb.org/feeds/rss/incidents/";
        } else {
            // states
            url = "http://inciweb.org/feeds/rss/incidents/state/" + num + "/";
        }

        Log.e("URL", url);
        long expire = -1;
        aq.progress(dialog).ajax(url, XmlDom.class, expire, this,
                "getFeedCallback");
    }

    public void getFeedCallback(String url, XmlDom xml, AjaxStatus status) {
        List<XmlDom> entries = xml.tags("item");
        List<Incidents> incidents = new ArrayList<Incidents>();
        for (XmlDom entry : entries) {
            incidents.add(new Incidents(entry.text("title"),
                    entry.text("link"), entry.text("description"), entry
                            .text("published"), entry.text("geo:lat"), entry
                            .text("geo:long"), entry.text("georss:point")));
        }

        lv = (ListView) findViewById(R.id.list);
        lv.setTextFilterEnabled(true);
        lv.setAdapter(new IncidentAdapter(this,
                android.R.layout.simple_list_item_1, incidents));

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {

                Toast.makeText(MainActivity.this, id + "' was clicked.",
                        Toast.LENGTH_LONG).show();
            }
        });

    }

    private class IncidentAdapter extends ArrayAdapter<Incidents> {

        private List<Incidents> items;

        public IncidentAdapter(Context context, int textViewResourceId,
                List<Incidents> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        // Create a title and detail
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.incidents_item, null);
            }
            Incidents o = items.get(position);
            if (o != null) {
                TextView title = (TextView) v.findViewById(R.id.title);
                TextView published = (TextView) v.findViewById(R.id.published);
                TextView link = (TextView) v.findViewById(R.id.link);

                link.setMovementMethod(LinkMovementMethod.getInstance());

                TextView description = (TextView) v
                        .findViewById(R.id.description);
                TextView geoLat = (TextView) v.findViewById(R.id.geoLat);
                TextView geoLon = (TextView) v.findViewById(R.id.geoLon);
                TextView geoLatLon = (TextView) v.findViewById(R.id.geoLatLon);

                if (title != null) {
                    title.setText(o.getTitle());
                }
                if (published != null) {
                    published.setText(o.getPublished());
                }
                if (link != null) {
                    link.setText(o.getLink());
                }
                if (description != null) {
                    description.setText(o.getDescription());
                }
                if (geoLat != null) {
                    geoLat.setText(o.getGeoLat());
                }
                if (geoLon != null) {
                    geoLon.setText(o.getGeoLon());
                }
                if (geoLatLon != null) {
                    geoLatLon.setText(o.getGeoLatLon());
                }
            }
            return v;
        }
    }

}

What am I missing?

edit:

Here is my XML files...

activity_main.xml

<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=".MainActivity" >


    <Spinner
        android:id="@+id/stateSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inputSearch" />


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

</RelativeLayout>

incident_items.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/published"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/link"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/geoLat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/geoLon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/geoLatLon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />
    </LinearLayout>

</LinearLayout>

EDIT:

What I'd like to do is show the Title from the XML, then show the other XML fields onClick of the XML title from the listview...


回答1:


Maybe your custom Views in ListView has clickable items and consume click event?




回答2:


Make sure your ListView has the focus and there are no other clickable items that could steal the click events.




回答3:


Well I cannot be 100% sure why it is working now, however, I think it has to do with going back through my XML and making sure nothing was missing or even a bit off according to Eclipse.

Thanks for all the comments.

For future folks: CHECK YOUR XML.



来源:https://stackoverflow.com/questions/17571339/android-listview-setonitemclicklistener-not-registering-click-with-custom-adapte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!