1
votes

I have a listview with textView and a button in each line, i'm trying to get the text by clicking on the button not by clicking the whole line but the adapterView method:(AdapterView arg0, View v,int position, long arg3) is not working for buttons click.

public View getView(int position, View convertView, ViewGroup parent) {
        //Set the view for each item in the list view
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.employeeitem, null);
        }
        //Get the Textviews from the row view and set the appropriate values for them
        TextView labelName=(TextView) v.findViewById(R.id.labelName);
        TextView labelAddress=(TextView)v.findViewById(R.id.labelAddress);
        TextView labelImmat=(TextView)v.findViewById(R.id.labelImmat);
        labelName.setText(array[position].marque);
        labelAddress.setText(array[position].categorie);
       labelImmat.setText(array[position].Prix_Achats);
        return v;
    }

This is how I select item by clicking the line of the listview but, I want to select item by clickin on the button not the whole line:

listEmployeeDetails.setOnItemClickListener(new OnItemClickListener(){

           public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
     {

             TextView tv=(TextView)v.findViewById(R.id.labelName);
             String name=tv.getText().toString();


             Toast.makeText(getApplicationContext(), "Contact Selected "+name, Toast.LENGTH_LONG).show();
     }
    });
6
post your adapter getView code.iTurki

6 Answers

2
votes

set an onClickListener to your button:

Button button = (Button) getActivity().findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
       // button was clicked
    }

});
1
votes

You can set a tag to the button to access it from getView() method. In the tag you can store the text of the item when you are declaring it programatically or via xml.

public View getView(int position, View convertView, ViewGroup parent) {
    //Set the view for each item in the list view
    View v = convertView;
    //Do your view stuff here
    Button btn = (Button)v.findViewById(R.id.your_button_id);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String str = (String)v.getTag();
            //Do whatever you want with your str here
        }
    });
    return v;
}
1
votes

get text from textview by clicking on a button

    btn.setTag(textView.getText().toString());
btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        String s =(String)v.getTag();


    }
});
0
votes

I have done something similar using a cursor adapter.

Inside your adapter define the onClickListener. Before that you will have to use getTag and setTag functions to send and retreive the information you want to recover inside the function.

myButton.setTag("Value I want to store");

    myButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String value = (String) v.getTag();
            //Whatever you want to do
        }
    });

Hope it helps :)

0
votes
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(getApplicationContext(), "Clicked "+parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

        }

    });

parent.getItemAtPosition(position).toString()

This method returns the current selected item at that position

-1
votes

It took me three days to solve this problem but the solution was quite obvious. My list is inflated from SQL server using JSONArray and what I had to do in the getView() method was simply to pass the TextView into the button's setTag() after I have inflated it with jsonOjbect. Then in the button's onClick() method I had to get the tag. Take a look at the code, hope I help.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

   ListCell cell;//ListCell is my widgets class holder

    //set up the convertView

    if(convertView==null){

        convertView=inflater.inflate(R.layout.menu_custom_layout, null);
        cell=new ListCell();
        cell.Diet=(TextView)convertView.findViewById(R.id.dietEat); //Diet is the custom list layout textview
        cell.eatButton=(Button)convertView.findViewById(R.id.eatButtton); //eatButton is the button to be clicked
        cell.eatButton.setOnClickListener(this);

    }else{
        cell=(ListCell)convertView.getTag();

    }

    //Change data of each cell
    try {

        JSONObject jsonObject=this.dataArray.getJSONObject(position);
        cell.Diet.setText(jsonObject.getString("food_name"));
        cell.eatButton.setTag(cell.Diet.getText()); //Tag for button's onclick Listener

    } catch (JSONException e) {

        e.printStackTrace();

    }

    return convertView;
}

                @Override
public void onClick(View v) {

String fud=(String) v.getTag(); //Getting the tag and parsing it to string

}