BaseAdapter class 方法需要显式调用?
BaseAdapter class methods need to be called explicitly?
我正在尝试使用 BaseAdapter class 创建动态列表视图。为此,我将 BaseAdapter class 扩展为一个自定义适配器 class。我已经实现了 BaseAdapter 的所有默认方法,如 getCount()、getItem()、getView() 等。
现在在调用自定义基础适配器 class 时,它仅在 class 中执行构造函数。这些方法未被调用。
当我单独显式调用这些方法时,一切正常。
下面是我的自定义基础适配器代码 class:
public class MyBaseAdapter extends BaseAdapter {
public Context ba_context;
public ArrayList<String> listitem = new ArrayList<>();
public LayoutInflater inflater;
public MyBaseAdapter(Context ma_context, ArrayList<String> ma_listitem) {
super();
this.ba_context = ma_context;
this.listitem = ma_listitem;
inflater = (LayoutInflater) ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.listitem.size();
}
@Override
public Object getItem(int position) {
return this.listitem.get(position);
}
@Override
public long getItemId(int position) {
return (long) position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.listview_item_layout,parent,false);
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;
}
}
Main Activity class 的部分是:
MyBaseAdapter baseAdapter = new MyBaseAdapter(context,listitem);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(baseAdapter);
如果我明确地调用它们,它就会起作用。如下所示 --
int listitem_size = baseAdapter.getCount();
我的问题是:
1.根据我之前的阅读,这些方法总是被隐式调用。那为什么它对我不起作用。
2. 我还能继续我的工作,做这个显式调用吗?
请帮帮我!
编辑部分
添加 ListView 布局 xml.
<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:layout_marginBottom="10dip"
tools:context="saumikbhattacharya.FlightSearch.com.DisplayFlightsActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
Main 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);
}
}
我玩过你的代码。请看下面的代码。
下面的代码进入 Activity 的 onCreate()
:
ArrayList<String> listitem = new ArrayList<String>();
listitem.add("ABC");
listitem.add("DEF");
MyBaseAdapter baseAdapter = new MyBaseAdapter(this,listitem);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(baseAdapter);
以下代码用于 BaseAdapter 实现:
public class MyBaseAdapter extends BaseAdapter {
public Context ba_context;
public ArrayList<String> listitem = new ArrayList<String>();
public LayoutInflater inflater;
public MyBaseAdapter(Context ma_context, ArrayList<String> ma_listitem) {
super();
this.ba_context = ma_context;
this.listitem = ma_listitem;
inflater = (LayoutInflater) ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
Log.e("MyBaseAdapter", "getCount() => " + this.listitem.size());
return this.listitem.size();
}
@Override
public Object getItem(int position) {
return this.listitem.get(position);
}
@Override
public long getItemId(int position) {
return (long) position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
Log.i("MyBaseAdapter", "String=>" + this.listitem.get(position).toString());
if (convertView == null)
vi = inflater.inflate(R.layout.listview_item_layout, parent, false);
TextView tvData = (TextView) vi.findViewById(R.id.tvData);
String data = listitem.get(position).toString();
tvData.setText(data);
return vi;
}
}
运行 你的申请
查看您的 LogCat,您会发现为 getCount()
编写的日志被 Android OS 隐式调用。
在下面找到我用于示例的 listview_item_layout.xml
文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="10dp" >
<TextView android:id="@+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="data"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"/>
</LinearLayout>
我正在尝试使用 BaseAdapter class 创建动态列表视图。为此,我将 BaseAdapter class 扩展为一个自定义适配器 class。我已经实现了 BaseAdapter 的所有默认方法,如 getCount()、getItem()、getView() 等。
现在在调用自定义基础适配器 class 时,它仅在 class 中执行构造函数。这些方法未被调用。
当我单独显式调用这些方法时,一切正常。
下面是我的自定义基础适配器代码 class:
public class MyBaseAdapter extends BaseAdapter {
public Context ba_context;
public ArrayList<String> listitem = new ArrayList<>();
public LayoutInflater inflater;
public MyBaseAdapter(Context ma_context, ArrayList<String> ma_listitem) {
super();
this.ba_context = ma_context;
this.listitem = ma_listitem;
inflater = (LayoutInflater) ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.listitem.size();
}
@Override
public Object getItem(int position) {
return this.listitem.get(position);
}
@Override
public long getItemId(int position) {
return (long) position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.listview_item_layout,parent,false);
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;
}
}
Main Activity class 的部分是:
MyBaseAdapter baseAdapter = new MyBaseAdapter(context,listitem);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(baseAdapter);
如果我明确地调用它们,它就会起作用。如下所示 --
int listitem_size = baseAdapter.getCount();
我的问题是: 1.根据我之前的阅读,这些方法总是被隐式调用。那为什么它对我不起作用。 2. 我还能继续我的工作,做这个显式调用吗?
请帮帮我!
编辑部分 添加 ListView 布局 xml.
<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:layout_marginBottom="10dip"
tools:context="saumikbhattacharya.FlightSearch.com.DisplayFlightsActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
Main 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);
}
}
我玩过你的代码。请看下面的代码。
下面的代码进入 Activity 的 onCreate()
:
ArrayList<String> listitem = new ArrayList<String>();
listitem.add("ABC");
listitem.add("DEF");
MyBaseAdapter baseAdapter = new MyBaseAdapter(this,listitem);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(baseAdapter);
以下代码用于 BaseAdapter 实现:
public class MyBaseAdapter extends BaseAdapter {
public Context ba_context;
public ArrayList<String> listitem = new ArrayList<String>();
public LayoutInflater inflater;
public MyBaseAdapter(Context ma_context, ArrayList<String> ma_listitem) {
super();
this.ba_context = ma_context;
this.listitem = ma_listitem;
inflater = (LayoutInflater) ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
Log.e("MyBaseAdapter", "getCount() => " + this.listitem.size());
return this.listitem.size();
}
@Override
public Object getItem(int position) {
return this.listitem.get(position);
}
@Override
public long getItemId(int position) {
return (long) position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
Log.i("MyBaseAdapter", "String=>" + this.listitem.get(position).toString());
if (convertView == null)
vi = inflater.inflate(R.layout.listview_item_layout, parent, false);
TextView tvData = (TextView) vi.findViewById(R.id.tvData);
String data = listitem.get(position).toString();
tvData.setText(data);
return vi;
}
}
运行 你的申请
查看您的 LogCat,您会发现为 getCount()
编写的日志被 Android OS 隐式调用。
在下面找到我用于示例的 listview_item_layout.xml
文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="10dp" >
<TextView android:id="@+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="data"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_marginTop="5dp"/>
</LinearLayout>