Question

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




    }
}
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top