0

好吧,我不确定发生了什么。我经历了许多这样的“答案”,但没有任何结果。我的列表视图上运行了一个自定义适配器。我希望能够单击列表项以“查看更多”,但我什至无法单击进行注册。

这是我的活动:

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;
        }
    }

}

我错过了什么?

编辑:

这是我的 XML 文件...

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>

event_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>

编辑:

我想做的是从 XML 中显示标题,然后在列表视图中显示 XML 标题的其他 XML 字段 onClick ...

4

3 回答 3

3

也许您在 ListView 中的自定义视图具有可点击项目并消耗点击事件?

于 2013-07-10T13:10:55.817 回答
2

确保您的 ListView 具有焦点,并且没有其他可以窃取点击事件的可点击项目。

于 2013-07-10T13:11:53.450 回答
0

好吧,我不能 100% 确定它为什么现在可以工作,但是,我认为这与回顾我的 XML 并确保根据 Eclipse 没有遗漏任何内容甚至有点偏离有关。

感谢所有的评论。

对于未来的人:检查您的 XML。

于 2013-07-16T04:16:47.047 回答