2012-06-30 64 views
-1

我想显示数据,我从服务器通过json。我如何使用lazylist来显示数据。我目前的代码不起作用,我不知道为什么。请帮帮我。修改当前的代码将会很有帮助。json数据与懒惰列表

这里是我的代码

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; 
} 
} 
+0

编辑帖子与LazyAdapter类代码,因为你在LazyAdapter类中有问题 –

+0

[为什么NullPointerException?](http://samir-mangroliya.blogspot.in/p/why-nullpointerexception-occures-in.html) –

+0

@imran khan我的广告ded lazylist adapter请参阅 – 2619

回答

2

问题是在乌拉圭回合适配器设置,而无需数据u有设置适配器,以发生异常
add this lin Ë

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

此行

t.toArray(tS); 

还有一件事,就是在乌拉圭回合活性先检查其中u已经mTitles assing数据我没有得到任何的代码在乌尔活动

+0

我添加了'adapter = new LazyAdapter(this,tS,mTitles) ; list.setAdapter(adapter);'t.toArray(tS)'后面的行。但现在它不显示任何东西。其次,我还在上面的代码中添加了mTitles代码,请参阅。 – 2619

+0

并检查它是否有效,还有一件事** mTitles **数组数据我认为它是空的在你的活动中没有bcz没有添加代码来设置该数组中的数据没有数据如果你正在提取数据,它显示一个错误你喜欢NUllPointerException – Khan

+0

现在我添加了mTitles数组以及在我上面的代码中。现在可以看到我的线程中的代码? – 2619