RecyclerView仅显示JsonArray的第一项

时间:2019-07-07 20:29:41

标签: android arrays json kotlin

screenshot

大家好,我试图显示jsonarray中的jsonarray的项目,但是如屏幕截图所示,它仅显示第一个数组,而不是全部,在我调试应用程序后,它会从所有数组中获取所有项目,但仅显示第一个请帮助!!!!

Json

{"results": [
{
  "name": "Bulbasaur",
  "number": "001",
  "info": [
    {
      "infoOne": "Grass",
      "infoTwo": "grass"
    },
    {
      "infoOne": "Poison",
      "infoTwo": "poison"
    }
  ]
},
{
  "name": "Ivysaur",
  "number": "002",
  "info": [
    {
      "infoOne": "Grass",
      "infoTwo": "grass"
    },
    {
      "infoOne": "Poison",
      "infoTwo": "poison"
    }
  ]
},
{
  "name": "Venusaur",
  "number": "003",
  "info": [
    {
      "infoOne": "Grass",
      "infoTwo": "grass"
    },
    {
      "infoOne": "Poison",
      "infoTwo": "poison"
    }
  ]
},
{
  "name": "Charmander",
  "number": "004",
  "info": [
    {
      "infoOne": "Fire",
      "infoTwo": "fire"
    }
  ]
},
{
  "name": "Charmeleon",
  "number": "005",
  "info": [
    {
      "infoOne": "Fire",
      "infoTwo": "fire"
    }
  ]
}]}

适配器

class PokedexTypeAdapter(private val context: Context, private val pokemonDataLists: ArrayList<PokedexTypes>) :
RecyclerView.Adapter<PokedexTypeAdapter.TypeHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TypeHolder {
    return PokedexTypeAdapter.TypeHolder(
        LayoutInflater.from(parent.context).inflate(
            R.layout.pokedex_type_card,
            parent,
            false
        )
    )
}

override fun getItemCount(): Int = pokemonDataLists.size

override fun onBindViewHolder(kantoHolder: TypeHolder, position: Int) {

    kantoHolder.displayHolder(pokemonDataLists[position])
}

class TypeHolder(itemView: View) :
    RecyclerView.ViewHolder(itemView) {

    var typeText: TextView? = null
    var typeImage: ImageView? = null;


    fun displayHolder(pokemonDataList: PokedexTypes) {

        typeText = itemView.findViewById(R.id.pokedex_info_two)
        typeImage = itemView.findViewById(R.id.pokedex_info_one)

        typeText?.text = pokemonDataList.typeText
        Picasso.get().load("http://www.gstyles.net/pokemonApp/pokemonDetails/${pokemonDataList.typeImage}.png").into(typeImage)
    }
}
}

AsyncTask

class PokedexTypeTask(
private val context: Context,
private val recyclerView: RecyclerView
) :
AsyncTask<String, Int, String>() {

private val httpRequest = HttpRequest()

override fun doInBackground(vararg strings: String): String? {
    return httpRequest.Request(strings[0])
}

override fun onPostExecute(s: String) {

    val mainObject: JSONObject
    val array: JSONArray
    var typeArray: JSONArray? = null
    val index = 2

    val pokemonTypeArrayList = ArrayList<PokedexTypes>()

    try {
        mainObject = JSONObject(s)
        array = mainObject.getJSONArray("results")

        for (i in 0 until array.length()) {
            val typeObject = array.getJSONObject(i)

            typeArray = typeObject.getJSONArray("info")

            for (t in 0 until typeArray.length()) {
                val infoObject = typeArray.getJSONObject(t)
                val infoOne = infoObject.getString("infoOne")
                val infoTwo = infoObject.getString("infoTwo")
                pokemonTypeArrayList.add(PokedexTypes(infoOne, infoTwo))
            }
        }

    } catch (e: JSONException) {
        e.printStackTrace()
    }

    when {
        typeArray!!.length() != 2 -> 1
    }

    recyclerView.layoutManager = GridLayoutManager(context, index)
    recyclerView.setHasFixedSize(true)
    recyclerView.adapter = PokedexTypeAdapter(context, pokemonTypeArrayList)
}
}

编辑: pokedex_card_view.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="0dp"
    app:cardBackgroundColor="@color/colorPrimary">
<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp">

    <TextView
            android:id="@+id/pokedex_number"
            android:layout_width="wrap_content"
            android:layout_height="18dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/pokedex_name"
            app:layout_constraintBottom_toTopOf="@id/pokedex_image"
            android:text="@string/pokemon_number"
            android:textSize="12sp"
            android:textColor="@color/colorAccent"
            android:layout_marginEnd="4dp"/>

    <TextView
            android:id="@+id/pokedex_name"
            android:layout_width="wrap_content"
            android:layout_height="18dp"
            app:layout_constraintTop_toTopOf="@id/pokedex_number"
            app:layout_constraintStart_toEndOf="@id/pokedex_number"
            app:layout_constraintBottom_toBottomOf="@id/pokedex_number"
            android:text="@string/pokemon_name"
            android:textSize="12sp"
            android:textColor="@color/colorAccent"
            android:layout_marginStart="4dp"/>

    <ImageView
            android:id="@+id/pokedex_image"
            android:layout_width="64dp"
            android:layout_height="64dp"
            app:layout_constraintTop_toBottomOf="@id/pokedex_number"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@id/pokedex_type"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:src="@mipmap/ic_launcher_round"
            android:contentDescription="@string/image"
            android:background="@color/colorAccent"/>

    <android.support.v7.widget.RecyclerView
            android:id="@+id/pokedex_type"
            android:layout_width="0dp"
            android:layout_height="40dp"
            app:layout_constraintTop_toBottomOf="@id/pokedex_image"
            app:layout_constraintStart_toStartOf="@id/pokedex_image"
            app:layout_constraintEnd_toEndOf="@id/pokedex_image"
            app:layout_constraintBottom_toBottomOf="parent"
            android:background="@color/colorAccent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

pokedex_type_card.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
            android:id="@+id/pokedex_info_one"
            android:layout_width="16dp"
            android:layout_height="16dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@id/pokedex_info_two"
            android:src="@drawable/fire"
            android:contentDescription="@string/image"/>
    <TextView
            android:id="@+id/pokedex_info_two"
            android:layout_width="match_parent"
            android:layout_height="16dp"
            app:layout_constraintTop_toBottomOf="@id/pokedex_info_one"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:text="@string/info_type_one"
            android:textSize="10sp"
            android:textAlignment="center"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

PokedexClass

class KantoDex : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view: View = inflater.inflate(R.layout.fragment_dex, container, false)

    val mainJsonString = "http://www.gstyles.net/pokemonApp/pokedexJson/pokemonKantoJson.json"

    val dexRecycler: RecyclerView = view.findViewById(R.id.dex_list)

    PokedexTask(context!!, dexRecycler).execute(mainJsonString)

    val dexTitle = view.findViewById<TextView>(R.id.dex_title)
    val dexImage = view.findViewById<ImageView>(R.id.dex_img)

    dexTitle.text = context?.resources?.getString(R.string.kanto_items)
    dexImage.setImageResource(R.drawable.kanto)

    return view
 }
 }

0 个答案:

没有答案