1

Using Lisview on Android. Each item of Listview has a button and I want to open activity when I click each button.

My code is like this, it works but I think that there is better way to do this.

  public class Resul extends Fragment{



    ImageView vi;
    @Override


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View v= inflater.inflate(R.layout.resul, container, false);
            listview = (ListView) v.findViewById(R.id.lista_resultados_liga);

 new DownloadJSON().execute();
             listview.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {

                        estado = ((TextView) view.findViewById(R.id.estado))
                                .getText().toString();
                        jor = ((TextView) view.findViewById(R.id.jor))
                                .getText().toString();
                        vi = ((ImageView) view.findViewById(R.id.imageView1));



                            vi.setOnClickListener(new View.OnClickListener() {
                                 @Override
                                    public void onClick(View v) {

                                     Intent in = new Intent(getActivity(),
                                          Videos.class);
                                    in.putExtra("id_video", resumen_id);
                                    startActivity(in);

                                    }
                                });

                    }


             }
             });


            return v;
        }


    // DownloadJSON AsyncTask
                private class DownloadJSON extends AsyncTask<Void, Void, Void> {

                    @Override
                    protected void onPreExecute() {
                        //code
                    }

                    @Override
                    protected Void doInBackground(Void... params) {
                        //code
                    }

                    @Override
                    protected void onPostExecute(Void args) {
                        // Locate the listview in listview_main.xml
                        listview = (ListView) getActivity().findViewById(R.id.mylist);
                        // Pass the results into ListViewAdapter.java
                        adapter = new LazyAdapterResul(getActivity(), arraylist);
                        // Set the adapter to the ListView
                        listview.setAdapter(adapter);
                        // Close the progressdialog
                        mProgressDialog.dismiss();
                    }
                }





    }

My adapter´s code, I want that when I click to image activity open:

public class LazyAdapterResul extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public LazyAdapterResultadosLiga(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

     public interface MyItemClickListener {
            void onItemClick(View view, int position);
        }

        private MyItemClickListener mItemClickListener;

        public void setMyItemClickListener(MyItemClickListener listener) {
            this.mItemClickListener = listener;
        }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables

        TextView equipo_local, equipo_visitante, estado_video;


        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.list_row, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml

        jor= (TextView) itemView.findViewById(R.id.jornada);
        estado= (TextView) itemView.findViewById(R.id.estado);
        vi = (ImageView) itemView.findViewById(R.id.imageView1);


        estado.setText(resultp.get(Resul.TAG_ESTADO));
        jor.setText(resultp.get(Resul.TAG_JOR));



        vi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(view, position);
                }
            }

        });



        return itemView;
    }


}
Elena
  • 181
  • 2
  • 12
  • 2
    Write your onclick code on list view adapter .. – duggu Jan 15 '15 at 10:19
  • But can I open activity from list view adapter? How? Intent in = new Intent(getActivity(), MyOtherActivity.class); startActivity(in); – Elena Jan 15 '15 at 10:20
  • @Elena use Intent in = new Intent(getContext(), MyOtherActivity.class); startActivity(in); – null pointer Jan 15 '15 at 10:22
  • Better you make a custom ListView and make a separate custom Adapter class for it – Bhavik Mehta Jan 15 '15 at 10:22
  • yes use custom adapter for another type of implementation http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – K Guru Jan 15 '15 at 10:24
  • it say that getContext() for the type new View.OnClickListener() is undefined on my adapter – Elena Jan 15 '15 at 10:25
  • `getContext()` is a method on the adapter. Inside an anonymous `OnClickListener` class you'll need to use `MyAdapter.this.getContext()` where `MyAdapter` is the name of your adapter class. – David Wasser Jan 15 '15 at 10:29
  • and how is this method? it says the method getContext() is undefined for the type of my adapter – Elena Jan 15 '15 at 10:31

1 Answers1

0

Try it with an interface in your adapter like this:

public class ListViewAdapter{    
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    private MyItemClickListener mItemClickListener;

    public void setMyItemClickListener(MyItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Find your image view and add clicklistener

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view, int position) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(view, position);
                }
            }
        });
        return convertView;
    }
}

And now you can set your clicklistener in your activity like this:

// Pass the results into ListViewAdapter.java
adapter = new LazyAdapterResultadosLiga(getActivity(), arraylist);
// add click listener
adapter.setMyItemClickListener(new MyItemClickListener(){
            @Override
            public void onItemClick(View view, int position) {
                Intent in = new Intent(getActivity(), MyOtherActivity.class);
                startActivity(in);
            }
});
// Set the adapter to the ListView
listview.setAdapter(adapter);
Mann
  • 661
  • 5
  • 9
  • Works but if I want to pass to this new activity the "estado" like this in.putExtra("estado", estado); how I get it from list item? Thank you so much – Elena Jan 15 '15 at 23:38
  • Thats the cool thing. You have the position in my onItemClick-Method. You can operate with it or can find some views with the view (item) that is passed by the method. Does it answer your question? Then please mark it and show me some reputation because this is a much cleaner version than yours ;) – Mann Jan 15 '15 at 23:42
  • I need without the position, I need to rescue the value of "estado" of this item. How can I find some views with the view (item), this I don´t understand, sorry and many thanks – Elena Jan 15 '15 at 23:48
  • I have done like this but not works estado = ((TextView) view.findViewById(R.id.estado)) .getText().toString(); in.putExtra("estado", estado); – Elena Jan 15 '15 at 23:50
  • Do in the onItemClick method something like this to get your reference: estado = view.findViewById(R.id.estado) – Mann Jan 15 '15 at 23:51
  • What happen? Do you have an output? – Mann Jan 15 '15 at 23:52
  • NullPointerException at estado = ((TextView) view.findViewById(R.id.estado)) .getText().toString(); I am doing it at Activity not in Adapter, is like this, not? – Elena Jan 15 '15 at 23:54
  • Ok I found our problem. We pass the imageview vi to the onItemClick-Method. Try to set the clicklistener on the itemView instead if the imageview vi – Mann Jan 15 '15 at 23:57
  • Or change mItemClickListener.onItemClick(view, position); to mItemClickListener.onItemClick(itemView, position); in your adapter – Mann Jan 15 '15 at 23:58
  • Thank you so so much for your patience, you are a machine, so so grateful – Elena Jan 16 '15 at 00:02