将json解析为listview(片段)的问题

时间:2013-07-12 21:33:23

标签: android android-listview

我遇到了解析JSON字符串工作的问题,它返回一个空白屏幕,并且据我所知,没有重大错误来指示原因。 我附上了我的logcat和代码。

logcat的

07-12 16:38:30.134  29441-29441/com.example.wtf                I/SurfaceTextureClient: [0x51830a20] frames:19, duration:3.969000, fps:4.786047
07-12 16:38:30.135  29441-29441/com.example.wtf                V/Provider/Setting: from settings cache , name = sound_effects_enabled value = 0
07-12 16:38:30.159  29441-29441/com.example.wtf                D/AbsListView: checkAbsListViewlLogProperty get invalid command
07-12 16:38:30.469  29441-29441/com.example.wtf                D/dalvikvm: create interp thread : stack size=32KB
07-12 16:38:30.470  29441-29441/com.example.wtf                D/dalvikvm: create new thread
07-12 16:38:30.470  29441-29441/com.example.wtf                D/dalvikvm: update thread list
07-12 16:38:30.472  29441-29920/com.example.wtf                D/dalvikvm: threadid=12: interp stack at 0x52b8c000
07-12 16:38:30.472  29441-29920/com.example.wtf                D/dalvikvm: threadid=12: created from interp
07-12 16:38:30.472  29441-29441/com.example.wtf                D/dalvikvm: start new thread
07-12 16:38:30.472  29441-29441/com.example.wtf                I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
07-12 16:38:30.485  29441-29920/com.example.wtf                D/dalvikvm: threadid=12: notify debugger
07-12 16:38:30.486  29441-29920/com.example.wtf                D/dalvikvm: threadid=12 (AsyncTask #2): calling run()
07-12 16:38:30.488  29441-29920/com.example.wtf                I/System.out: httpget:http://gdata.youtube.com/feeds/api/videos?q=random&max-results=10&v=2&alt=jsonc&format=5
07-12 16:38:30.506  29441-29441/com.example.wtf                W/MMUMapper: fail to register MVA, unsupported format(0x5)
07-12 16:38:30.519  29441-29920/com.example.wtf                I/System.out: http://gdata.youtube.com/feeds/api/videos?q=random&max-results=10&v=2&alt=jsonc&format=5
07-12 16:38:30.533  29441-29441/com.example.wtf                W/MMUMapper: fail to register MVA, unsupported format(0x5)
07-12 16:38:30.561  29441-29441/com.example.wtf                W/MMUMapper: fail to register MVA, unsupported format(0x5)
07-12 16:38:30.620  29441-29920/com.example.wtf                I/System.out: propertyValue:true
07-12 16:38:30.621  29441-29920/com.example.wtf                I/System.out: [socket][1] connection /74.125.234.131:80;LocalPort=35635(0)
07-12 16:38:30.624  29441-29920/com.example.wtf                I/System.out: [CDS]connect[/74.125.234.131:80] tm:90
07-12 16:38:30.628  29441-29920/com.example.wtf                D/Posix: [Posix_connect Debug]Process com.example.wtf :80
07-12 16:38:31.089  29441-29920/com.example.wtf                I/System.out: [socket][/192.168.1.34:35635] connected
07-12 16:38:31.109  29441-29920/com.example.wtf                I/System.out: [CDS]rx timeout:0
07-12 16:38:31.120  29441-29920/com.example.wtf                I/System.out: >doSendRequest
07-12 16:38:31.161  29441-29920/com.example.wtf                I/System.out: <doSendRequest
07-12 16:38:31.406  29441-29441/com.example.wtf                I/SurfaceTextureClient: [0x51830a20] frames:9, duration:1.268000, fps:7.095583
07-12 16:38:31.526  29441-29441/com.example.wtf                I/SurfaceTextureClient: [0x52c09558] frames:75, duration:1.007000, fps:74.471581
07-12 16:38:31.830  29441-29920/com.example.wtf                I/System.out: [CDS]close[35635]
07-12 16:38:31.831  29441-29920/com.example.wtf                I/System.out: close [socket][/0.0.0.0:35635]
07-12 16:38:31.832  29441-29920/com.example.wtf                I/System.out: close [socket][/0.0.0.0:35635]
07-12 16:38:31.849  29441-29441/com.example.wtf                D/OpenGLRenderer: Flushing caches (mode 0)
07-12 16:38:31.892  29441-29441/com.example.wtf                W/MMUMapper: invalid operation for unregister MVA with VA(0x5348c000) size(466560) f(0x5)
07-12 16:38:31.893  29441-29441/com.example.wtf                W/MMUMapper: invalid operation for unregister MVA with VA(0x534fe000) size(466560) f(0x5)
07-12 16:38:31.894  29441-29441/com.example.wtf                W/MMUMapper: invalid operation for unregister MVA with VA(0x53570000) size(466560) f(0x5)
07-12 16:38:31.926  29441-29441/com.example.wtf                V/InputMethodManager: onWindowFocus: null softInputMode=32 first=false flags=#1810100

YoutubeAdapter.java

package com.example.wtf;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class YoutubeAdapter extends BaseAdapter {

Context mContext;
LayoutInflater mInflater;
ArrayList<String> mVideo= new ArrayList<String>();
ArrayList mTitle= new ArrayList<String>();

public YoutubeAdapter(Context context,ArrayList<String> a,ArrayList title)  {
    this.mContext=context;
    mVideo=a;
    //noinspection unchecked
    mTitle = title;
    mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return mVideo.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View arg1, ViewGroup arg2) {
    ViewHolder vh;

    if(arg1==null)
    {
        arg1=mInflater.inflate(R.layout.list_row, null);
        vh= new ViewHolder();
        assert arg1 != null;
        vh.tv=(TextView)arg1.findViewById(R.id.title);
        arg1.setTag(vh);
    }
    else
    {
        vh= (ViewHolder)arg1.getTag();
    }
    vh.tv.setText((String) mTitle.get(position));
    return arg1;
}

static class ViewHolder
{
    TextView tv;
}
}

MyListFragment.java

package com.example.wtf;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class MyListFragment extends Fragment implements ActionBar.TabListener {

private Fragment mFragment;
ListView lv;
ArrayList<String> msg = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ProgressDialog pd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setContentView(R.layout.frag_mylist);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            new TheTask().execute();
        }
    }, 50);

    lv= (ListView) getActivity().findViewById(R.id.lv);
    pd = new ProgressDialog(getActivity());
    pd.setTitle("Please Wait");
    pd.setMessage("Loading list..");
}
public void getData()
{
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/videos?q=random&max-results=10&v=2&alt=jsonc&format=5");
    try
    {
        HttpResponse response = httpclient.execute(request);
        HttpEntity resEntity = response.getEntity();
        String _response= EntityUtils.toString(resEntity); // content will be consume only once

        JSONObject json = new JSONObject(_response);

        JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            String title1 = jsonObject.getString("title");
            title.add(title1);
        }
    }
    catch(Exception ListFragErr)
    {
        ListFragErr.printStackTrace();
    }

    httpclient.getConnectionManager().shutdown();
}
class TheTask extends AsyncTask<Void,Void,Void>
{

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        pd.dismiss();
        YoutubeAdapter you = new YoutubeAdapter(getActivity(),msg,title);
        lv.setAdapter(you);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        getData();
        return null;
    }

}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    mFragment = new MyListFragment();
    ft.add(android.R.id.content, mFragment);
    ft.attach(mFragment);
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(mFragment);
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

}

frag_mylist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Fragment2"
        android:textColor="#f2f2f2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView"/>

<ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/lv"
        android:listSelector="@drawable/list_selector"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/textView"/>

</RelativeLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/list_selector"
            android:orientation="horizontal"
            android:padding="5dip" >

<LinearLayout android:id="@+id/thumbnail"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="3dip"
              android:layout_alignParentLeft="true"
              android:background="@drawable/image_bg"
              android:layout_marginRight="5dip">

    <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"/>

</LinearLayout>

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="15dip"
        android:textStyle="bold"/>

<TextView
        android:id="@+id/artist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:textColor="#343434"
        android:textSize="10dip"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        />

<TextView
        android:id="@+id/duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/title"
        android:gravity="right"
        android:layout_marginRight="5dip"
        android:textSize="10dip"
        android:textColor="#10bcc9"
        android:textStyle="bold"
        android:visibility="invisible"/>

<ImageView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/arrow"
           android:layout_alignParentRight="true"
           android:layout_centerVertical="true"/>

</RelativeLayout>

谁能告诉我我做错了什么? Ps:JSON在另一个应用程序中工作正常。
修改
当我触摸列表视图所在的区域时,我会收到这些错误,并发出警告。

07-12 17:30:15.586  31273-31273/com.example.wtf  W/AbsListView: Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 2,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@419fd3d8
07-12 17:30:15.600  31273-31273/com.example.wtf  W/AbsListView: Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 2,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@419fd3d8
07-12 17:30:15.619  31273-31273/com.example.wtf  W/AbsListView: Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 2,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@419fd3d8
07-12 17:30:15.632  31273-31273/com.example.wtf  W/AbsListView: Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 2,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@419fd3d8
07-12 17:30:15.639  31273-31273/com.example.wtf  W/AbsListView: Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 2,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@419fd3d8
07-12 17:30:15.648  31273-31273/com.example.wtf  W/AbsListView: Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 1,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@419fd3d8

1 个答案:

答案 0 :(得分:0)

onPostExecute()代码:

YoutubeAdapter you = new YoutubeAdapter(getActivity(),R.layout.list_row ,msg, title);
        lv.setAdapter(you);
ListView listView = (ListView)getActivity().findViewById(R.id.lv);
listView.setAdapter(you);

YoutubeAdapter:

    public class YoutubeAdapter extends ArrayAdapter{

    Context context; 
    int layoutResourceId ; 

    private List<String> titleList ;
    private List<String> msgList ;

    private TextView title;
    private TextView msg ;


    public YoutubeAdapter(Context context, int layoutResourceId, List<String> titleList, List<String> msgList) {
        super(context, layoutResourceId, titleList, msgList);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.titleList = titleList ;
        this.msgList= msgList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View currView = convertView;
        currView = null ;
        if(currView == null) {

            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            currView = inflater.inflate(layoutResourceId, parent, false);


            title = (TextView)currView.findViewById(R.id.title);
            msg =  (TextView)currView.findViewById(R.id.artist);            

            currView.setTag(title) ;
            currView.setTag(msg) ;     

        }

        String title = titleList.get(position);
        String msg = msgList.get(position);

        title.setText(title );
        msg.setText(msg);

        //currView.setId(id) ;

        return currView ;
    }

}