无法将新数据从JSON添加到Android中的Listview适配器

时间:2016-01-22 04:49:00

标签: android listview android-volley

我绝对是Android的初学者。我正在学习如何使用排球并将数据绑定到来自排球请求响应的listview。当我获取请求并检索响应并将其转换为模型对象时。一切正常。但是当我将它绑定到Listview时。这是投掷错误。我的代码出了什么问题?这是正确的做法吗?

我的活动课

public class VolleyActivity extends Activity{
    private int lastSawFirstListItem;
    private int itemLoadedOn;
    private ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.volley_main);

        Button getBtn = (Button)findViewById(R.id.btn_get_request);
        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        ListView listView = (ListView)findViewById(R.id.volleyListView);
        adapter = new CustomAdapter(this,new Entity[0]);
        listView.setAdapter(adapter);
        String url = "http://api.androidhive.info/feed/feed.json";

        JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                List<Entity> items = new ArrayList<Entity>();
                try{
                    JSONArray jsonArray = jsonObject.getJSONArray("feed");
                    for(int i=0;i<jsonArray.length();i++)
                    {
                        JSONObject item = jsonArray.getJSONObject(i);
                        Entity en = new Entity();
                        en.setId(item.getInt("id"));
                        en.setName(item.getString("name"));
                        en.setUrl(item.getString("url"));
                        items.add(en);
                    }
                    adapter.addAll(items);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getBaseContext(),"Success",Toast.LENGTH_SHORT).show();
                }
                catch (JSONException e)
                {
                    Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });

        Volley.newRequestQueue(getBaseContext()).add(jsonRequest);
  }
}

这是我的自定义适配器

public class CustomAdapter extends ArrayAdapter<Entity> {
    private final Context context;
    private final Entity[] values;

    public CustomAdapter(Context context,Entity[] values)
    {
        super(context,-1,values);
        this.context = context;
        this.values = values;
    }

    @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.list_row,parent,false);
        TextView id_text = (TextView)rowView.findViewById(R.id.row_id);
        TextView name_text = (TextView)rowView.findViewById(R.id.row_name);
        TextView url_text = (TextView)rowView.findViewById(R.id.row_url);
        id_text.setText(values[position].getId());
        name_text.setText(values[position].getName());
        url_text.setText(values[position].getUrl());
        return rowView;
    }
}

这是我运行活动时的logcat

01-22 04:45:56.298 1492-1492/? W/EGL_genymotion: eglSurfaceAttrib not implemented
    01-22 04:45:56.354 477-492/? I/ActivityManager: Displayed com.example.newfeeds.newfeeds/.VolleyActivity: +110ms
    01-22 04:45:57.070 1492-1492/? D/AndroidRuntime: Shutting down VM
    01-22 04:45:57.070 1492-1492/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa6219908)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime: FATAL EXCEPTION: main
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime: java.lang.UnsupportedOperationException
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at java.util.AbstractList.add(AbstractList.java:404)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at java.util.AbstractList.add(AbstractList.java:425)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at java.util.AbstractCollection.addAll(AbstractCollection.java:76)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:195)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at com.example.newfeeds.newfeeds.VolleyActivity$2.onResponse(VolleyActivity.java:66)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at com.example.newfeeds.newfeeds.VolleyActivity$2.onResponse(VolleyActivity.java:51)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:725)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    01-22 04:45:57.074 1492-1492/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
    01-22 04:45:57.074 477-698/? W/ActivityManager:   Force finishing activity com.example.newfeeds.newfeeds/.VolleyActivity
    01-22 04:45:57.150 477-698/? D/dalvikvm: GC_FOR_ALLOC freed 129K, 29% free 9794K/13620K, paused 10ms, total 10ms
    01-22 04:45:57.654 477-490/? W/ActivityManager: Activity pause timeout for ActivityRecord{533353cc u0 com.example.newfeeds.newfeeds/.VolleyActivity}
    01-22 04:46:07.154 477-490/? W/ActivityManager: Launch timeout has expired, giving up wake lock!
    01-22 04:46:07.654 477-490/? W/ActivityManager: Activity idle timeout for ActivityRecord{53441ff4 u0 com.example.newfeeds.newfeeds/.MainActivity}
    01-22 04:46:20.714 56-56/? D/Genyd: Received Set Clipboard
    01-22 04:46:20.714 56-56/? D/Genymotion: Received Set Clipboard
    01-22 04:47:00.034 477-480/? D/dalvikvm: GC_CONCURRENT freed 1670K, 28% free 9848K/13620K, paused 1ms+2ms, total 25ms
    01-22 04:47:15.470 56-56/? D/Genyd: Received Set Clipboard
    01-22 04:47:15.470 56-56/? D/Genymotion: Received Set Clipboard

这是使用Listview和绑定数据的凌空的正确方法吗?我该如何解决这个错误?

然后我在自定义适配器中从E​​ntity []更改为ArrayList<Entity>。它仍然抛出错误。

我的新定制适配器类,带有ArrayList

public class CustomAdapter extends ArrayAdapter<Entity> {
    private final Context context;
    private final ArrayList<Entity> values;

    public CustomAdapter(Context context,ArrayList<Entity> values)
    {
        super(context,-1,values);
        this.context = context;
        this.values = values;
    }

    @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.list_row,parent,false);
        TextView id_text = (TextView)rowView.findViewById(R.id.row_id);
        TextView name_text = (TextView)rowView.findViewById(R.id.row_name);
        TextView url_text = (TextView)rowView.findViewById(R.id.row_url);
        id_text.setText(values.get(position).getId());
        name_text.setText(values.get(position).getName());
        url_text.setText(values.get(position).getUrl());
        return rowView;
    }
}

这是我改为ArrayList后的logcat

01-22 05:09:54.770 1895-1895/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa6219908)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime: FATAL EXCEPTION: main
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0x1
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.content.res.Resources.getText(Resources.java:230)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.TextView.setText(TextView.java:3769)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at com.example.newfeeds.newfeeds.CustomAdapter.getView(CustomAdapter.java:36)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.ListView.makeAndAddView(ListView.java:1831)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.ListView.fillDown(ListView.java:674)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.ListView.fillFromTop(ListView.java:735)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.ListView.layoutChildren(ListView.java:1638)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.View.layout(View.java:14008)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4373)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:532)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:725)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    01-22 05:09:54.774 1895-1895/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
    01-22 05:09:54.778 477-489/? W/ActivityManager:   Force finishing activity com.example.newfeeds.newfeeds/.VolleyActivity
    01-22 05:09:54.894 477-489/? D/dalvikvm: GC_FOR_ALLOC freed 179K, 33% free 9817K/14460K, paused 15ms, total 15ms
    01-22 05:09:55.398 477-490/? W/ActivityManager: Activity pause timeout for ActivityRecord{5334f1a0 u0 com.example.newfeeds.newfeeds/.VolleyActivity}

4 个答案:

答案 0 :(得分:1)

好的,如果我理解正确,你使用的是Entity[] values,这是一个正常的数组,你必须定义它在初始化时的大小。

问题在于,这个正常的数组是它无法在移动中扩展或缩小(我的意思是你不能减少或添加更多的项目)。因此它的大小与初始化时的大小相同。

在您的活动中,您提供的是ArrayList对象,可以在旅途中添加或删除项目。

所以这就是你在这里面临的冲突,

您需要做的是ArrayList中有Customadapter个实例,类似这样的内容,

    private final Context context;
    private final ArrayList<Entity> values;

    public CustomAdapter(Context context,ArrayList<Entity> values;
    {
        super(context,-1,values);
        this.context = context;
        this.values = values;
    }

现在您可以分配这样的值     ArrayList values = new ArrayList();     CustomAdapter adapter = new CustomAdapter(this,values);

在呼叫网络呼叫后,将项目添加到列表中,如此

values.addAll(items);

并调用此方法notifyDataSetChanged(),因为您可以使用观察者模式

adapter.notifyDataSetChanged();

您还可以使用github上的listview了解JSON人口中的一些示例,

并且您必须更像OOP,创建一个模型类,其实例将为您保存数据

public class Entity {

    private String id;
    private String name;
    private String url;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
}
  

更新,   因为有很多困惑,试试这个

public class ListVewTestThreeActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<Entity> entities;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_vew_test_three);
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_custom_toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initializeUI();
    }

    private void initializeUI() {
        listView = (ListView) findViewById(R.id.ListVewTestThreeActivity_listView);
        entities = new ArrayList<>();
        myAdapter = new MyAdapter(getApplicationContext(), R.layout.simple_list_item_1, entities);
        listView.setAdapter(myAdapter);

        new DownloadJSON().execute();
    }

    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        private ArrayList<Entity> tempEntities;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            tempEntities = new ArrayList<>();
        }

        @Override
        protected Void doInBackground(Void... params) {

            try {

                URL url = new URL("http://api.androidhive.info/feed/feed.json");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();

                String result = IOUtils.toString(httpURLConnection.getInputStream());
                System.out.println("" + result);

                JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray = jsonObject.getJSONArray("feed");

                for (int i = 0; i < jsonArray.length(); i++) {
                    Entity entity = new Entity();
                    entity.setId(jsonArray.getJSONObject(i).getString("id"));
                    entity.setName(jsonArray.getJSONObject(i).getString("name"));
                    entity.setUrl(jsonArray.getJSONObject(i).getString("image"));
                    System.out.println(""+entity.getName());
                    tempEntities.add(entity);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if (tempEntities != null) {
                entities.addAll(tempEntities);
                System.out.println("Size: "+entities.size());
                myAdapter.notifyDataSetChanged();
            }

        }
    }


    private class MyAdapter extends ArrayAdapter {

        private ArrayList<Entity> a_productInfos;
        private Context a_context;
        private LayoutInflater a_layoutInflater;

        public MyAdapter(Context context, int resource, ArrayList<Entity> a_productInfos) {
            super(context, resource, a_productInfos);
            this.a_productInfos = a_productInfos;
            this.a_context = context;
            a_layoutInflater = LayoutInflater.from(this.a_context);
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewHolder holder = null;
            if (row == null) {
                row = a_layoutInflater.inflate(R.layout.single_item_listview_test_three, parent, false);
                holder = new ViewHolder();
                holder.id = (TextView) row.findViewById(R.id.ListVewTestThreeActivity_id_textView);
                holder.name = (TextView) row.findViewById(R.id.ListVewTestThreeActivity_name_textView);
                holder.url = (TextView) row.findViewById(R.id.ListVewTestThreeActivity_url_textView);
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }

            final Entity productInfo = a_productInfos.get(position);
            holder.id.setText("" + productInfo.getId());
            holder.name.setText("" + productInfo.getName());
            holder.url.setText("" + productInfo.getUrl());
            return row;
        }

        class ViewHolder {
            TextView id;
            TextView name;
            TextView url;
        }

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

    public class Entity {

        private String id;
        private String name;
        private String url;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
  

输出

enter image description here

更新

答案 1 :(得分:0)

请用以下代码替换您的代码,如果有帮助请告诉我。

VolleyActivity

    public class VolleyActivity extends Activity{
    private int lastSawFirstListItem;
    private int itemLoadedOn;
    private ArrayAdapter adapter;
    private List<Entity> items;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.volley_main);

        Button getBtn = (Button)findViewById(R.id.btn_get_request);
        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        items = new ArrayList<Entity>();
        ListView listView = (ListView)findViewById(R.id.volleyListView);
        adapter = new CustomAdapter(this, items);
        listView.setAdapter(adapter);
        String url = "http://api.androidhive.info/feed/feed.json";

        JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                List<Entity> items = new ArrayList<Entity>();
                try{
                    JSONArray jsonArray = jsonObject.getJSONArray("feed");
                    for(int i=0;i<jsonArray.length();i++)
                    {
                        JSONObject item = jsonArray.getJSONObject(i);
                        Entity en = new Entity();
                        en.setId(item.getInt("id"));
                        en.setName(item.getString("name"));
                        en.setUrl(item.getString("url"));
                        items.add(en);
                    }
                    adapter.addAll(items);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getBaseContext(),"Success",Toast.LENGTH_SHORT).show();
                }
                catch (JSONException e)
                {
                    Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });

        Volley.newRequestQueue(getBaseContext()).add(jsonRequest);
    }
}

CustomAdapter

    public class CustomAdapter extends ArrayAdapter<Entity> {
      private final Context context;
      private List<Entity> values;

      public CustomAdapter(Context context,List<Entity> values){
         super(context,-1,values);
         this.context = context;
         this.values = values;
      }

      @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.list_row,parent,false);
          TextView id_text = (TextView)rowView.findViewById(R.id.row_id);
          TextView name_text = (TextView)rowView.findViewById(R.id.row_name);
          TextView url_text = (TextView)rowView.findViewById(R.id.row_url);
          id_text.setText(String.valueOf(values.get(position).getId()));
          name_text.setText(values.get(position).getName());
          url_text.setText(values.get(position).getUrl());
          return rowView;
      }
     }

答案 2 :(得分:0)

我认为您在adapterid_text.setText(values[position].getId());的下一行收到错误您必须将其更改为

id_text.setText(String.valueOf(values[position].getId()))

这很简单,因为你必须在setText方法中传递String。目前它正在获得int价值,这就是为什么它会从R中找到资源的原因。这就是为什么它会抛出{{}} {\ n} {1}}。

希望你能清楚。

答案 3 :(得分:0)

我得到了Pankaj Nimgade和Soham的答案相结合的答案。首先,我将自定义适配器的值转换为ArrrayList而不是数组。然后我在适配器中检索id为整数的id值。

String.valueOf(values.get(position).getId())

它甚至不需要adapter.notifyDataSetChanged()。它有效。谢谢大家。