Android Development notes -- Repeated execution of baseAdapter's getview Method

Source: Internet
Author: User

Android Development notes -- Repeated execution of baseAdapter's getview Method
When implementing the baseAdapter subclass, The getview method is overloaded to implement its own Adapter object. However, during the debugging process, it is found that this getview will be continuously called, as a result, the listview does not display what we need.
The key problem is that we have not set the length of the listview, so we cannot determine how many custom views need to be drawn in the listview, therefore, we need to set the height of the listview in the layout file. You can select fill_parent or set the value height.

 
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  4. android:paddingRight="@dimen/activity_horizontal_margin"
  5. android:paddingTop="@dimen/activity_vertical_margin"
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/appBar"
  11. android:layout_weight="1"/>
  12. <!--<LinearLayout-->
  13. <!--android:layout_width="fill_parent"-->
  14. <!--android:layout_height="wrap_content"-->
  15. <!-->-->
  16. <ListView
  17. android:layout_width="fill_parent"
  18. android:layout_height="50dip"
  19. android:id="@+id/listview" />
  20. <!--</LinearLayout>-->

  21. <TextView
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:id="@+id/rank"
  25. android:layout_weight="1"/>
  26. </LinearLayout>
In this way, you can correctly display your own Adapter.
Code:

 
 
  1. package com.example.dolby.webviewtest;

  2. import android.content.Context;
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.BaseAdapter;
  12. import android.widget.ListView;
  13. import android.widget.TextView;

  14. import com.android.volley.Request;
  15. import com.android.volley.RequestQueue;
  16. import com.android.volley.Response;
  17. import com.android.volley.VolleyError;
  18. import com.android.volley.toolbox.JsonArrayRequest;
  19. import com.android.volley.toolbox.JsonObjectRequest;
  20. import com.android.volley.toolbox.Volley;

  21. import org.json.JSONArray;
  22. import org.json.JSONObject;
  23. import org.w3c.dom.Text;

  24. import java.util.ArrayList;
  25. import java.util.Date;


  26. //class jobinfo
  27. //{
  28. // public String url;
  29. // public String name;
  30. //}

  31. public class MainActivity extends ActionBarActivity {

  32. private TextView text;
  33. private ListView infolist;
  34. private ArrayList<JSONObject> jsData;
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. text = (TextView)findViewById(R.id.appBar);
  40. jsData = new ArrayList<JSONObject>();
  41. infolist = (ListView)findViewById(R.id.listview);
  42. startLoadInfo();


  43. }


  44. @Override
  45. public boolean onCreateOptionsMenu(Menu menu) {
  46. // Inflate the menu; this adds items to the action bar if it is present.
  47. getMenuInflater().inflate(R.menu.menu_main, menu);
  48. return true;
  49. }

  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. // Handle action bar item clicks here. The action bar will
  53. // automatically handle clicks on the Home/Up button, so long
  54. // as you specify a parent activity in AndroidManifest.xml.
  55. int id = item.getItemId();

  56. //noinspection SimplifiableIfStatement
  57. if (id == R.id.action_settings) {
  58. return true;
  59. }

  60. return super.onOptionsItemSelected(item);
  61. }

  62. private void startLoadInfo()
  63. {
  64. RequestQueue queue = Volley.newRequestQueue(this);
  65. String jsonurl = "http://jobdev.sinaapp.com/api/getinfoList";
  66. JsonArrayRequest jsRequest = new JsonArrayRequest(jsonurl,new Response.Listener<JSONArray>() {
  67. @Override
  68. public void onResponse(JSONArray response) {
  69. //Log.d("first is " , "" + response.getJSONObject(0));
  70. try{
  71. //text.setText(response.getJSONObject(0).getString("name"));
  72. for(int i = 0; i < response.length() ; i++)
  73. {
  74. jsData.add(response.getJSONObject(i));
  75. }
  76. jsonAdapter adapter = new jsonAdapter(MainActivity.this);
  77. infolist.setAdapter(adapter);

  78. }
  79. catch(Exception e)
  80. {
  81. Log.d("json get exception",e.getMessage());
  82. }

  83. }

  84. },new Response.ErrorListener() {
  85. @Override
  86. public void onErrorResponse(VolleyError error) {
  87. Log.d("json Request Error",error.getMessage());
  88. }
  89. });
  90. queue.add(jsRequest);
  91. }

  92. class jsonAdapter extends BaseAdapter
  93. {
  94. Context m_context;
  95. public jsonAdapter(Context context)
  96. {
  97. super();
  98. m_context = context;
  99. }
  100. @Override
  101. public int getCount(){
  102. return jsData.size();
  103. }

  104. @Override
  105. public Object getItem(int position) {
  106. return null;
  107. }

  108. @Override
  109. public long getItemId(int position) {
  110. return 0;
  111. }

  112. @Override
  113. public View getView(int position, View convertView, ViewGroup parent) {
  114. TextView view = new TextView(m_context);
  115. String name = "";
  116. try{
  117. name = jsData.get(position).getString("name");
  118. }catch(Exception e){
  119. Log.d("haha error exception","nick");
  120. }
  121. view.setText(name);
  122. return view;
  123. }
  124. }
  125. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.