How to get the parent view of a button within a list adapter?

陌路散爱 提交于 2019-12-01 13:34:39

问题


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));




    }
}

回答1:


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



回答2:


For example for a checkBox (same for the rest of views): View viewParent = (View)checkBox.getParent();

But you would like to do might be easier, check this post: OnItemClick listener not working in Custom ListView

Good luck :)



来源:https://stackoverflow.com/questions/14570512/how-to-get-the-parent-view-of-a-button-within-a-list-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!