如何获取按钮列表适配器内部的父视图? [英] How to get the parent view of a button within a list adapter?

查看:186
本文介绍了如何获取按钮列表适配器内部的父视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的来自巴西,我有一个包含按钮,图像和文字列表视图。 我想按一下按钮列表视图,并得到负责,我一下就行的观点。这样我就可以使上线的变化在哪里这是我的按钮。

发生什么事,如果code以下,当我按一下按钮,无论按钮列表中,他总是这样,在第一个列表项的变化,我想他做在各自的行。 因此这个名单

图像------------ --------文本 - 按钮
IMAGE ------------ ---------------文本 - 按钮
IMAGE ------------ ---------------文本 - 按钮
IMAGE ------------ ---------------文字 - BUTTON

我按一下按钮,他只是改变了他的行例如文本:

图像------------ --------文本 - 按钮
IMAGE ------------ --------------变化 - BUTTON< - CLICKED
IMAGE ------------ ---------------文本 - 按钮
IMAGE ------------ ---------------文字 - BUTTON

和..

图像------------ --------文本 - 按钮
IMAGE ------------ ---------------文本 - 按钮
IMAGE ------------ ---------------文本 - 按钮
IMAGE ------------ --------------变化 - BUTTON< - CLICKED

回顾和所使用的片段列表内的适配器 对不起,我的英语不好!

 公共类JAdapterList扩展了BaseAdapter实现OnClickListener {

    私人LayoutInflater mInflater;
    私人的ArrayList< JItemList> itens;

    公共JAdapterList(上下文的背景下,ArrayList的< JItemList> itens){
        // Itens阙preencheramØ列表视图
        this.itens = itens;
        // responsavel POR pegarØ布局做项目。
        mInflater = LayoutInflater.from(上下文);
    }

    公众诠释getCount将(){
        返回itens.size();
    }

    公共JItemList的getItem(INT位置){
        返回itens.get(位置);
    }

        众长getItemId(INT位置){
        返回的位置;
    }

    公共查看getView(INT的立场,观点来看,一个ViewGroup父){
        // PEGA O产品德acordo COM一个posçãO操作。
        JItemList项目= itens.get(位置);
        // inflaØ布局对podermos preencher OS dados

        鉴于= mInflater.inflate(R.layout.navlistitem,NULL);

        ((TextView的)view.findViewById(R.id.textListView))的setText(item.getNome());


        的ImageButton IB =(的ImageButton)view.findViewById(R.id.imageButton1);
        ib.setOnClickListener(本);

        返回查看;
    }

    @覆盖
    公共无效的onClick(视图v){

        // TODO自动生成方法存根


    查看VV = v.getRootView();


    ImageView的电视=(ImageView的)vv.findViewById(R.id.imageView1);
    TextView的texto =(TextView中)vv.findViewById(R.id.textListView4);
    texto.setText(CHANGE);
    tv.setAnimation(AnimationUtils.loadAnimation(tv.getContext(),R.anim.motion));




    }
}
 

解决方案

您可以设置文本视图是按钮的标签,这使得它更容易。 喜欢这个: 在getView:

 的TextView TextView的=((TextView中)view.findViewById(R.id.textListView))的setText(item.getNome());


    的ImageButton IB =(的ImageButton)view.findViewById(R.id.imageButton1);
    ib.setOnClickListener(本);
     ib.setTag(TextView的);
 

在onClickListener:

 公共无效的onClick(视图v){
    TextView的texto =(TextView中)v.getTag();
    texto.setText(CHANGE);
  }
 

i`m from brazil and I have a listview that contains buttons images and texts. I'm trying to click on the button listview and get the view responsible for the line I'm clicking. so I can make changes on the line where this my button.

what is happening and if the code below, when I click the button, whichever button the list he always does the change in the first list item, and I wanted him to do in their respective line. thus this list

IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------- TEXT -- BUTTON

I click on the button and he just change the text of his line example:

IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------CHANGE -- BUTTON <--CLICKED
IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------- TEXT -- BUTTON

AND..

IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------- TEXT -- BUTTON
IMAGE ------------ --------------CHANGE -- BUTTON <--CLICKED

Recalling that and used an adapter inside the fragment list Sorry , my english is bad!

public class JAdapterList extends BaseAdapter  implements OnClickListener {

    private LayoutInflater mInflater;
    private ArrayList<JItemList> itens;

    public JAdapterList(Context context, ArrayList<JItemList> itens) {
        //Itens que preencheram o listview
        this.itens = itens;
        //responsavel por pegar o Layout do item.
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return itens.size();
    }

    public JItemList getItem(int position) {
        return itens.get(position);
    }

        public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View view, ViewGroup parent) {
        //Pega o item de acordo com a posção.
        JItemList item = itens.get(position);
        //infla o layout para podermos preencher os dados

        view = mInflater.inflate(R.layout.navlistitem, null);

        ((TextView) view.findViewById(R.id.textListView)).setText(item.getNome());


        ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton1);
        ib.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub


    View vv = v.getRootView();


    ImageView tv = (ImageView) vv.findViewById(R.id.imageView1);
    TextView texto = (TextView) vv.findViewById(R.id.textListView4);
    texto.setText("CHANGE");
    tv.setAnimation(AnimationUtils.loadAnimation(tv.getContext(), R.anim.motion));




    }
}

解决方案

You can set the text view to be the tag of the button, that makes it much easier. Like this: In the getView:

TextView textView = ((TextView)  view.findViewById(R.id.textListView)).setText(item.getNome());


    ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton1);
    ib.setOnClickListener(this);
     ib.setTag(textView);

In the onClickListener:

 public void onClick(View v) {
    TextView texto = (TextView) v.getTag();
    texto.setText("CHANGE");
  }

这篇关于如何获取按钮列表适配器内部的父视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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