我的PurchaseListAdapter扩展了基础适配器,但是当我在我的活动中调用它时,它不会进入getView并在我调用listPurchase.setAdapter(adapter);
时返回null
这是我的代码。
Results.java
...
List<Purchase> purchases ;
purchases = controller.getAll();
listPurchase = new ListView(getBaseContext());
listPurchase = (ListView) findViewById(R.id.list_purchase);
adapter = new PurchaseListAdapter(MainActivity.mContext, purchases);
listPurchase.setAdapter(adapter); //It's returning null here
PurchaseListAdapter.java
public class PurchaseListAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private List<Purchase> mItens;
//Labels
private TextView lbl_tittle;
private TextView lbl_current_tittle;
private TextView lbl_performance_tittle;
//TextView
private TextView gas_liters;
private TextView gas_price;
private TextView performance;
private TextView total_kilometers;
//private ImageView imgStatus;
//private Button btnHistory;
//Fonts
Typeface nexaBold;
Typeface nexaLight;
public PurchaseListAdapter( Context ctx, List<Purchase> itens) {
this.mItens = itens;
this.inflater = LayoutInflater.from(ctx);
}
@Override
public int getCount() {
return this.mItens.size();
}
@Override
public Object getItem(int position) {
return this.mItens.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.purchase_element, null);
gas_liters = (TextView) view.findViewById(R.id.element_gas_liters);
gas_price = (TextView) view.findViewById(R.id.element_gas_price);
performance = (TextView) view.findViewById(R.id.element_perfomance);
total_kilometers = (TextView) view.findViewById(R.id.element_total_kilometers);
//LoadingFonts
nexaBold = Typeface.createFromAsset(MainActivity.mContext.getAssets(), "nexa_bold.otf");
nexaLight = Typeface.createFromAsset(MainActivity.mContext.getAssets(), "nexa_light.otf");
setFonts(view);
//Loading Views
gas_liters.setText(mItens.get(position).getGas().toString() + "L");
gas_price.setText("R$" + mItens.get(position).getGasPrice()+"");
if(position == 0)
performance.setText("First time");
else
{
double result = (mItens.get(position).getKilometers() - (mItens.get(position-1).getKilometers()))
/ Double.parseDouble(mItens.get(position).getGas());
//Checar
performance.setText(result+" KM/L");
}
total_kilometers.setText(mItens.get(position).getKilometers()+" KM");
lbl_tittle = (TextView) view.findViewById(R.id.element_tittle);
lbl_tittle.setText(mItens.get(position).getDate().toString() + " - " + mItens.get(position).getTime().toString());
return view;
}
xml文件:
行: purchase_element.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/element_tittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:layout_gravity="center_horizontal"
android:background="@null"
android:textColor="#5d5d5d"
android:textSize="32dp"
android:text=""
/>
<View
android:layout_width="300dp"
android:layout_height="1dp"
android:layout_below="@id/element_tittle"
android:layout_marginTop="4dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="4dp"
android:background="#20000000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="11dp"
android:layout_marginBottom="10dp"
android:layout_weight="0.28" >
<TextView
android:id="@+id/element_current_tittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginTop="-1dp"
android:background="@null"
android:textColor="#5d5d5d"
android:textSize="23dp"
android:text="Current"
/>
<ImageView
android:id="@+id/img_gas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_below="@id/element_current_tittle"
android:src="@drawable/gasoline"/>
<TextView
android:id="@+id/element_gas_liters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:background="@null"
android:layout_below="@id/element_current_tittle"
android:layout_toRightOf="@id/img_gas"
android:textColor="#5d5d5d"
android:textSize="21dp"
android:text=""
/>
<TextView
android:id="@+id/element_gas_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="8dp"
android:background="@null"
android:layout_below="@id/element_gas_liters"
android:textColor="#5d5d5d"
android:textSize="21dp"
android:text=""
/>
</RelativeLayout>
<View
android:id="@+id/line"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="19dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="12dp"
android:background="#20000000"
/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="11dp"
android:layout_marginBottom="10dp"
android:layout_weight="0.28" >
<TextView
android:id="@+id/element_perfomance_tittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="08dp"
android:layout_alignParentTop="true"
android:background="@null"
android:text="Perfomance"
android:textColor="#5d5d5d"
android:textSize="23dp" />
<TextView
android:id="@+id/element_perfomance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:background="@null"
android:layout_below="@id/element_perfomance_tittle"
android:textColor="#5d5d5d"
android:textSize="21dp"
android:text=""
/>
<TextView
android:id="@+id/element_total_kilometers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:background="@null"
android:layout_below="@id/element_perfomance"
android:textColor="#5d5d5d"
android:textSize="19dp"
android:text=""
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
ListView:
purchase_list_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/gas_station_main_bg">
<ListView
android:id="@+id/list_purchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>