Search code examples
androidlistviewbuttonandroid-cursor

Add one custom item to the ListView


I use a listview with cursoradapter, and I need to add a custom item as last item of the query result, that Ill use like a button. I need it even when the result is 0.

Activity oncreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lsto = (ListView) findViewById(R.id.listid);
    dbco1 = new dbClass(this);
    db = dbCo1.getReadableDatabase();       
    c = db.rawQuery( "SELECT * FROM ssnt", null);
    lao = new listAd(this, c, 1);
    lsto.setAdapter(lao);
    db.close();
    lsto.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ... }});
}

Adapter class:

public class listAd extends CursorAdapter {
    LayoutInflater inf;
    String lna;

    public listAd(Context context, Cursor c, int flags) {
        super(context, c, flags);
        inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void bindView(View view, Context context, Cursor cur) {
        lna = cur.getString(4);
        TextView rv = (TextView) view.findViewById(R.id.lro);
        rv.setText(lna);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View tiew = inf.inflate(R.layout.ro, parent, false);
        return tiew;
    }
}

ib.xml, The layout to add:

<?xml version="1.0" encoding="utf-8"?>
    <ImageButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bbb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ni"
    android:contentDescription="@string/desc"
    style="?android:attr/buttonBarButtonStyle" />

Thanks!

SOLUTION: used setEmptyView() AND addFooterView(), thanks arash.

The new oncreate function:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nv);
    setupActionBar();
            // setEmptyView wants an object, addFooterView wants an inflated layout
            // Howevere both function point to the same view, ib.xml above.
    ImageButton boo = (ImageButton) findViewById(R.id.bbb);
    lito = (ListView) findViewById(R.id.itli);
    View bo = getLayoutInflater().inflate(R.layout.ib, null);
    lito.setEmptyView(boo);
    lito.addFooterView(bo);
    dbCo = new dbClass(this);
    db = dbCo.getReadableDatabase();
    c = db.rawQuery( "SELECT * FROM ssnt", null);       
    lai = new itAd(this, c, 1);     
    lito.setAdapter(lai); 
    lito.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                  
                      // stuff to do
          }});
}

Solution

  • if it works like a button Why didn't you use setEmptyView when the list is empty

    YourList.SetEmptyView(yourView).
    

    or a FooterView when its not.