令人惊讶的ListView与onclicklistener无法正常工作,请帮忙

时间:2014-05-07 08:37:59

标签: android listview android-listview onclick endlessadapter

我使用了来自https://code.google.com/p/android-amazing-listview/

的android-amazing-listview

我添加了一个setOnItemClickListener,但它不起作用。有人可以帮我一把:

PaginationDemoActivity

package com.foound.amazinglistview.demo;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.foound.widget.AmazingAdapter;
import com.foound.widget.AmazingListView;

public class PaginationDemoActivity extends Activity {
    AmazingListView lsComposer;
    PaginationComposerAdapter adapter;
    ImageLoader imageLoader;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pagination_demo);
        imageLoader = new ImageLoader(this);

        lsComposer = (AmazingListView) findViewById(R.id.lsComposer);
        lsComposer.setLoadingView(getLayoutInflater().inflate(R.layout.loading_view, null));
        lsComposer.setAdapter(adapter = new PaginationComposerAdapter());

        adapter.notifyMayHaveMorePages();
    }

    public void bRefresh_click(View v) {
        adapter.reset();
        adapter.resetPage();
        adapter.notifyMayHaveMorePages();
    }

    class PaginationComposerAdapter extends AmazingAdapter {
        List<Composer> list = Data.getRows(1).second;

        private AsyncTask<Integer, Void, Pair<Boolean, List<Composer>>> backgroundTask;

        public void reset() {
            if (backgroundTask != null) backgroundTask.cancel(false);

            list = Data.getRows(1).second;

            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Composer getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        protected void onNextPageRequested(int page) {
            Log.d(TAG, "Got onNextPageRequested page=" + page);

            if (backgroundTask != null) {
                backgroundTask.cancel(false);
            }

            backgroundTask = new AsyncTask<Integer, Void, Pair<Boolean, List<Composer>>>() {
                @Override
                protected Pair<Boolean, List<Composer>> doInBackground(Integer... params) {
                    int page = params[0];

                    Log.e("more page", "page: " + page);
                    return Data.getRows(page);
                }

                @Override
                protected void onPostExecute(Pair<Boolean, List<Composer>> result) {
                    if (isCancelled()) return; 

                    Log.e("onPostExecute", "result: " + result.first);
                    list.addAll(result.second);
                    nextPage();
                    notifyDataSetChanged();

                    if (result.first) {
                        // still have more pages
                        notifyMayHaveMorePages();
                    } else {
                        notifyNoMorePages();
                    }
                };
            }.execute(page);
        }

        @Override
        protected void bindSectionHeader(View view, int position, boolean displaySectionHeader) {
        }

        @Override
        public View getAmazingView(int position, View convertView, ViewGroup parent) {
            View res = convertView;

            if (res == null) res = getLayoutInflater().inflate(R.layout.item_composer, null);

            // we don't have headers, so hide it
            res.findViewById(R.id.header).setVisibility(View.GONE);

            TextView lName = (TextView) res.findViewById(R.id.lName);
            TextView lYear = (TextView) res.findViewById(R.id.lYear);
            TextView lId = (TextView) res.findViewById(R.id.lId);
            // Locate the ImageView in listview_item.xml
            ImageView lImg = (ImageView) res.findViewById(R.id.lImg);

            final Composer composer = getItem(position);
            lName.setText(composer.name);
            lYear.setText(composer.year);
            lId.setText(composer.id);

            Log.e("getAmazingView PRINT THE URL 1111111111", "URL: " + composer.img);
            // Capture position and set results to the ImageView
            // Passes img images URL into ImageLoader.class
            imageLoader.DisplayImage(composer.img, lImg);
            Log.e("222","333");


            //test
            lsComposer.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Log.e("onItemClick","4444444444444444444444");


                    /*// getting values from selected ListItem
                    String lId =   ((TextView) view.findViewById(R.id.lId))  .getText()   .toString();
                    String lName = ((TextView) view.findViewById(R.id.lName)).getText()   .toString();
                    String lYear = ((TextView) view.findViewById(R.id.lYear)).getText()   .toString();
                    String lImg =  ((ImageView) view.findViewById(R.id.lImg)).getContext().toString();
                    */
                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(), SingleItemView.class);
                    in.putExtra("id", composer.id);
                    in.putExtra("title", composer.name);
                    in.putExtra("s_desc", composer.year);
                    in.putExtra("img", composer.img);

                    Log.e("onItemClick PRINT THE URL 777777777777", "onItemClick: ");

                    startActivity(in);

                }
            });
            //test */


            return res;

        }




        @Override
        public void configurePinnedHeader(View header, int position, int alpha) {
        }

        @Override
        public int getPositionForSection(int section) {
            return 0;
        }

        @Override
        public int getSectionForPosition(int position) {
            return 0;
        }

        @Override
        public Object[] getSections() {
            return null;
        }

    }
}

单项视图

package com.foound.amazinglistview.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class SingleItemView extends Activity {
    // Declare Variables
    String id;
    String name;
    String year;
    String img;
    String position;
    ImageLoader imageLoader = new ImageLoader(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from singleitemview.xml
        setContentView(R.layout.singleitemview);

        Intent in = getIntent();
        // Get the result of id
        id = in.getStringExtra("id");
        // Get the result of title
        name = in.getStringExtra("name");
        // Get the result of s_desc
        year = in.getStringExtra("year");
        // Get the result of img
        img = in.getStringExtra("img");



        // Locate the TextViews in singleitemview.xml
        TextView lId =   (TextView) findViewById(R.id.lId);
        TextView lName = (TextView) findViewById(R.id.lName);
        TextView lYear = (TextView) findViewById(R.id.lYear);

        // Locate the ImageView in singleitemview.xml
        ImageView imgimg = (ImageView) findViewById(R.id.lImg);

        // Set results to the TextViews
        lId.setText(id);
        lName.setText(name);
        lYear.setText(year);

        // Capture position and set results to the ImageView
        // Passes img images URL into ImageLoader.class
        imageLoader.DisplayImage(img, imgimg);
    }

1 个答案:

答案 0 :(得分:0)

这种做法是错误的。

您在onCreate中为setOnItemClickListener撰写ListView

        lsComposer.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                    // action
            }
        });

或者您将其更改为onClickListenner内的其他视图Adapter

示例:

        res.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                    Intent in = new Intent(getApplicationContext(), SingleItemView.class);
                    in.putExtra("id", composer.id);
                    in.putExtra("title", composer.name);
                    in.putExtra("s_desc", composer.year);
                    in.putExtra("img", composer.img);

                    Log.e("onItemClick PRINT THE URL 777777777777", "onItemClick: ");

                    startActivity(in);
            }
        });