如何在创建列表视图时停止显示的列表项?

时间:2016-07-15 13:19:59

标签: android listview custom-adapter

我正在开发一个应用程序,它将显示包含其名称和价格的包列表。所有数据[包裹的名称和价格]都来自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());
    }}

5 个答案:

答案 0 :(得分: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。我认为它会完成这项工作。

答案 1 :(得分:0)

您应该首先存储包,然后将所有包排序为0,然后将它们发送到列表中。

答案 2 :(得分:0)

在传递给ListView之前创建一个新的List并存储仅具有值&gt;的数据0然后将该列表传递给listView

答案 3 :(得分: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 

答案 4 :(得分:0)

最佳解决方案是将项目添加到要显示的列表中,然后以编程方式添加要在所需时间显示的视图,或者您可以拥有一个包含两个数据集的自定义适配器和一个在两个数据之间切换的标记根据你想要展示的内容进行设置。

更新

另一种解决方案是将您不想要显示的项目添加到列表的最后,除非在特殊情况下,并保留对始终显示的项目的索引和特殊情况项目下的显示的引用。需要注意的是,这需要特别注意保持计数变量同步,并且具有阻止您以自定义顺序显示项目的效果,即除了始终显示的项目和特殊情况项目以外的任何其他项目;至少没有特殊的排序逻辑。