Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在listview中显示JSON对象?_Android_Json_Listview - Fatal编程技术网

Android 如何在listview中显示JSON对象?

Android 如何在listview中显示JSON对象?,android,json,listview,Android,Json,Listview,主类 使用volley库从URL获取JSON数据 public class MyActivity extends Activity { ListView listView; List<Details> rowItems; RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance

主类 使用volley库从URL获取JSON数据

   public class MyActivity extends Activity {   

ListView listView;
List<Details> rowItems;

RequestQueue requestQueue;

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.details2);
    url = getIntent().getStringExtra("key");
    if(savedInstanceState!=null){
        Log.d("STATE",savedInstanceState.toString());
    }


    requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest jor = new  JsonObjectRequest(url, null,
            new Response.Listener<JSONObject>()  {
                @Override
                public void onResponse(JSONObject response) {

                    try {


                        JSONArray ja = response.getJSONArray("results");

                        ArrayList<Details> myModelList = new     ArrayList<Details>();
                        Details mymodel = null;
                        for (int i = 0; i < ja.length(); i++) {

                            JSONObject jsonObject = ja.getJSONObject(i);
                            mymodel = new Details();
                            mymodel.id = Integer.parseInt(jsonObject.optString("id").toString());
                            mymodel.url = jsonObject.getString("resLink");
                            mymodel.resType = jsonObject.getString("resType");
                            mymodel.name = jsonObject.getString("resName");

                            myModelList.add(mymodel);
                              setData();
                        }


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

            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley","Error");

                }
            }
    );
    requestQueue.add(jor);
}

private void setData() {

    listView = (ListView) findViewById(R.id.list);
    CustomListViewAdapter adapter = new CustomListViewAdapter(this,
            R.layout.list_item, rowItems);
    listView.setAdapter(adapter);
    //listView.setOnItemClickListener(this);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
*想在列表视图中显示我收到的数据吗*

我的XML

    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

list_item.xml

       <LinearLayout       xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/icon"
    android:paddingBottom="10dp"
    android:textColor="#CC0033"
    android:textSize="16dp" />

<TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_toRightOf="@+id/icon"
    android:paddingLeft="10dp"
    android:textColor="#3399FF"
    android:textSize="14dp" />


在适配器类getView()中使用以下行

Details行项目=(细节)getItem(位置)

而不是


Details rowItem=getItem(位置)

在适配器类getView()中使用以下行

Details行项目=(细节)getItem(位置)

而不是


Details rowItem=getItem(位置)

您做的一切都是正确的,但没有传递包含数据的myModelList,也没有传递空的rowItems列表。获取所有数据并创建myModelList后,调用

             setData(myModelList);
并按如下方式更改setData方法

private void setData(List<Details> mList) {
      listView = (ListView) findViewById(R.id.list);
      CustomListViewAdapter adapter = new CustomListViewAdapter(this,
        R.layout.list_item, mList);
      listView.setAdapter(adapter);
}
private void setData(列表mList){
listView=(listView)findViewById(R.id.list);
CustomListViewAdapter=新的CustomListViewAdapter(此,
R.layout.list_项目,mList);
setAdapter(适配器);
}

您做的一切都是正确的,但没有传递包含数据的myModelList,也没有传递空的rowItems列表。获取所有数据并创建myModelList后,调用

             setData(myModelList);
并按如下方式更改setData方法

private void setData(List<Details> mList) {
      listView = (ListView) findViewById(R.id.list);
      CustomListViewAdapter adapter = new CustomListViewAdapter(this,
        R.layout.list_item, mList);
      listView.setAdapter(adapter);
}
private void setData(列表mList){
listView=(listView)findViewById(R.id.list);
CustomListViewAdapter=新的CustomListViewAdapter(此,
R.layout.list_项目,mList);
setAdapter(适配器);
}

由于无法获取设置数据的列表,请更新设置数据方法

 private void setData(List<Details> list) {
          listView = (ListView) findViewById(R.id.list);
          CustomListViewAdapter adapter = new CustomListViewAdapter(this,
            R.layout.list_item, list);
          listView.setAdapter(adapter);
    }
private void setData(列表){
listView=(listView)findViewById(R.id.list);
CustomListViewAdapter=新的CustomListViewAdapter(此,
R.layout.list(项目、列表);
setAdapter(适配器);
}

同时更改
setData()
设置数据(myModelList)

由于无法获取设置数据的列表,请更新设置数据方法

 private void setData(List<Details> list) {
          listView = (ListView) findViewById(R.id.list);
          CustomListViewAdapter adapter = new CustomListViewAdapter(this,
            R.layout.list_item, list);
          listView.setAdapter(adapter);
    }
private void setData(列表){
listView=(listView)findViewById(R.id.list);
CustomListViewAdapter=新的CustomListViewAdapter(此,
R.layout.list(项目、列表);
setAdapter(适配器);
}

同时更改
setData()
设置数据(myModelList)

在适配器中,您正在使用列表行项目

所以onResponse方法的代码应该如下所示

 JSONArray ja = response.getJSONArray("results");
 rowItems = new     ArrayList<Details>();
 Details mymodel = null;
 for (int i = 0; i < ja.length(); i++) {
    JSONObject jsonObject = ja.getJSONObject(i);
    mymodel = new Details();
    mymodel.id = Integer.parseInt(jsonObject.optString("id").toString());
    mymodel.url = jsonObject.getString("resLink");
    mymodel.resType = jsonObject.getString("resType");
    mymodel.name = jsonObject.getString("resName");
    rowItems.add(mymodel);
    }
    setData();
JSONArray ja=response.getJSONArray(“结果”);
rowItems=新的ArrayList();
详细信息mymodel=null;
对于(int i=0;i
为了简化json解析,您可以使用Gson库,如下所述

在适配器中,您正在使用列表行项目

所以onResponse方法的代码应该如下所示

 JSONArray ja = response.getJSONArray("results");
 rowItems = new     ArrayList<Details>();
 Details mymodel = null;
 for (int i = 0; i < ja.length(); i++) {
    JSONObject jsonObject = ja.getJSONObject(i);
    mymodel = new Details();
    mymodel.id = Integer.parseInt(jsonObject.optString("id").toString());
    mymodel.url = jsonObject.getString("resLink");
    mymodel.resType = jsonObject.getString("resType");
    mymodel.name = jsonObject.getString("resName");
    rowItems.add(mymodel);
    }
    setData();
JSONArray ja=response.getJSONArray(“结果”);
rowItems=新的ArrayList();
详细信息mymodel=null;
对于(int i=0;i
为了简化json解析,您可以使用Gson库,如下所述

我在int java.util.List.size()上将myModelList声明为全局并尝试将其发送到适配器类上时遇到空点异常。
CustomListViewAdapter=new CustomListViewAdapter(this,R.layout.List\u项,myModelList);setAdapter(适配器)我在int java.util.List.size()上将myModelList声明为全局并尝试将其发送到适配器类上时遇到空点异常。
CustomListViewAdapter=new CustomListViewAdapter(this,R.layout.List_项,myModelList);setAdapter(适配器)谢谢..非常感谢Erginus和@Rajesh我不能对你们两人都投4票,所以只能使用fifs(先到先得的服务)。@Shwetabshing但如果有帮助,你可以直接投票。)谢谢..非常感谢Erginus和@Rajesh我不能对你们两人都投4票,所以只能使用fifs(先到先得的服务)。@Shwetabshing但如果有帮助,你可以直接投票。:)谢谢:)随时都可以!很乐意帮忙,随时都可以!很乐意帮忙。