需要显式调用BaseAdapter类方法吗?

时间:2015-08-20 19:27:20

标签: java android baseadapter

我正在尝试使用BaseAdapter类创建动态列表视图。为此,我已将BaseAdapter类扩展为一个自定义适配器类。我已经实现了BaseAdapter的所有默认方法,如getCount(),getItem(),getView()等。

现在,在调用自定义基本适配器类时,它只执行该类中的构造函数。这些方法没有被调用。

当我明确地单独调用方法时,事情就会起作用。

以下是我的自定义基础适配器类的代码:

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;
}

}

主要活动类的部分是:

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

如果我明确地打电话给他们,那么它是有效的。如下 -

int listitem_size = baseAdapter.getCount();

我的问题是: 根据我之前读过的内容,这些方法总是被隐含地称为。那为什么它不适合我。 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>

主要活动的代码在这里:

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);
}
}

1 个答案:

答案 0 :(得分:1)

我玩过你的代码。请看下面的代码。

以下代码位于您的活动的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,您将找到为{OS}隐式调用的getCount()编写的日志。

查找我用于示例的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>