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

Android 如何从JSON解析器加载数据并将其显示在自定义列表视图中?,android,listview,Android,Listview,我有包含字符串和图像URL的JSON数据,我已经在第一个活动中解析了它。在第二个活动中,如何将其显示为自定义Listview中的通讯簿我应该自定义适配器吗 如果这是解决方案,我该怎么办 我对适配器不熟悉 以下是第一个活动的代码: public class BBtest05Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundl

我有包含字符串和图像URL的JSON数据,我已经在第一个活动中解析了它。在第二个活动中,如何将其显示为自定义Listview中的通讯簿我应该自定义适配器吗

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

我对适配器不熟悉

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

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);
}
公共类BBtest05Activity扩展了活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
试一试{
executeHttpGet();
}捕获(例外e){
Log.i(“bird”,“解析器失败”);
e、 printStackTrace();
}
}
字符串asr=新字符串();
字符串Astraray[]=新字符串[2048];
public void executeHttpGet()引发异常{
试一试{
HttpClient=new DefaultHttpClient();
HttpGet请求=新建HttpGet(“http://test.mobibon.com.tw/MovieGoTest/GetMovies.js");
HttpResponse response=client.execute(请求);
字符串retSrc=EntityUtils.toString(response.getEntity(),“utf-8”);
//头上有个空间。。。
retSrc=retSrc.substring(1);
JSONObject obj=新的JSONObject(retSrc);
JSONArray movieArray=obj.getJSONArray(“电影”);
对于(int i=0;i
}

方法是改变活动方式

下面的代码是我在第二个活动中的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解析器中的数据放入此活动?

是的,您需要自定义适配器,图像加载应该是延迟的,在google上搜索列表中图像的延迟实现,您将获得足够的资料。在3.0中引入了一个新特性,即Loader,它可以为您执行延迟加载工作