0

I have used the android-amazing-listview from :https://code.google.com/p/android-amazing-listview/

I added a setOnItemClickListener, But it does not work. Can someone please give me a hand :

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

    }
}

Single item View

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

1 回答 1

0

This implementation is wrong.

Either you write setOnItemClickListener for your ListView in your onCreate,

        lsComposer.setOnItemClickListener(new OnItemClickListener() {

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

or you will change it to another View's onClickListenner inside Adapter

Example:

        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);
            }
        });
于 2014-05-07T08:46:38.807 回答