【问题标题】:How to load data to Custom Listview from JSON array如何从 JSON 数组将数据加载到自定义 Listview
【发布时间】:2012-06-04 10:07:59
【问题描述】:

我已经从这样的 JSON 数组将数据加载到普通列表视图中

 JSONArray jArray = new JSONArray(result);       
 final String[] array_spinner = new String[jArray.length()];
 for(int i=0;i<jArray.length();i++){
     JSONObject json_data = jArray.getJSONObject(i);
     String jj=json_data.getString("f_name");
     array_spinner[i] = jj;
 }
 ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,  android.R.layout.simple_list_item_1,array_spinner); adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
 //adapter.setDropDownViewResource(R.layout.spinner_layout);
 list.setAdapter(adapter);

我想将数据加载到自定义列表视图。 我的自定义列表视图的 XML 文件是这个 接收器.xml

如何将数据从 JSON 加载到自定义列表视图?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:
    • 创建一个扩展 BaseAdpter 或 ArrayAdpter 的自定义适配器,并在构造函数中传递数组或 ArrayList
    • 在(行的)布局中创建视图
    • 在自定义Adapter的getView函数中填充这个xml并设置数据

    http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/

    Populate Listview from JSON

    为了你,我混合了两者让你明白......

    活动 XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lstText"
        />
    </LinearLayout>
    

    列表行 XML(在布局 row.xml 中)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    
            <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtAlertText" />
    
        </LinearLayout>
    </LinearLayout>
    

    你的适配器类

    class JSONAdapter extends BaseAdapter implements ListAdapter {
    
        private final Activity activity;
        private final JSONArray jsonArray;
        private JSONAdapter (Activity activity, JSONArray jsonArray) {
            assert activity != null;
            assert jsonArray != null;
    
            this.jsonArray = jsonArray;
            this.activity = activity;
        }
    
    
        @Override public int getCount() {
            if(null==jsonArray) 
             return 0;
            else
            return jsonArray.length();
        }
    
        @Override public JSONObject getItem(int position) {
             if(null==jsonArray) return null;
             else
               return jsonArray.optJSONObject(position);
        }
    
        @Override public long getItemId(int position) {
            JSONObject jsonObject = getItem(position);
    
            return jsonObject.optLong("id");
        }
    
        @Override public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = activity.getLayoutInflater().inflate(R.layout.row, null);
    
    
    
            TextView text =(TextView)convertView.findViewById(R.id.txtAlertText);
    
                        JSONObject json_data = getItem(position);  
                        if(null!=json_data ){
                        String jj=json_data.getString("f_name");
                        text.setText(jj); 
                       }
    
             return convertView;
        }
    }
    

    你的活动

    public class main extends Activity {
        /** Called when the activity is first created. */
    
        ListView lstTest;
        //Array Adapter that will hold our ArrayList and display the items on the ListView
        JSONAdapter jSONAdapter ;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //Initialize ListView
            lstTest= (ListView)findViewById(R.id.lstText);
    
    
            jSONAdapter = new JSONAdapter (main.this,jArray);//jArray is your json array 
    
            //Set the above adapter as the adapter of choice for our list
            lstTest.setAdapter(jSONAdapter );
    
    
    }
    

    【讨论】:

    • 我不明白你没有一个简单的例子吗?
    • 但是它给出了错误你能按照我在问题中希望的那样编辑我的代码吗?
    • jSONAdapter = new JSONAdapter (main.this, R.layout.listitems,);这里传递了 3 个参数,但在 JSONADapter 类的构造函数中,这里有 2 个参数: private VenuesAdapter(Activity activity, JSONArray jsonArray) {
    • 场地适配器?那个构造函数是什么?如果我给你我的完整代码,你能修复它们吗?
    • 我不懂代码。我是安卓新手。如果我给你完整的代码,你能修复这个项目吗?
    猜你喜欢
    • 1970-01-01
    • 2018-01-05
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多