Reputation: 941
I am trying to create a dynamic listview using BaseAdapter class. For that I have extended the BaseAdapter class into one custom adapter class. I have implemented all the default methods for BaseAdapter like getCount(), getItem(), getView() etc.
Now while calling the custom base adapter class, it is only executing the constructor within that class. The methods are not getting invoked.
When I explicitly call the methods individiually then things are working.
Below are my code for custom base adapter 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;
}
}
The portion of Main Activity class is:
MyBaseAdapter baseAdapter = new MyBaseAdapter(context,listitem);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(baseAdapter);
If I explicitly call them then it works. Like below --
int listitem_size = baseAdapter.getCount();
MY QUESTION IS: 1. According to what I have read before, these methods are always implicitly called. Then why it is not working for me. 2. Can I still proceed with my work, doing this explicit call?
Please help me out!
Editted Part Adding the ListView layout 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>
The code for Main Activity is here:
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);
}
}
Upvotes: 0
Views: 2153
Reputation: 1105
I have played with your code. Please look at below code.
Below code goes inside onCreate()
of your Activity :
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);
Below code is for BaseAdapter implementation :
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;
}
}
Run your application
Look at your LogCat and you will find log which is written for getCount()
that is called implicitly by Android OS.
Find below listview_item_layout.xml
file which i used for my example.
<?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>
Upvotes: 1