如何将 JSONArray 转换为 ListView?

新手上路,请多包涵

我有一个代码执行以下操作 -

  1. 通过 HttpClient 连接到 Web 服务到 PHP 文件
  2. 从 SQL 查询返回结果
  3. 返回格式是一个 jArray(一个 JSONArray)
 for(int i=0; i < jArray.length() ; i++) {
    json_data = jArray.getJSONObject(i);
    int id=json_data.getInt("id");
    String name=json_data.getString("name");
    Log.d(name,"Output");
}

当我查看 LogCat 时,我看到查询的所有“名称”,每条记录都被打印出来。我只需要将这些结果插入到 ListView 中。我怎样才能做到这一点?

PS - 我没有单独的 ArrayAdapter 类。会不会是这个原因?

原文由 Beetle 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 387
2 个回答

如果您只想显示一个 textView 列表,则不需要覆盖任何内容,只需将所有项目添加到 arrayList 中并使用 arrayAdapter。

在名为 android:list 的 xml 中放置一个列表视图,然后使用要使用的 textView 创建 arrayAdapter。

之后你所要做的就是调用 setListAdapter(mArrayAdapter) 它应该填充你的列表。

 ArrayList<String> items = new ArrayList<String>();
for(int i=0; i < jArray.length() ; i++) {
    json_data = jArray.getJSONObject(i);
    int id=json_data.getInt("id");
    String name=json_data.getString("name");
    items.add(name);
    Log.d(name,"Output");
}

ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,
           android.R.layout.simple_expandable_list_item_1, items));
setListAdapter(mArrayAdapter)

希望这可以帮助!

原文由 ByteMe 发布,翻译遵循 CC BY-SA 3.0 许可协议

我不喜欢重新映射数据,因为我已经将它们放在 JSON 数组中,所以这是我的代码……它对我来说很简单……希望它有帮助

package ...;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DataListFragment extends ListFragment{
    JSONArray data;

    public DataListFragment(){

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i("mycode", "DataListFragment onActivityCreated");
        data=((MainActivity)this.getActivity()).data;
        Log.i("mycode", "data length "+data.toString());
        setEmptyText("No Data Here");
        final JSONArrayAdapter adapter = new JSONArrayAdapter(this.getActivity(),data);
        setListAdapter(adapter);

        // Start out with a progress indicator.
        //setListShown(false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Insert desired behavior here.
        Log.i("mycode", "Item clicked: " + id);
    }

    private class JSONArrayAdapter extends BaseAdapter {
        JSONArray data;
        Context context;

        public JSONArrayAdapter(Context context,JSONArray data) {
            super();
            this.context=context;
            this.data=data;
        }

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

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            try {
                return data.getJSONObject(arg0);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

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

        @Override
        public boolean hasStableIds(){
            return true;
        }

        @Override
        public boolean isEmpty(){
            return data==null || data.length()==0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.single_item, parent, false);
            TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine);
            TextView textView2 = (TextView) rowView.findViewById(R.id.secondLine);
            //ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
            try {
                JSONObject jo = (JSONObject) data.get(position);
                textView1.setText(jo.getString("category_title"));
                textView2.setText(jo.getString("description"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return rowView;
        }

    }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="icon"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:textSize="16sp" />

</RelativeLayout>

原文由 Luca Rocchi 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题