2016-07-15 60 views
0

我正在處理一個應用程序,該應用程序將顯示包含其名稱和價格的包列表。所有的數據[包的名稱和價格]來自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()); 
    }} 
+0

爲什麼應用程序崩潰?在創建列表時,將您的logcat隊友 – basic

+1

設置可見性消失並顯示在getView – Pavya

+0

中您可以忽略其值小於零的包? –

回答

0

你或許應該先儲存包,然後用0整理所有數據,然後將它們發送到列表中。

+0

感謝兄弟...我也認爲,首先,但在我的應用程序中,每個軟件包都有兩個價格。當用戶選擇特定的單選按鈕時,我通過適配器的updateResult()方法更新整個列表....如果price2不是0,而price1是該包的0?它不會被顯示....這不是要求... – Intimate

1
ArrayList packageList; 
ArrayList newList; 

for(int position=0; position <packageList.size() ; position) 
{ 
if(packagelist.get(position).getPrice()!=0) 
newList.add(packagelist.get(position));  
// new list will not contain PRICE==0 

} 

將newList傳遞給listview而不是packageList。我認爲它會完成這項工作。

+0

這將留下一個佔用無用空間的空白視圖。 –

+0

如果我返回視圖...那麼它將顯示列表中的視圖。我想停止顯示視圖,而不是顯示一個空視圖...... tnx bydway ... – Intimate

+0

@ WadeWilson,是的是正確的,試試這個新的代碼它會工作 – Shivam

0

只是傳遞前的ListView只創建一個新的列表和存儲數據具有值> 0,然後通過該列表的ListView

0
if(packagelist.get(position).getPrice()!=0) 
      { 
       textView.setText(""+ packagelist.get(position).getPrice()) // or any code you want when PRICE!=0 
      } 
else{ 
textView.setText(""); // Or any other value you want to display when value is 0 
} 

try this 
0

最好的解決辦法是將商品加入您想要顯示的列表,然後在期望的時間以編程方式添加想要顯示的視圖,或者您可以擁有一個帶有兩個數據集的自定義適配器和一個根據您要顯示的內容在兩個數據集之間切換的標誌。

更新:

一個另一種解決方案是添加你不想顯示,除非在特殊情況下,以列表的最末尾的項目,保持對它的引用總是顯示項目和索引在特殊情況下顯示項目。需要注意的是,這需要特別注意保持計數變量同步,並且具有防止您以自定義順序顯示項目的效果,即,除了始終顯示的項目和特殊情況項目以外的任何項目;至少沒有特殊的排序邏輯。