我正在處理一個應用程序,該應用程序將顯示包含其名稱和價格的包列表。所有的數據[包的名稱和價格]來自server.now我只是不想顯示價格= 0的那些包...如何停止創建列表視圖時顯示的列表項?
我已經寫了下面的代碼到我的自定義適配器的getview()方法...
if(packagelist.get(position).getPrice()==0)
{
return null;
}
但是在getview()中返回null在應用程序崩潰時不起作用。有沒有解決方案?請幫我...
logcat的輸出
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cakenkeyboard.appoinment, PID: 18099
java.lang.NullPointerException
at android.widget.AbsListView.obtainView(AbsListView.java:2277)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16514)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5129)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16514)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16514)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5129)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16514)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16514)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5129)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16514)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5129)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16514)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5129)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16514)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5129)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16514)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5129)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2305)
at android.view.View.measure(View.java:16514)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5095)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
編輯: 在我的應用程序,每包有兩個價格。當用戶選擇特定的單選按鈕時,我正在根據所選的單選按鈕通過適配器的updateResult()方法更新整個列表。我已將兩個價格存儲在單個bean類中。
這是我的bean類存儲特定包的數據...
public class PackageData implements Serializable{
private int price1,price2;
private String package_name;
private int id;
private boolean isSelected;
public PackageData(int id, int price1, int price2, String package_name) {
this.id = id;
this.price1 = price1;
this.price2 = price2;
this.package_name = package_name;
isSelected = false;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getPrice1() {
return price1;
}
public void setPrice1(int price1) {
this.price1 = price1;
}
public int getPrice2() {
return price2;
}
public void setPrice2(int price2) {
this.price2 = price2;
}
public String getPackage_name() {
return package_name;
}
public void setPackage_name(String package_name) {
this.package_name = package_name;
}
public void setSelectionFlag(boolean isSelected)
{
this.isSelected = isSelected;
}
public boolean getSelectionflag()
{
return this.isSelected;
}}
這是我的自定義適配器....
public class PackageRowAdapter extends BaseAdapter {
private Context ctx;
private LayoutInflater inflater;
private List<PackageData> packagelist;
boolean isPrice1;
public PackageRowAdapter(Context ctx, List<PackageData> packagelist)
{
this.ctx = ctx;
this.packagelist = packagelist;
isPrice1 = true;
}
public class Viewholder {
CheckBox chkbox;
TextView lblprice;
public Viewholder(View v)
{
this.chkbox = (CheckBox) v.findViewById(R.id.package_row_chkbox);
this.lblprice = (TextView) v.findViewById(R.id.package_row_price);
}
}
@Override
public int getCount() {
return packagelist.size();
}
@Override
public Object getItem(int i) {
return packagelist.get(i);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// getting package data for the row
PackageData currentPackage = packagelist.get(position);
TextView lbltitle;
Viewholder holder = null;
if (inflater == null) {
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null)
{
convertView = inflater.inflate(R.layout.row_package, parent, false);
//Binding UI elements with JAVA Objects....
holder = new Viewholder(convertView);
lbltitle = (TextView) convertView.findViewById(R.id.package_row_title);
//setting recieved data into UI elements
lbltitle.setText(currentPackage.getPackage_name());
holder.chkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
set_Checkbox(view);
}
});
convertView.setTag(holder);
}
else
{
holder = (Viewholder) convertView.getTag();
}
holder.chkbox.setChecked(currentPackage.getSelectionflag());
if(isPrice1)
{
holder.lblprice.setText(currentPackage.getPrice1() + " Rs.");
if(packagelist.get(position).getPrice1()==0)
{
return null;
}
}
else
{
holder.lblprice.setText(currentPackage.getPrice2() + " Rs.");
if(packagelist.get(position).getPrice2()==0)
{
return null;
}
}
holder.chkbox.setTag(position);
return convertView;
}//End of getView()
public void updateResult(Boolean isPrice1)
{
this.isPrice1 = isPrice1;
//Triggers the list update
notifyDataSetChanged();
}
public void set_Checkbox(View view)
{
CheckBox v = (CheckBox) view;
int temp_position =(Integer)v.getTag();
Log.w("Position Value",""+temp_position + " checked = " + ((CheckBox) view).isChecked());
PackageData temp_package = packagelist.get(temp_position);
temp_package.setSelectionFlag(v.isChecked());
}}
爲什麼應用程序崩潰?在創建列表時,將您的logcat隊友 – basic
設置可見性消失並顯示在getView – Pavya
中您可以忽略其值小於零的包? –