简体   繁体   English

从getAdapter到通用类的转换如何工作

[英]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. 我已经在.net中进行了很多转换-大多是将通用对象向下转换为更具体的对象,而我知道这是可行的。 But I've never cast to my own class until now that I'm in Android have put together some code from examples. 但是直到现在我在Android中将示例中的一些代码组合在一起,我才开始讲授自己的课程。

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. 基本上,我想精确地了解这种情况下的转换方式,因为我想修改下面的类dbData来包含其他变量和方法,但不确定如何影响下面的onClick方法中的转换。 The onClick method is in an Activity that contains a listView and a couple buttons. onClick方法位于包含listView和几个按钮的Activity中。 The buttons either add a listView item or deletea the first listView item. 这些按钮可以添加一个listView项目或删除第一个listView项目。 When deleting the item, it sends this cast in question dbData = (dbData) lstView.getAdapter().getItem(0); 删除项目时,它将发送此有问题的演员表dbData =(dbData)lstView.getAdapter()。getItem(0);

dbData is my class that was derived from an example. dbData是我的类,它是从一个示例派生的。 The part I'm failing to understand is that the cast seems to successfully populate the variable id and dbData with the cast. 我无法理解的部分是,强制转换似乎已成功地将变量id和dbData填充了强制转换。

  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. 如果dbData只是一个字符串,而getItem(0)仅返回一个字符串,这似乎很明显。 But in this case its populating the string "dbData" and the integer "id" 但是在这种情况下,它会填充字符串“ dbData”和整数“ 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? 意思是,如果我可以获取其他变量,我将如何知道可以获取哪些变量,并且是否有任何特殊的技术来定义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? 是否可以在dbData类中添加其他变量和类而又不弄乱转换,也可以在转换中无意中填充这些变量和类? 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: 这是dbData类:

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: 您正在使用包含dbData对象的通用ArrayAdapter:

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. 也许是在代码中的某个位置进行了设置,所以无论何时它是ArrayAdapter(或派生类),上面的转换都将起作用。

About dbData... you are not casting the adapter to dbData. 关于dbData ...您没有将适配器转换为dbData。 You are getting your adapter (ArrayAdapter), retrieving an item and casting it to a dbData: 您正在获取适配器(ArrayAdapter),检索一个项目并将其强制转换为dbData:

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

So, as your ArrayAdapter contains dbData objects, casting will also work. 因此,由于您的ArrayAdapter包含dbData对象,因此转换也将起作用。

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. 因此,是的,您可以修改dbData类以添加(或删除)所需的任意数量的属性或方法,并且不会破坏代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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