Aaron
Aaron

Reputation: 4480

Button in listAdapter not working

I am pulling some data from my db and get it to populate the TextView correctly. The problem I am running into is when I click on the button in the list the button does nothing. I added the code finish in onClick, but the button does not respond. I looked at other stack posts and tried the solutions, but they do not work. I am sure it is something obvious I am missing. Here is my code

<?xml version="1.0" encoding="utf-8"?>

<TableRow
    android:id="@+id/visitorNotLeft"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/names"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="" />

    <TextView
        android:id="@+id/companies"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="" />

    <Button
        android:id="@+id/timeOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" 
        android:focusableInTouchMode="false"
        android:descendantFocusability="blocksDescendants"
        android:layout_weight="1"
        android:text="@string/timeOut" />

</TableRow>

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    timeOut = (Button)v.findViewById(R.id.timeOut);
    timeOut.setTag(position);
    timeOut.setFocusable(false);
    timeOut.setFocusableInTouchMode(false);
    timeOut.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            finish();

        }

    });
}

Here is my adapter code

        @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        ListAdapter adapter = new SimpleAdapter(GetDataFromDB.this,
                productsList, R.layout.list_of_visitors_not_signed_out,
                new String[] { TAG_NAME, TAG_COMPANY }, new int[] {
                        R.id.names, R.id.companies });
        // updating listview
        setListAdapter(adapter);

    }

If you want me to add more code let me know.

Upvotes: 0

Views: 74

Answers (2)

mohammed momn
mohammed momn

Reputation: 3210

try this code and feed me back in any not obvious thing for you

ListAdapter adapter = new SimpleAdapter(GetDataFromDB.this, productsList, R.layout.list_of_visitors_not_signed_out,
    new String[] { TAG_NAME, TAG_COMPANY }, new int[] {
            R.id.names, R.id.companies }){
             public View getView(int position, View convertView, android.view.ViewGroup parent) {
   View v = super.getView(position, convertView, parent);
   Button buttonObject= (Button)v.findViewById(R.id.timeOut);
   buttonObject.setOnClickListener(new View.OnClickListener(){

          public void onClick(View v) {

          // Write your code here
        }
    });
    return v;
   }
  };

 // updating listview
 setListAdapter(adapter);'

Upvotes: 1

domi
domi

Reputation: 968

You should create onClickListener in your getView overriden function. See example below:

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.item, parent, false);

    TextView text = (TextView) rowView.findViewById(R.id.text);
    Button myButton = (Button) rowView.findViewById(R.id.mybutton);

    myButton.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            // Do something             
        }
    });


    return rowView;
}

Upvotes: 0

Related Questions