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.
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/appBar"
- android:layout_weight="1"/>
- <!--<LinearLayout-->
- <!--android:layout_width="fill_parent"-->
- <!--android:layout_height="wrap_content"-->
- <!-->-->
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:id="@+id/listview" />
- <!--</LinearLayout>-->
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/rank"
- android:layout_weight="1"/>
- </LinearLayout>
In this way, you can correctly display your own Adapter.
Code:
- package com.example.dolby.webviewtest;
- import android.content.Context;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonArrayRequest;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.w3c.dom.Text;
- import java.util.ArrayList;
- import java.util.Date;
- //class jobinfo
- //{
- // public String url;
- // public String name;
- //}
- public class MainActivity extends ActionBarActivity {
- private TextView text;
- private ListView infolist;
- private ArrayList<JSONObject> jsData;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- text = (TextView)findViewById(R.id.appBar);
- jsData = new ArrayList<JSONObject>();
- infolist = (ListView)findViewById(R.id.listview);
- startLoadInfo();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private void startLoadInfo()
- {
- RequestQueue queue = Volley.newRequestQueue(this);
- String jsonurl = "http://jobdev.sinaapp.com/api/getinfoList";
- JsonArrayRequest jsRequest = new JsonArrayRequest(jsonurl,new Response.Listener<JSONArray>() {
- @Override
- public void onResponse(JSONArray response) {
- //Log.d("first is " , "" + response.getJSONObject(0));
- try{
- //text.setText(response.getJSONObject(0).getString("name"));
- for(int i = 0; i < response.length() ; i++)
- {
- jsData.add(response.getJSONObject(i));
- }
- jsonAdapter adapter = new jsonAdapter(MainActivity.this);
- infolist.setAdapter(adapter);
- }
- catch(Exception e)
- {
- Log.d("json get exception",e.getMessage());
- }
- }
- },new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.d("json Request Error",error.getMessage());
- }
- });
- queue.add(jsRequest);
- }
- class jsonAdapter extends BaseAdapter
- {
- Context m_context;
- public jsonAdapter(Context context)
- {
- super();
- m_context = context;
- }
- @Override
- public int getCount(){
- return jsData.size();
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- TextView view = new TextView(m_context);
- String name = "";
- try{
- name = jsData.get(position).getString("name");
- }catch(Exception e){
- Log.d("haha error exception","nick");
- }
- view.setText(name);
- return view;
- }
- }
- }