使ListView按钮在Android中可点击?

时间:2013-07-03 12:02:08

标签: android listview

这是列表视图,我想点击单元格的每个值...

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

想要获得该领域的价值......

我有一个Listview,我希望从我的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 ");
//
//  }

}

1 个答案:

答案 0 :(得分:0)

设置自定义ListView后,您需要使用id引用每个元素。然后,您可以检测单击按钮的位置。希望这能帮到你!