如何从JSON解析器加载数据并将其显示在我的自定义列表视图中?

时间:2012-01-09 04:51:37

标签: android listview

我有JSON数据,其中包含字符串和图像URL,我已在第一个Activity中解析它。如何在第二个Activity中自定义Listview中的地址簿中显示它?我应该自定义适配器吗?

如果这是解决方案,我该怎么办?

我不熟悉Adapter。

以下是第一个活动的代码:

public class BBtest05Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        executeHttpGet();
    } catch (Exception e) {
        Log.i("bird","parser failure");
        e.printStackTrace();
    }
    }

    String aStr = new String();
    String aStrArray[] = new String[2048];
    public void executeHttpGet() throws Exception {
        try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://test.mobibon.com.tw/MovieGoTest/GetMovies.js");
        HttpResponse response = client.execute(request); 
         String retSrc = EntityUtils.toString(response.getEntity(),"utf-8");
        //there's a space in the head...
         retSrc = retSrc.substring(1);
           JSONObject obj = new JSONObject(retSrc);
           JSONArray movieArray = obj.getJSONArray("movies");
           for(int i = 0;i < movieArray.length(); i++)
           {
           JSONObject movie_data = movieArray.getJSONObject(i);
           aStr = aStr+","+movie_data.getString("cTitle");

           }
           } finally {
               aStrArray = aStr.split(",");
               ListView list = (ListView) findViewById(R.id.linearLayout1);
               list.setAdapter(new ArrayAdapter<String>(this,
               android.R.layout.simple_list_item_1, aStrArray));
               list.setTextFilterEnabled(true);
           }
}
public void aMethod(View theButton) {   
    Intent i = new Intent(this,nowamovie.class);    
    startActivity(i);
}

}

aMethod正在改变活动方法......

以下代码是我在第二个Activity中的XML文件......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" android:background="@drawable/pic_background">
<LinearLayout android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/linearLayout1" >
<ImageView android:layout_height="wrap_content"
android:id="@+id/imageView1" 
android:layout_width="wrap_content" 
android:src="@drawable/ic_launcher"></ImageView>
<TextView android:text="TextView" 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip" 
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/linearLayout1" 
android:orientation="horizontal">
<TextView android:id="@+id/textView2" 
android:text="TextView" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
<TextView android:text="TextView" android:id="@+id/textView3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textAppearance="?android:attr/textAppearanceSmall" 
android:layout_marginLeft="6dip">
</TextView>
</LinearLayout>

如何将来自JSON解析器的数据放入此活动中?

1 个答案:

答案 0 :(得分:0)

是的,您需要自定义您的适配器并且图片加载应该是懒惰的,在谷歌搜索列表中的图像的懒惰实现,您将获得足够的材料。在3.0中,引入了一个新功能,即Loader,它为您执行这种延迟加载工作。