简体   繁体   中英

Make ListView Buttons Clickable in android?

This is the list view and I want to click the every value of the cell...

_________________________________
               |                 |
         1     |                 |
               |           2     |
_______________|_________________|
               |                 |
               |             4   |
       3       |                 |
_______________|_________________|

And want to get the values of the field...

I have a Listview where I want to get the values of individual item of that listview from my MainActivity...

    public class MainActivity extends Activity implements OnItemClickListener {
         ListView mylist;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            LayoutInflater i = LayoutInflater.from(this);

            ListView mylist=(ListView)findViewById(R.id.listexample);

            List<Item> items = new ArrayList<Item>();
            items.add(new Header(i, "Group","See More",R.drawable.ic_launcher));
            items.add(new EventItem(i, R.drawable.demoimage , R.drawable.demoimage ,R.drawable.demoimage ,R.drawable.demoimage ));




            MyListAdapter adapter = new MyListAdapter(this, items);
            mylist.setAdapter(adapter);
            mylist.setOnItemClickListener(this);
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    //What will the Value here .....

        }

}

............................................

Header.java

package com.antew.listexample;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import com.antew.listexample.MyListAdapter.RowType;

public class Header implements Item, OnClickListener {
    private final String catagory;
    private final String see_more;
    private final int image;
    private final LayoutInflater inflater;

    public Header(LayoutInflater inflater, String catagory, String see_more,
            int image) {
        this.catagory = catagory;
        this.see_more = see_more;
        this.image = image;
        this.inflater = inflater;
    }

    @Override
    public int getViewType() {
        return RowType.HEADER_ITEM.ordinal();
    }

    @Override
    public View getView(View convertView) {
        if (convertView == null) {

            convertView = (View) inflater.inflate(R.layout.header, null);
        }

        TextView cat = (TextView) convertView
                .findViewById(R.id.header_catagory);
        cat.setText(catagory);

    //  cat.setOnClickListener(Header.this);

        TextView s_m = (TextView) convertView.findViewById(R.id.header_seemore);
        s_m.setText(see_more);

        ImageButton imb = (ImageButton) convertView
                .findViewById(R.id.header_imageButton1);

        imb.setBackgroundResource(image);
        return convertView;
    }

//  @Override
//  public void onClick(View v) {
//      // TODO Auto-generated method stub
//
//      Log.d("*****************CLCIK", v.toString() + "CATAGORY CLICKED ");
//
//  }

}

once you setup your custom ListView you need to refrence each element with an id. Then you can detect the position of the button clicked. Hope this help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM