Search code examples
androidandroid-layoutandroid-custom-viewgrid-layout

Button inside Grid layout is null


I am creating custom grid. Each row has 1 button and 2 textviews. I am trying to assign values/text to each programmatically. I am able to set text for both the text views but, findviewbyId for button returns null.

Grid is like:

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dip"
        android:horizontalSpacing="10dip"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:background="#FFFFFF"
        android:padding="5dip"
        android:id="@+id/gvMYActivitySearch" />

Row layout is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnHistory"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:background="@drawable/button_background"
        android:text="History"/>  

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="35dp" />

    <TextView
        android:id="@+id/tvContact"
        android:layout_width="wrap_content"
        android:layout_height="35dp" />

</LinearLayout>

Custom grid java file is like

public class CustomGrid extends BaseAdapter {

private Context mContext;
private final String[] names;
private final String[] Contact;
private final String[] ID;

public CustomGrid(Context c,String[] names,String[] Contact, String[] ID ) {
    mContext = c;
    this.Contact = Contact;
    this.names = names;
    this.ID = ID;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return names.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //grid = new View(mContext);
        grid = inflater.inflate(R.layout.myactivity_singlerow, null);

    } else {
        grid = (View) convertView;
    }

    TextView tvName = (TextView) grid.findViewById(R.id.tvName);
    TextView tvContact = (TextView) grid.findViewById(R.id.tvContact);
    Button btnHistory = (Button) grid.findViewById(R.id.btnHistory);
    tvName.setText(names[position]);
    tvContact.setText(Contact[position]);
    if(btnHistory!=null) {
        btnHistory.setId(Integer.parseInt(ID[position]));


        btnHistory.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new MainActivity().testButton(view.getId());

                    }
                }
        );
    }

    return grid;
}

When I try to debug, I can see for the first time when position is '0', btnHistory is not null. But after that when position is 1, btnHistory is null.

What could possibly go wrong? When both the textviews are not null, why btnHistory is null?


Solution

  • Comment to improve your code: callback somewhere outside your getView

    I mean this:

       public class CustomGrid extends BaseAdapter implements OnClickListener {
            if(btnHistory!=null) {
                btnHistory.setId(Integer.parseInt(ID[position]));
                btnHistory.setTag((Integer)view.getId());
    
                btnHistory.setOnClickListener(this);
            }
    
            public void onClick(View v) {
              new MainActivity().testButton(v.getTag());
            }
        }