获取巴顿在列表视图中的位置 [英] Get the position of Button in List View

查看:218
本文介绍了获取巴顿在列表视图中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。

我想在列表视图

按钮的位置

在下面的图片,当我点击了编辑或删除按钮,我只是希望它在位置列表视图

怎么可能?

这是我的办公室管理屏幕。在获得 ListView中的所有数据运行时间从我的网络服务。

我用这个code获得列表视图中的数据 -


  ListAdapter适配器=新SimpleAdapter(Office_Mgmt.this,officeList,R.layout.office_mgmt_list_model,新的String [] {office_name},新的INT [] {R.id.label });setListAdapter(适配器);


解决方案

您可以创建自定义的适配器,通过你的应用程序作为参数的情况下,设置点击:

  listView.setAdapter(新PesquisaAdapter(这一点,anunciantescidades,这一点);

然后,在你的适配器的构造函数,你将有一个 OnClickListener 来收到参数这个你通过:

 公共PesquisaAdapter(上下文的背景下,列表与LT; Anunciante> anunciantes,OnClickListener onClick1)

在适配器的getView方法,你可以设置按钮的onClickListener:

  @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    视图V = inflater.inflate(R.layout.your_custom_layout,NULL);
    按钮BTN =(按钮)v.findViewById(R.id.yourbutton);
    btn.setOnClickListener(onClick1);
}

在Java中,您可以农具 onClickListener ,然后你想与你的按钮是什么:

  @覆盖
公共无效的onClick(视图v){
    如果(yourbutton.getId()== v.getId()){
        最终诠释位置= listView.getPositionForView((的LinearLayout)v.getParent());
    }
}

所以,你将在你点击列表中的位置,并且可以管理编辑的点击单独删除。

希望它帮助!

I have one problem.

I want to get the position of Button in List View.

In below image when I clicked in Edit or Delete button i just want its position in List View.

How it possible?

This is my Office Management Screen. All the data getting in ListView run-time from my web-service.

I get the data in List View by using this code-


ListAdapter adapter = new SimpleAdapter(Office_Mgmt.this, officeList, R.layout.office_mgmt_list_model, new String[] { "office_name" }, new int[] { R.id.label}); 

setListAdapter(adapter);

解决方案

You can create your custom adapter, passing the context of your app as argument to set the click:

listView.setAdapter(new PesquisaAdapter(this, anunciantescidades, this);

Then, in the constructor of your adapter you will have a OnClickListener to recieve the argument this you passed:

public PesquisaAdapter(Context context, List<Anunciante> anunciantes, OnClickListener onClick1)

In the getView method of the adapter, you can set onClickListener of the button:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.your_custom_layout, null);
    Button btn = (Button) v.findViewById(R.id.yourbutton);
    btn.setOnClickListener(onClick1);
}

In your java, you can implements onClickListener, and then do what you want with your button:

@Override
public void onClick(View v) {
    if(yourbutton.getId() == v.getId()){                
        final int position = listView.getPositionForView((LinearLayout)v.getParent());
    }
}

So, you will have the position in the list that you clicked, and you can manage the click of the edit and delete separately.

Hope it helps!

这篇关于获取巴顿在列表视图中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆