带有懒惰列表的json数据

时间:2012-06-30 06:57:40

标签: java android

我正在尝试显示通过json从服务器获取的数据。如何使用lazylist来显示数据。我目前的代码不起作用,我不知道为什么。请帮我。修改当前代码会很有帮助。

这是我的代码

public class MainActivity extends Activity {

ListView list;
LazyAdapter adapter;
JSONParser jsons = new JSONParser();
UserFunctions userFunctions = new UserFunctions();
JSONArray data = null;
ArrayList<HashMap<String, String>> inboxList;
String[] tS;

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

    inboxList = new ArrayList<HashMap<String, String>>();

    List<String> t = new ArrayList<String>(); 
    List<String> im = new ArrayList<String>();

    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(this, tS, mTitles);
    list.setAdapter(adapter);

    Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(listener);

    /********************************/

    JSONObject json = userFunctions.homeData();

    Log.e("Data", json.toString());

    // Check your log cat for JSON reponse
    //Log.d("Inbox JSON: ", json.toString());

    try {
        data = json.getJSONArray("data");
        Log.d("inbox array: ", data.toString());
        // looping through All messages
        for (int i = 0; i < data.length(); i++) {
            JSONObject c = data.getJSONObject(i);

            // Storing each json item in variable
            String uid = c.getString("uid");
            String name = c.getString("name");
            String success = c.getString("success");
            String profile_img = c.getString("profile_image");
            //String date = c.getString(TAG_DATE);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put("uid", uid);
            map.put("name", name);
            map.put("success", success);
            map.put("profile_image", profile_img);

            t.add(profile_img);

            // adding HashList to ArrayList
            inboxList.add(map);
        }

        tS = new String[t.size()];
        t.toArray(tS);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    /*******************************/
}

@Override
public void onDestroy()
{
    list.setAdapter(null);
    super.onDestroy();
}

public OnClickListener listener=new OnClickListener(){
    public void onClick(View arg0) {
        adapter.imageLoader.clearCache();
        adapter.notifyDataSetChanged();
    }
};

private String[] mTitles = {
        "title 1", "title 2", "title 3", "title 4", "title 5", "title 6", "title 7", "title 8", "title 9", "title 10",
        "title 11", "title 12", "title 13", "title 14", "title 15", "title 16", "title 17", "title 18", "title 19", "title 20",
        "title 21", "title 22", "title 23", "title 24", "title 25", "title 26", "title 27", "title 28", "title 29", "title 30",
        "title 31", "title 32", "title 33", "title 34", "title 35", "title 36", "title 37", "title 38", "title 39", "title 40",
        "title 41", "title 42", "title 43", "title 44", "title 45", "title 46", "title 47", "title 48", "title 49", "title 50",
        "title 51", "title 52", "title 53", "title 54", "title 55", "title 56", "title 57", "title 58", "title 59", "title 60",
        "title 61", "title 62", "title 63", "title 64", "title 65", "title 66", "title 67", "title 68", "title 69", "title 70",
        "title 71", "title 72", "title 73", "title 74", "title 75", "title 76", "title 77", "title 78", "title 79", "title 80",
        "title 81", "title 82", "title 83", "title 84", "title 85", "title 86", "title 87", "title 88", "title 89", "title 90",
        "title 91", "title 92", "title 93", "title 94", "title 95", "title 96", "title 97", "title 98", "title 99", "title 100",
        "title 101", "title 102", "title 103", "title 104"
};

}

这是我的logcat输出

06-30 08:49:25.210: E/AndroidRuntime(31245): FATAL EXCEPTION: main
06-30 08:49:25.210: E/AndroidRuntime(31245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fedorvlasov.lazylist/com.fedorvlasov.lazylist.MainActivity}: java.lang.NullPointerException
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.os.Looper.loop(Looper.java:130)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.app.ActivityThread.main(ActivityThread.java:3691)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at java.lang.reflect.Method.invokeNative(Native Method)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at java.lang.reflect.Method.invoke(Method.java:507)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at dalvik.system.NativeStart.main(Native Method)
06-30 08:49:25.210: E/AndroidRuntime(31245): Caused by: java.lang.NullPointerException
06-30 08:49:25.210: E/AndroidRuntime(31245):    at com.fedorvlasov.lazylist.LazyAdapter.getCount(LazyAdapter.java:29)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.widget.ListView.setAdapter(ListView.java:485)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at com.fedorvlasov.lazylist.MainActivity.onCreate(MainActivity.java:41)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-30 08:49:25.210: E/AndroidRuntime(31245):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-30 08:49:25.210: E/AndroidRuntime(31245):    ... 11 more

这是我的lazyadapter

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private String[] titles;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d, String[] mTitles) {
    activity = a;
    data=d;
    titles = mTitles;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.item, null);

    TextView text=(TextView)vi.findViewById(R.id.text);
    TextView title=(TextView)vi.findViewById(R.id.textView_title);
    ImageView image=(ImageView)vi.findViewById(R.id.image);
    text.setText("item "+position);
    title.setText("Title: " + titles[position]);
    imageLoader.DisplayImage(data[position], image);
    return vi;
}
}

1 个答案:

答案 0 :(得分:2)

问题出在你的适配器设置没有数据你已设置适配器y异常发生
添加此行

adapter=new LazyAdapter(this, tS, mTitles);
list.setAdapter(adapter);

这一行之后

 t.toArray(tS);

还有一件事就是在你的活动中首先检查你在 mTitles 中的数据,我没有在你的活动中获得任何代码