Android 如果arraylist中存在数据,如何设置可见性
我想检查数据库中是否存在特定的数据行。如果数据存在,则“设置可见性”模式可见并显示我的第一个图像如果数据库中不存在数据,则“设置可见性”不可见并显示我的第二个图像 这是我的密码Android 如果arraylist中存在数据,如何设置可见性,android,Android,我想检查数据库中是否存在特定的数据行。如果数据存在,则“设置可见性”模式可见并显示我的第一个图像如果数据库中不存在数据,则“设置可见性”不可见并显示我的第二个图像 这是我的密码 public class ListViewAdapter extends BaseAdapter { Context context; LayoutInflater inflater; ArrayList<Product> AllMenu = new ArrayList<>(); ImageLo
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;
}}
使用登录您的代码以确保您的代码正常工作,并使用equalsIgnoreCase,如@shark所说。像这样:
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 monitor中的日志结果,确保它始终返回“facility1包含池”以获得正确的设置可见性
我更喜欢在其自身中拆分检查“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;
}
致:
为什么不
equalsIgnoreCase()
?在我的数据库中,唯一的“池”是填充表中包含的冒号。如果数据包含null,则我希望可见性在输出中不可见。在比较字符串时,请使用String.equals(Object Object)
我可以使用谁。我现在不知道我是新安卓。好的,等等,我会试试这个。好的,先生,我会试试你的解决方案。谢谢,这段代码没有任何问题,只有第一个图像在我的listview中显示所有值。如何记录消息?。尝试将xml列表项中的所有图像可见性设置为“不可见”。@yogipuranik:阅读我的更新答案。您的代码中有错误。感谢您解决此问题。现在它工作正常了。
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;
}