2016-03-08 90 views
0

主类 使用排球库从URL中获取JSON数据。如何在listview中显示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); 
} 

我CustomListViewAdapter克拉斯低于

public class CustomListViewAdapter extends ArrayAdapter<Details> { 

Context context; 

public CustomListViewAdapter(Context context, int resourceId, 
          List<Details> items) { 
    super(context, resourceId, items); 
    this.context = context; 
} 

/*private view holder class*/ 
private class ViewHolder { 

    TextView txtTitle; 
    TextView txtDesc; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    Details rowItem = getItem(position); 

    LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_item, null); 
     holder = new ViewHolder(); 
     holder.txtDesc = (TextView) convertView.findViewById(R.id.desc); 
     holder.txtTitle = (TextView) convertView.findViewById(R.id.title); 

     convertView.setTag(holder); 
    } else 
     holder = (ViewHolder) convertView.getTag(); 

    holder.txtDesc.setText(rowItem.getResType()); 
    holder.txtTitle.setText(rowItem.getName()); 

    return convertView; 
} 

我的详细信息类 公共类详细扩展结果{

String name; 
String resType; 
String url; 
int id; 
public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getResType() { 
    return resType; 
} 
public void setResType(String resType) { 
    this.resType = resType; 
} 
public String getUrl() { 
    return url; 
} 
public void setUrl(String url) { 
    this.url = url; 
} 

*希望综艺节目我在列表视图中接收到的数据*

我的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" /> 

回答

1

您正在做的一切正确,但不传递包含数据的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); 
} 
+0

随时!乐意效劳。 –

1

使用后在适配器类getView()线

Details rowItem = (Details) getItem(position);

,而不是

Details rowItem = getItem(position);

+0

我越来越对INT的Java零点异常.util.List.size() –

+0

将** myModelList **声明为全局变量,并尝试将其发送到适配器类。 listView.setAdapter(adapter);' – malli

1

更新您的使用setData梅索德。

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); 
    } 

也改变setData();setData(myModelList);

+0

谢谢你..很烂Erginus和@Rajesh我不能投票4你们两个,所以只是使用fifs(先来先服务) –

+0

@ShwetabhSingh但如果它有帮助,你可以upvote。:)谢谢:) –

1

在您使用列表rowItems适配器;

所以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(); 

为了使JSON解析容易,你可以使用GSON库作为这里解释 http://www.mysamplecode.com/2013/07/android-json-stream-data-parsing.html