Listview的项目按钮单击

时间:2015-01-15 10:16:56

标签: android listview android-listview listadapter

在Android上使用LisviewListview的每个项目都有一个button,当我点击每个activity时,我想打开button

我的代码是这样的,它有效,但我认为有更好的方法来做到这一点。

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





    }

我的适配器代码,当我点击图像活动打开时,我希望这样:

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


}

1 个答案:

答案 0 :(得分:0)

尝试使用适配器中的接口,如下所示:

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

现在您可以在活动中设置clicklistener,如下所示:

// 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);