簡體   English   中英

使ListView按鈕在Android中可點擊?

[英]Make ListView Buttons Clickable in android?

這是列表視圖,我想單擊單元格的每個值...

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

並想要獲取該字段的值...

我有一個Listview,我想從MainActivity獲取該listview的單個項目的值...

    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 .....

        }

}

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

頭文件

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

}

設置自定義ListView后,您需要為每個元素添加一個ID。 然后,您可以檢測到所單擊按鈕的位置。 希望這對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM