Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- 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
- 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;
- }
- }
- 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}
- 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;
- }
- values.addAll(items);
- adapter.notifyDataSetChanged();
- 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;
- }
- }
- }
- id_text.setText(String.valueOf(values[position].getId()))
- 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);
- }
- }
- 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;
- }
- }
- String.valueOf(values.get(position).getId())
Add Comment
Please, Sign In to add comment