我正在尝试使用BaseAdapter类创建动态列表视图。 以下是我在这里使用的代码。
主要活动代码:
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 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;
}
}
我的问题是:永远不会为我调用Base Adapter类。我试图调试,但控件永远不会进入BaseAdapter类。我能够解析JSON数据并将其放在ListItem中,但我无法访问Base Adapter类。
我做错了什么吗?任何人都可以查看代码并让我知道它为什么不起作用。
提前致谢!
答案 0 :(得分:0)
我能够通过在上面添加 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实例为null。现在这个问题已经解决了。