【问题标题】:Using JSON Array for custom listview in Android在 Android 中使用 JSON 数组进行自定义列表视图
【发布时间】:2014-02-16 16:25:26
【问题描述】:

大家好,我对 Android 开发比较陌生,需要一些有关 JSON 数组的帮助,以便我可以创建自定义列表视图。 这是我所知道/拥有的

  1. 我创建了一个 Main.xml 文件(包含 2 个文本视图和一个列表视图

  2. 然后我为自定义列表视图创建了第二个 xml 文件(文件名:customlist.xml)

  3. 我成功地将数据从 mySQL 服务器获取到 JSON 数组中。

现在我不知道如何发送/使用 JSON 数组中的数据(确实转换为字符串)来创建自定义列表视图。

这里是 JSON 数组的代码:

               try {    //allocate memory for a JSON Array

                  JSONArray jArray = new JSONArray(result);

               // arrays to hold animal name and ID based on the returned length of JSON array
               final String [] apptArrayName = new String[jArray.length()];
               final String [] apptArrayID = new String[jArray.length()];

               //vars that will be grabbed from db
               String animalName;
               String ID;
               String time;
               String reason;

                     for(int i=0;i<jArray.length();i++){
                             JSONObject json_data = jArray.getJSONObject(i);

                             animalName=json_data.getString("animalName");
                             ID=json_data.getString("animalID");
                             time=json_data.getString("time");
                             reason=json_data.getString("reasonvisit");

                             //put the animal name and ID into corresponding arrays
                             //append time to show user time of appointment
                             apptArrayName[i] = animalName + "  -  " + time + " - " + reason;   
                             apptArrayID[i] = ID;             
                     }

                     //list view that is populated by data returned based on the current date
                     final ListView appointmentList = (ListView)findViewById(R.id.appointmentList);
                     final ArrayAdapter<String> arrayadpt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, apptArrayName);
                     appointmentList.setAdapter(arrayadpt);

                     //sets the onclicklistener of the selected item in arraylist (setting onclicklistener to go to next screen from the listview)
                     appointmentList.setOnItemClickListener(new OnItemClickListener(){
                         public void onItemClick(AdapterView<?> parent, View v, int position, long id){
                            //make the list view option go to the next intent
                            animalIntent.putExtra("animalName", apptArrayName[position]);
                            animalIntent.putExtra("animalID", apptArrayID[position]);
                            startActivity(animalIntent);
                        }
                    });`

感谢任何帮助

【问题讨论】:

标签: android android-listview android-arrayadapter arrays jsonobject


【解决方案1】:

创建您自己的 ArrayAdapter 子类并覆盖 getView() 方法。在该方法中,您将获得每个列表视图项的单独视图,这就是您填充视图的位置。搜索“android 自定义列表适配器”,您会发现大量有关如何执行此操作的教程。这很简单。

【讨论】:

  • 好吧,我刚刚创建了一个数组适配器,现在如何将 JSON 数组从主要活动发送到数组适配器
  • 当您创建 ArrayAdapter 时,您使用此构造函数 ArrayAdapter(Context context, int resource, List objects) 将 JSON 对象作为 List 传递。然后 ArrayAdapter 将为 List 中的每个项目调用 getView(int position, View convertView, ViewGroup parent)。您膨胀列表项布局并​​以该方法填充它。您可以使用 getItem(position) 从 Adapter 获取特定的 JSON 对象,将传入的位置传递给该 getView() 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多