2017-09-23 67 views
0

我想知道是否有人可以帮助我用Intent修复这个错误?在Kotlin的两个活动之间传递一个对象实例

我有2个活动之间进行连接:第一活性“MainActivity.kt”, 第二对列表视图“Main2Activity.kt” 为网格视图和我有2对象实例(适配器1,适配器2)从“Main2Activity .kt”我想通过特定对象实例(适配器1)或(适配器2)当点击特定图像中的‘MainActivity.kt’

注:我使用BaseAdapter

这里是一个MainActivity。 kt

class MainActivity : AppCompatActivity() { 

var adapter:FoodAdapter?=null 
var listOfFoods =ArrayList<Food>() 
override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(layout.activity_main) 


    // load foods 
    listOfFoods.add(Food("Coffee"," Coffee preparation is", a)) 
    listOfFoods.add(Food("Coffee"," Coffee preparation is", b)) 
    listOfFoods.add(Food("Coffee"," Coffee preparation is", c)) 
    listOfFoods.add(Food("Coffee"," Coffee preparation is", d)) 

    adapter= FoodAdapter(this,listOfFoods) 

    gvListFood.adapter =adapter 

} 


class FoodAdapter: BaseAdapter { 
    var listOfFood= ArrayList<Food>() 
    var context: Context?=null 
    constructor(context:Context,listOfFood:ArrayList<Food>):super(){ 
     this.context=context 
     this.listOfFood=listOfFood 
    } 
    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View? { 
     val food = this.listOfFood[p0] 
     var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater 
     var foodView= inflator.inflate(layout.food_ticket,null) 
     foodView.ivFoodImage.setImageResource(food.image!!) 
     foodView.ivFoodImage.setOnClickListener { 


      if (foodView.ivFoodImage.context!!.equals(a)) { 
       val intent = Intent(context, Main2Activity::class.java) 
       intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_2.ordinal) 
        intent.putExtra("name", food.name!!) 
        intent.putExtra("des", food.des!!) 
        intent.putExtra("image", food.image!!) 

        context!!.startActivity(intent) 

      } 
      else if (foodView.ivFoodImage.context!!.equals(b)) { 
       val intent = Intent(context, Main2Activity::class.java) 
       intent.putExtra(Main2Activity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_2.ordinal) 
        intent.putExtra("name", food.name!!) 
        intent.putExtra("des", food.des!!) 
        intent.putExtra("image", food.image!!) 
        context!!.startActivity(intent) 

      } 
     } 
     return foodView 
    } 

    override fun getItem(p0: Int): Any { 
     return listOfFood[p0] 
    } 

    override fun getItemId(p0: Int): Long { 
     return p0.toLong() 
    } 

    override fun getCount(): Int { 

     return listOfFood.size 
    } 

} 
} 

这里是一个Main2Activity.kt

class Main2Activity : AppCompatActivity() { 
companion object { 
    val EXTRA_ADAPTER_MODE = "extra_adapter_mode" 
} 
var adapter1: FoodAdapter? = null 
var adapter2: FoodAdapter2? = null 
var listOfFoods2 = ArrayList<Food>() 
var listOfFoods3 = ArrayList<Food>() 
override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_main2) 


    val bundle = intent?.getIntExtra(EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_1.ordinal) 
    val name = bundle?.let { AdapterType.fromOrdinal(it) } 
    val des = bundle?.let { AdapterType.fromOrdinal(it) } 
    val image = bundle?.let { AdapterType.fromOrdinal(it) } 

    //val bundle = intent.extras 
    //val name = bundle.getString("name") 
    //val des = bundle.getString("des") 
    //val image = bundle.getInt("image") 



    /* 
    ivFoodImage2?.let { 
     it.setImageResource(image) 
    } 

    tvName2?.let { 
     it.text = name 
    } 

    tvDes2?.let { 
     it.text = des 
    } 
    */ 





    // load foods2 
    listOfFoods2.add(Food("Coffee", " Coffee1 preparation is", R.drawable.a)) 
    listOfFoods2.add(Food("Coffee", " Coffee2 preparation is", R.drawable.b)) 

     //listOfFoods2.add(Food("Coffee"," Coffee preparation is",R.drawable.c)) 
     //listOfFoods2.add(Food("Coffee"," Coffee preparation is",R.drawable.d)) 


    // load foods3 


    //listOfFoods3.add(Food("Coffee"," Coffee preparation is",R.drawable.a)) 
    //listOfFoods3.add(Food("Coffee"," Coffee preparation is",R.drawable.b)) 
    listOfFoods3.add(Food("Coffee", " Coffee3 preparation is", R.drawable.c)) 
    listOfFoods3.add(Food("Coffee", " Coffee4 preparation is", R.drawable.d)) 



    if(adapter1 ==lvFoods2.adapter) { 
     adapter1 = FoodAdapter(listOfFoods2, this) 
     lvFoods2.adapter = adapter1 
    } else if (adapter2==lvFoods2.adapter) { 
     adapter2 = FoodAdapter2(listOfFoods3, this) 
     lvFoods2.adapter = adapter2 
    } 

    } 


class FoodAdapter : BaseAdapter { 
    var context: Context? = null 
    var listOfFoodsLocal2 = ArrayList<Food>() 

    constructor(listOfFoods2: ArrayList<Food>, context: Context) : super() { 
     this.listOfFoodsLocal2 = listOfFoods2 
     this.context = context 
    } 

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View { 
     val food = this.listOfFoodsLocal2[p0] 
     var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater 
     val foodView = inflator.inflate(R.layout.food_ticket2, null) 


       foodView.ivFoodImage2?.let { 
        it.setImageResource(food.image!!) 
       } 
     foodView.tvName2?.let { 

      it.text = food.name!! 
     } 
     foodView.tvDes2?.let { 
      it.text = food.des!! 
     } 

     foodView.ivFoodImage2.setOnClickListener { 

      //move to next 
      val intent = Intent(context, FoodDetails::class.java) 
      intent.putExtra("name", food.name!!) 
      intent.putExtra("des", food.des!!) 
      intent.putExtra("image", food.image!!) 

      context!!.startActivity(intent) 

     } 
     return foodView 

    } 


    override fun getItem(p0: Int): Any { 
     return listOfFoodsLocal2[p0] 
    } 

    override fun getItemId(p0: Int): Long { 
     return p0.toLong() 
    } 

    override fun getCount(): Int { 
     return listOfFoodsLocal2.size 
    } 

} 

class FoodAdapter2 : BaseAdapter { 
    var context: Context? = null 
    var listOfFoodsLocal3 = ArrayList<Food>() 

    constructor(listOfFoods3: ArrayList<Food>, context: Context) : super() { 
     this.listOfFoodsLocal3 = listOfFoods3 
     this.context = context 
    } 

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View { 
     val food = this.listOfFoodsLocal3[p0] 
     var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater 
     val foodView = inflator.inflate(R.layout.food_ticket2, null) 


     foodView.ivFoodImage2?.let { 
      it.setImageResource(food.image!!) 
     } 
     foodView.tvName2?.let { 
      it.text = food.name!! 
     } 
     foodView.tvDes2?.let { 
      it.text = food.des!! 
     } 



     foodView.ivFoodImage2.setOnClickListener { 

      //move to next 
      val intent = Intent(context, FoodDetails::class.java) 
      intent.putExtra("name", food.name!!) 
      intent.putExtra("des", food.des!!) 
      intent.putExtra("image", food.image!!) 

      context!!.startActivity(intent) 

     } 
     return foodView 

    } 


    override fun getItem(p0: Int): Any { 
     return listOfFoodsLocal3[p0] 
    } 

    override fun getItemId(p0: Int): Long { 
     return p0.toLong() 
    } 

    override fun getCount(): Int { 
     return listOfFoodsLocal3.size 
    } 

} 
} 

这是枚举类

enum class AdapterType { 
ADAPTER_TYPE_1, 
ADAPTER_TYPE_2, 
ADAPTER_TYPE_3; 


companion object { 
    fun fromOrdinal(name:String,des:String,image: Int): AdapterType? { 
     return AdapterType.values().firstOrNull { it.ordinal == image} 



    } 
} 
} 
+0

,我不认为这个代码将编译,不是吗?在你的意图中,你指定了你想要去的活动。您无法指定适配器。把另一个布尔值加入到intent中,然后根据布尔值来决定使用什么适配器... –

+0

谢谢@Nino Handler,是的,我的代码不能编译 – Marzadmz

+0

boolean接受true或false,其实在Activity 2中我想添加超过2个适配器不仅2它的50个适配器(每个适配器有不同的ArrayList),所以我怎么做,没有指定适配器 – Marzadmz

回答

2

创建IntDef像:

@Retention(SOURCE) 
@IntDef({ADAPTER_MODE_1, ADAPTER_MODE_2, ADAPTER_MODE_3}) 
public @interface AdapterMode {} 
public static final int ADAPTER_MODE_1 = 0; 
public static final int ADAPTER_MODE_2 = 1; 
public static final int ADAPTER_MODE_3 = 2; 
//... 

和INT添加到包。 对于科特林你必须使用一个companion object定义像

class FirstActivity { 
    companion object { 
     val ADAPTER_MODE1 = 0 
     val ADAPTER_MODE2 = 1 
     val ADAPTER_MODE3 = 2 
     //... 
    } 
} 

变量所以,你可以通过无限适配器的情况下,以你的第二个活动

编辑: 科特林doesn't support @IntDef所以你应该使用枚举为:

enum class AdapterType { 
    ADAPTER_TYPE_1, 
    ADAPTER_TYPE_2, 
    ADAPTER_TYPE_3; 

    companion object { 
     fun fromOrdinal(ordinal: Int): AdapterType? { 
      return AdapterType.values().firstOrNull { it.ordinal == ordinal } 
     } 
    } 
} 

然后使用它像:

class FirstActivity : AppCompatActivity() { 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     setContentView(R.layout.activity_main) 

     val intent = Intent(this, SecondActivity::class.java) 
     intent.putExtra(SecondActivity.EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_2.ordinal) 
    } 
} 

和检索它像:

class SecondActivity : AppCompatActivity() { 
    companion object { 
     val EXTRA_ADAPTER_MODE = "extra_adapter_mode" 
    } 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     //... 
     val adapterTypeOrdinal = intent?.getIntExtra(EXTRA_ADAPTER_MODE, AdapterType.ADAPTER_TYPE_1.ordinal) 
     val adapterType = adapterTypeOrdinal?.let { AdapterType.fromOrdinal(it) } 

     // do something with it 
    } 
} 
+0

感谢@Nino Handler的回答,实际上我创建了一个“IntDef”并且我的问题代码已更新,在您的第二部分回答:请你能帮助我,因为我不知道如何将int添加到包中,以及在第二个活动中添加伴随对象代码的位置,请你检查我的第二个活动 – Marzadmz

+0

我更新了我的答案,因为Kotlin没有支持@IntDef,我不知道。所以从编辑部分阅读它。并更新您的问题 –

+0

谢谢@Nino Handler,我做到了,但这是我第一次使用枚举,请你可以检查我的问题代码更新,因为它还没有完成 – Marzadmz

1

你不能传递活动之间的适配器。至少以一种干净的方式,由Google推荐。适配器既不实现Parcelable也不实现Serializable接口。正如Nino Handler指出的一般规则 - 使用Intent发送数据(不是适配器本身),它应该用于创建适配器。

相关问题