gpt4 book ai didi

java - 当 listView 为空时,setEmptyView 不显示图像

转载 作者:行者123 更新时间:2023-11-29 23:02:32 25 4
gpt4 key购买 nike

我试图在 listView 为空时显示图像,但不幸的是,只要列表为空,它就不会在 ListView 为空时显示该图像。

我尝试将 setEmptyView 移动到 oncreate,在此之前我尝试检查光标是否为空,如果为空则调用 setemptyview 方法

栅栏 Activity 在此 Activity 中,我尝试使用 setEmptyView 方法。

public class FenceActivity extends AppCompatActivity {
List<Fence> fenceList;
SQLiteDatabase sqLiteDatabase;
ListView listViewFences;
FenceAdapter fenceAdapter;
DataBaseHelper dataBaseHelper;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.savedfences);

listViewFences = findViewById(R.id.fencesListView);
fenceList = new ArrayList<>();

showFencesFromDatabase();
}

public void showFencesFromDatabase() {

dataBaseHelper = new DataBaseHelper(this);
Cursor cursor = dataBaseHelper.getAllData();
if (cursor.moveToFirst()) {
do {
fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
} while (cursor.moveToNext());
}
cursor.close();
fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
listViewFences.setAdapter(fenceAdapter);

listViewFences.setEmptyView(findViewById(R.id.emptyElement));
}

public void reloadFencesFromDatabase() {
dataBaseHelper = new DataBaseHelper(this);
Cursor cursor = dataBaseHelper.getAllData();
fenceList.clear();
if (cursor.moveToFirst()) {
do {
fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
} while (cursor.moveToNext());
}
cursor.close();
fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
listViewFences.setAdapter(fenceAdapter);
listViewFences.setEmptyView(findViewById(R.id.emptyElement));
}
}

Savedfences.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ListView
android:id="@+id/fencesListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ImageView
android:id="@+id/emptyElement"
android:layout_width="200dp"
android:layout_height="200dp"
android:dividerHeight="1dp"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

适配器类

public class FenceAdapter extends ArrayAdapter<Fence> {

Context context;
int listLayoutRes;
List<Fence> fenceList;
DataBaseHelper dataBaseHelper;
FenceActivity fenceActivity;

public FenceAdapter(Context context, int listLayoutRes, List<Fence> fenceList) {
super(context, listLayoutRes, fenceList);
this.context = context;
this.listLayoutRes = listLayoutRes;
this.fenceList = fenceList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_layout_fences, null);
}


final Fence fence = fenceList.get(position);

TextView textViewSno = convertView.findViewById(R.id.textViewSnoLabel);
TextView textViewLat = convertView.findViewById(R.id.textViewLatitudeValue);
TextView textViewLon = convertView.findViewById(R.id.textViewLongitudeValue);
TextView textViewRadi = convertView.findViewById(R.id.textViewRadiusValue);

textViewSno.setText(Integer.toString(fence.getSno()));
textViewLat.setText(String.valueOf(fence.getLat()));
textViewLon.setText(String.valueOf(fence.getLon()));
textViewRadi.setText(Integer.toString(fence.getRadius()));

Button buttonDel = convertView.findViewById(R.id.buttonDeleteFence);

buttonDel.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Are you sure");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dataBaseHelper = new DataBaseHelper(context);
fenceActivity = (FenceActivity)context;
dataBaseHelper.deleteDataById(fence);
fenceActivity.reloadFencesFromDatabase();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
return convertView;
}
}

当列表为空时,我可以进行哪些更改以显示该图像。

最佳答案

public class FenceActivity extends AppCompatActivity {
List<Fence> fenceList;
SQLiteDatabase sqLiteDatabase;
ListView listViewFences;
FenceAdapter fenceAdapter;
DataBaseHelper dataBaseHelper;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.savedfences);

listViewFences = findViewById(R.id.fencesListView);
fenceList = new ArrayList<>();

showFencesFromDatabase();
}

public void showFencesFromDatabase() {

dataBaseHelper = new DataBaseHelper(this);
Cursor cursor = dataBaseHelper.getAllData();
if (cursor.moveToFirst()) {
do {
fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
} while (cursor.moveToNext());
}
cursor.close();
fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
listViewFences.setAdapter(fenceAdapter);

listViewFences.setEmptyView(findViewById(R.id.emptyElement));
}

public void reloadFencesFromDatabase() {
dataBaseHelper = new DataBaseHelper(this);
Cursor cursor = dataBaseHelper.getAllData();
fenceList.clear();
if (cursor.moveToFirst()) {
do {
fenceList.add(new Fence(cursor.getInt(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getInt(3)));
} while (cursor.moveToNext());
}
cursor.close();
fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
listViewFences.setAdapter(fenceAdapter);
listViewFences.setEmptyView(findViewById(R.id.emptyElement));
}
}

代替 listViewFences.setEmptyView(findViewById(R.id.emptyElement)); 添加这个

listViewFences.setVisibility(fenceList.size>0?View.VISIBLE:View.GONE);
emptyElement.setVisibility(fenceList.size>0?View.GONE:View.VISIBLE);

同时在 Activity 中定义 emptyElement view

关于java - 当 listView 为空时,setEmptyView 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56830018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com