如果arraylist

时间:2016-06-22 13:07:22

标签: android

我想检查数据库中是否存在特定的数据行。如果存在数据,则设置可见性模式是可见的,如果数据库中不存在数据则显示我的第一个图像,然后设置可见性是不可见的并显示我的第二个图像。

这是我的代码。

public class ListViewAdapter extends BaseAdapter {

Context context;
LayoutInflater inflater;
ArrayList<Product> AllMenu = new ArrayList<>();
ImageLoader imageLoader;
SQLiteDatabase sqLite;

public ListViewAdapter(Context context, ArrayList<Product> itemlist) {
    this.context=context;
    AllMenu = itemlist;
    imageLoader = new ImageLoader(context);
}

public int getCount() {
    return AllMenu.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

public View getView(final int position, final View convertView, ViewGroup parent) {
    // Declare Variables
    Product tempMenu = AllMenu.get(position);
    ImageView image_path,facility1,facility_1;
    TextView name,location,desc,facility2,facility3,facility4;
    ListView listView;

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View view = inflater.inflate(R.layout.viewpage, parent, false);
    // Get the position
    //listView = (ListView) view.findViewById(R.id.myimagelist);
    name = (TextView) view.findViewById(R.id.fh_name);
    location = (TextView) view.findViewById(R.id.fh_loc);
    desc = (TextView) view.findViewById(R.id.fh_desc);
    facility1 = (ImageView) view.findViewById(R.id.fh_fc1);
    facility_1 = (ImageView) view.findViewById(R.id.fh_fc11);
    image_path = (ImageView) view.findViewById(R.id.image_all_main);

    name.setText(tempMenu.getName());
    location.setText(tempMenu.getLocation());
    desc.setText(tempMenu.getDescription());
    for(Product myPoint : AllMenu) {
            if(myPoint.getFacility1() != null && myPoint.getFacility1().contains("Pool")) {
                facility1.setVisibility(view.VISIBLE);
                facility_1.setVisibility(view.INVISIBLE);
            }else {
                facility_1.setVisibility(view.VISIBLE);
                facility1.setVisibility(view.INVISIBLE);
            }
    }
    imageLoader.DisplayImage(tempMenu.getImage_path(),image_path);
    return view;
}}

所有数据都应来自服务器。请帮我解决这个问题。谢谢 我想与我的字符串进行比较,这是&#34; Pool&#34;是上面代码中的字符串。 如果给定string == pool,则设置Visibile或设置Invisible。 我只想在这里进行更改。

for(Product myPoint : AllMenu) {
        if(myPoint.getFacility1() != null && myPoint.getFacility1().contains("Pool")) {
            facility1.setVisibility(view.VISIBLE);
            facility_1.setVisibility(view.INVISIBLE);
        }else {
            facility_1.setVisibility(view.VISIBLE);
            facility1.setVisibility(view.INVISIBLE);
        }
}

1 个答案:

答案 0 :(得分:0)

在代码中使用log来确保您的比较代码正常工作,并使用像@shark这样的equalsIgnoreCase。像这样:

for(Product myPoint : AllMenu) {
   Log.d("LIST_VIEW_ADAPTER", myPoint.getFacility1());
   if(myPoint.getFacility1() != null && myPoint.getFacility1().equalsIgnoreCase("Pool")) {
     facility1.setVisibility(view.VISIBLE);
     facility_1.setVisibility(view.INVISIBLE);
     Log.d("LIST_VIEW_ADAPTER", "facility1 contains Pool");
   }else {
     facility_1.setVisibility(view.VISIBLE);
     facility1.setVisibility(view.INVISIBLE);
     Log.d("LIST_VIEW_ADAPTER", "facility1 no Pool!");  
   }
}

检查android监视器中的日志结果,并确保它始终返回“facility1 contains Pool”以获得正确的设置可见性。

我喜欢在它自己中分割检查'null'值,如下所示:

for(Product myPoint : AllMenu) {
  Log.d("LIST_VIEW_ADAPTER", myPoint.getFacility1());
  if(myPoint.getFacility1() != null) {
    if(myPoint.getFacility1().equalsIgnoreCase("Pool")) {
      facility1.setVisibility(view.VISIBLE);
      facility_1.setVisibility(view.INVISIBLE);
      Log.d("LIST_VIEW_ADAPTER", "facility1 contains Pool");
    } else {
      facility_1.setVisibility(view.VISIBLE);
      facility1.setVisibility(view.INVISIBLE);
      Log.d("LIST_VIEW_ADAPTER", "facility1 no Pool!");  
    }
  } else {
    // do something if null here.
     Log.d("LIST_VIEW_ADAPTER", "NULL Value!");
  }     
}

- 更新 -

您应该在以下代码中返回位置值:

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

致:

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}