简体   繁体   中英

How does the casting work from getAdapter to my generic class

I've done a lot of casting in .net - mostly down casting from generic objects to more specific ones, that I knew would work. But I've never cast to my own class until now that I'm in Android have put together some code from examples.

I can't seem to find good examples that show casting that is accompanied with the code for the classes that are being cast to and from. Basically, I'm trying to understand precisely how the casting works in this case because I want to modify the class below called dbData to include other variables and methods, but am uncertain how it will effect the casting in the onClick method below. The onClick method is in an Activity that contains a listView and a couple buttons. The buttons either add a listView item or deletea the first listView item. When deleting the item, it sends this cast in question dbData = (dbData) lstView.getAdapter().getItem(0);

dbData is my class that was derived from an example. The part I'm failing to understand is that the cast seems to successfully populate the variable id and dbData with the cast.

  1. How does it know to do that? Basic question, but can't seem to find a detailed description. if dbData was only a string, and getItem(0) returned only a string, that would seem obvious. But in this case its populating the string "dbData" and the integer "id"

  2. Could I populate additional variables or objects from the cast? This question is meant to be theoretical; meaning, if I could grab other variables, how would I know which ones I could grab, and is there any special technique to defining the variables in dbData?

  3. Can I add other variables and classes to dbData class without messing up the cast, or having those variables inadvertently populated in the cast? How would I be certain to do that?

Hopefully, the fact that I'm simply trying to understand specifically how the cast works comes through in my questions. Thank you for any help!

Here's the activity code with the cast mentioned above:

public void onClick(View view) {
    @SuppressWarnings("unchecked")

    ListView lstView = (ListView)findViewById(R.id.lstHolidays);

    ArrayAdapter<dbData> adapter = (ArrayAdapter<dbData>) lstView.getAdapter();
    dbData dbData = null;
    switch (view.getId()) {
        case R.id.add:
            String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
            int nextInt = new Random().nextInt(3);
            // save the new dbData to the database
            dbData = datasource.createDB_Row(comments[nextInt]);
            adapter.add(dbData);
            break;
        case R.id.delete:
            if (lstView.getAdapter().getCount() > 0) {
                dbData = (dbData) lstView.getAdapter().getItem(0);
                datasource.deleteDB_Row(dbData);
                adapter.remove(dbData);
            }
            break;
    }
    adapter.notifyDataSetChanged();
}

Here's the dbData class:

public class dbData {

    private long id;
    private String dbData;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getDbData() {
        return dbData;
    }

    public void setDbData(String dbData) {
        this.dbData = dbData;
    }

    // Will be used by the ArrayAdapter in the ListView
    @Override
    public String toString() {
        return dbData;
    }

}

There are two casting in your code.

You are using a generic ArrayAdapter cointaining dbData objects:

ArrayAdapter<dbData> adapter = (ArrayAdapter<dbData>) lstView.getAdapter();

Maybe somewhere in your code you are setting it, so whenever it is an ArrayAdapter (or derived class), casting above will work.

About dbData... you are not casting the adapter to dbData. You are getting your adapter (ArrayAdapter), retrieving an item and casting it to a dbData:

dbData = (dbData) lstView.getAdapter().getItem(0);

So, as your ArrayAdapter contains dbData objects, casting will also work.

This code is equivalent:

dbData = ((ArrayAdapter<dbData>) lstView.getAdapter()).getItem(0);

--- EDIT -----------------

So, yes, you can modify dbData class to add (or remove) as many properties or methods as you need and it won't break the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM