Android 视图未返回到基本适配器
我有扩展基本适配器的PurchaseListAdapter,但当我在我的活动中调用它时,它不会进入getView,当我调用listPurchase.setAdapteradapter时返回null 这是我的密码 Results.javaAndroid 视图未返回到基本适配器,android,listview,baseadapter,Android,Listview,Baseadapter,我有扩展基本适配器的PurchaseListAdapter,但当我在我的活动中调用它时,它不会进入getView,当我调用listPurchase.setAdapteradapter时返回null 这是我的密码 Results.java ... List<Purchase> purchases ; purchases = controller.getAll(); listPurchase = new ListView(getBaseContext()
...
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>
列表视图:
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>
什么是controller.getAll?是否确实返回包含项目的ArrayList?它返回List purchases=new ArrayList;从数据库中。我检查过了,它返回的值是正确的。在baseAdapter中,我检查了getCount和列表,它返回ok。那么,这里的问题是什么?是否不显示该列表?你有例外吗?请提供更多详细信息,以及您的活动/片段代码;在Results活动中,它在该行中给出了一个异常,nullpointer异常。当我在适配器中调试时,它没有进入getView,我认为这是问题所在。你确定你的listPurchase不是null吗?