使用 BaseAdapter class 创建动态 ListView 无效

Creating dynamic ListView using BaseAdapter class not working

我正在尝试使用 BaseAdapter class 创建动态列表视图。 下面是我在这里使用的代码。

主要代码 Activity:

public class DisplayFlightsActivity extends AppCompatActivity {

String str_jsonresponse,fare,carrier,number,arrivaltime,departuretime,origin,destination;

ArrayList<String> listitem = new ArrayList<String>();
ListView listView;
//MyBaseAdapter baseAdapter;
Context context = DisplayFlightsActivity.this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    str_jsonresponse = extras.getString(DisplaySummaryActivity.STR_JSONRESPONSE);

    try{
        JSONObject root_jsonresponse = new JSONObject(str_jsonresponse);
        JSONObject l1_jsonobject = root_jsonresponse.optJSONObject("trips");
        JSONArray l2_jsonarray = l1_jsonobject.optJSONArray("tripOption");
        for (int i=0;i < l2_jsonarray.length();i++)
        {
            JSONObject l21_jsonobject = l2_jsonarray.getJSONObject(i);
            fare = l21_jsonobject.getString("saleTotal");
            JSONArray l3_jsonarray = l21_jsonobject.optJSONArray("slice");
            for (int j=0;j < l3_jsonarray.length();j++)
            {
                JSONObject l4_jsonobject = l3_jsonarray.getJSONObject(j);
                JSONArray l5_jsonarray = l4_jsonobject.optJSONArray("segment");
                for(int k=0;k < l5_jsonarray.length();k++)
                {
                    JSONObject l6_jsonobject = l5_jsonarray.getJSONObject(k);
                    JSONObject l7_jsonobject = l6_jsonobject.optJSONObject("flight");
                    carrier = l7_jsonobject.getString("carrier");
                    number = l7_jsonobject.getString("number");
                    JSONArray l8_jsonarray = l6_jsonobject.optJSONArray("leg");
                    for(int m=0;m < l8_jsonarray.length(); m++)
                    {
                        JSONObject l9_jsonobject = l8_jsonarray.getJSONObject(m);
                        arrivaltime = l9_jsonobject.getString("arrivalTime");
                        departuretime = l9_jsonobject.getString("departureTime");
                        origin = l9_jsonobject.getString("origin");
                        destination = l9_jsonobject.getString("destination");
                    }
                }
            }
            listitem.add(fare);
            listitem.add(carrier);
            listitem.add(number);
            listitem.add(arrivaltime);
            listitem.add(departuretime);
            listitem.add(origin);
            listitem.add(destination);
        }
    }
    catch(JSONException JE){
        JE.printStackTrace();
    }
    catch (Exception err){
        err.printStackTrace();
    }

    MyBaseAdapter baseAdapter = new MyBaseAdapter(context,listitem);
    listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(baseAdapter);

    setTitle("Results Page");
    //setContentView(R.layout.activity_display_flights);
}
}

基本适配器代码 class:

class MyBaseAdapter extends BaseAdapter {

    Context context;
    ArrayList<String> listitem = new ArrayList<>();
    LayoutInflater inflater = null;

    public MyBaseAdapter(Context context, ArrayList<String> listitem) {
        this.context = context;
        this.listitem = listitem;

        inflater = LayoutInflater.from(this.context);

    }

    public int getCount() {
        return listitem.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.listview_item_layout, null);


        TextView carrier = (TextView) vi.findViewById(R.id.layout_textview1);
        TextView number = (TextView) vi.findViewById(R.id.layout_textview2);
        TextView arrivaltime = (TextView) vi.findViewById(R.id.layout_textview6);
        TextView departuretime = (TextView) vi.findViewById(R.id.layout_textview7);
        TextView saletotal = (TextView) vi.findViewById(R.id.layout_textview8);

        String str_carrier = listitem.get(position).toString();
        String str_number = listitem.get(position).toString();
        String str_arrivaltime = listitem.get(position).toString();
        String str_departuretime = listitem.get(position).toString();
        String str_saletotal = listitem.get(position).toString();

        carrier.setText(str_carrier);
        number.setText(str_number);
        arrivaltime.setText(str_arrivaltime);
        departuretime.setText(str_departuretime);
        saletotal.setText(str_saletotal);

        return vi;
    }
}

我的问题是:基本适配器 class 从来没有为我调用过。我试图调试,但控件从未进入 BaseAdapter class。我能够解析 JSON 数据并将其放入 ListItem 但我无法访问 Base Adapter class.

我做错了什么吗?任何人都可以查看代码并告诉我为什么它不起作用。

提前致谢!

我可以通过在上面添加行 setContentView(R.layout.activity_display_flights); 来解决问题,就像这样 --

baseAdapter = new MyBaseAdapter(context,listitem);

setTitle("Results Page");
setContentView(R.layout.activity_display_flights);
listView = (ListView) findViewById(R.id.list_view);
listView.setBackgroundResource(R.drawable.androidbckgrnd1);
listView.setAdapter(baseAdapter);

早些时候 ListView 实例为空。现在这个问题已经解决了。