samedi 21 novembre 2015

Expandle list view on android with button

I have a problem with Expandle list view and button on group row. I understand, what i should do

groupButton.setFocusable(false);

or

android:focusable="false"

And also have the button manage in getView() in adapter, if i want to get it worked along with row itself. The problem is, i have custom adapter for expListView, which contains database stuff in it. And problem is - then i write getView in my custom adapter clas, the text in my textView in group just getting filled with some DB stuff like "android.database.sqlite.SQLiteCursor@21e35cf0"m but before it printed the categories from my DB. Here is the adapter itself :

                            public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
                            private final DBHelper helper;

                            public ExpandableListAdapter(DBHelper helper, Cursor cursor, Context context, int groupLayout,
                                                         int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
                                                         int[] childrenTo) {
                                super(context, cursor, groupLayout, groupFrom, groupTo,
                                        childLayout, childrenFrom, childrenTo);
                                this.helper = helper;
                            }

                            @Override
                            protected Cursor getChildrenCursor(Cursor groupCursor) {
                                long id = getGroupId(groupCursor.getPosition());
                                Log.d(TAG, "getChildrenCursor category id: " + id);
                                return helper.fetchChildren(id);
                            }


            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = View.inflate(getApplicationContext(), R.layout.list_group, null);
                    final Button addButton = (Button)convertView.findViewById(R.id.GroupButton);

                    addButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                            View popupView = layoutInflater.inflate(R.layout.popup_2, null);
                            final PopupWindow popupWindow = new PopupWindow(
                                    popupView,
                                    LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT);
                            popupWindow.setFocusable(true);

                            popupWindow.showAsDropDown(addButton, 50, -30);

                        }

                    });

                }
                TextView textView = (TextView)convertView.findViewById(R.id.lblListHeader); //The problem itself - if write some db stuff in my textview row
                textView.setText(getGroup(groupPosition).toString());
                return convertView;
            }


                        }

I calling him in main like this:

   private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final DBHelper helper = new DBHelper(this);
    final Cursor cursor = helper.fetchGroup();


    final ExpandableListView expListView = (ExpandableListView) findViewById(R.id.expandableListView);

    final ExpandableListAdapter listAdapter = new ExpandableListAdapter(helper, cursor, this,
            R.layout.list_group, R.layout.list_item,
            new String[]{"CategoryName"}, new int[]{R.id.lblListHeader},
            new String[]{"SubCategoryName"}, new int[]{R.id.lblListItem});


    expListView.setAdapter(listAdapter);

And - my DBhelperClass.

public class DBHelper extends SQLiteOpenHelper {

// Define the version and database file name
private static final String TAG = "MainActivity";
private static final String DB_NAME = "main";
private static final int DB_VERSION = 1;

private SQLiteDatabase db;

// Constructor to simplify Business logic access to the repository
public DBHelper(Context context) {

    super(context, DB_NAME, null, DB_VERSION);
    // Android will look for the database defined by DB_NAME
    // And if not found will invoke your onCreate method
    this.db = this.getWritableDatabase();

}


@Override
public void onCreate(SQLiteDatabase db) {

    // Android has created the database identified by DB_NAME
    // The new database is passed to you vai the db arg
    // Now it is up to you to create the Schema.
    // This schema creates a very simple user table, in order
    // Store user login credentials

    db.execSQL(String.format("CREATE TABLE IF NOT EXISTS Category(_id INTEGER PRIMARY KEY, CategoryName TEXT);"));
    db.execSQL(String.format("CREATE TABLE IF NOT EXISTS SubCategory(_id INTEGER PRIMARY KEY , SubCategoryName TEXT, Category_id INTEGER, Link TEXT);"));

    db.execSQL("INSERT INTO Category VALUES (1,'Anime'),(2,'Games'),(3,'News');");
    db.execSQL("INSERT INTO SubCategory VALUES(null,'Shana',1, 'http://ift.tt/1MJgnGU');");
    db.execSQL("INSERT INTO SubCategory VALUES(null,'Roll',1, null);");
    db.execSQL("INSERT INTO SubCategory VALUES(null,'Animedub',1, null);");
    db.execSQL("INSERT INTO SubCategory VALUES(null,'Something1',2, null);");
    db.execSQL("INSERT INTO SubCategory VALUES(null,'Something2',2, null);");
    db.execSQL("INSERT INTO SubCategory VALUES(null,'Yahoo',3, null);");

}

public void Insert(long id, String name) {
    db.execSQL("INSERT INTO Category VALUES (" + id + ",'" + name + "');");
}

public void InsertSub(String name, int position, String link) {
    db.execSQL("INSERT INTO SubCategory VALUES(null,'" + name + "'," + position + ", '" + link + "');");
}



@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // Later when you change the DB_VERSION
    // This code will be invoked to bring your database
    // Upto the correct specification
}
public Cursor fetchGroup() {
    String query = "SELECT * FROM Category";
    Cursor c = db.rawQuery(query, null);
    Log.d(TAG, "fetchGroup ===================================" );
    DatabaseUtils.dumpCursor(c);
    Log.d(TAG, "fetchGroup ===================================");
    return c;
}

public Cursor fetchChildren(long cat_id) {
    String query = "SELECT * FROM SubCategory WHERE Category_id = ?";
    String[] args = {
            Long.toString(cat_id)
    };
    Cursor c = db.rawQuery(query, args);
    Log.d(TAG, "fetchChildren ======================================" );
    DatabaseUtils.dumpCursor(c);
    Log.d(TAG, "fetchChildren ======================================");
    return c;
}

}

And yeah, the child rows are working perfectly - i don't know why. Thank your for help in advance!

Aucun commentaire:

Enregistrer un commentaire