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

时间:2019-07-01 04:31:47

标签: java android xml android-listview

我正在尝试在listView为空时显示图像,但是不幸的是,每当列表为空时,它都不会显示该图像。

我试图将setEmptyView移入oncreate,然后再尝试检查游标是否为空,然后调用setemptyview方法

围栏活动 在此活动中,我尝试使用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;
    }
}

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

3 个答案:

答案 0 :(得分:1)

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);

还要在活动中定义emptyElement view

答案 1 :(得分:0)

尝试在设置适配器之前先设置空视图

    fenceAdapter = new FenceAdapter(FenceActivity.this, R.layout.list_layout_fences, fenceList);
    listViewFences.setEmptyView(findViewById(R.id.emptyElement));
    listViewFences.setAdapter(fenceAdapter);

答案 2 :(得分:0)

在您的代码中进行此更改-

typedef struct
{
    packed_float3 position;
    packed_float3 normal;
    packed_float2 texCoord1;
    packed_float3 tangent;
    packed_float3 bitagent;
    packed_float4 vertColor;
} vertex_t;

typedef struct
{
    float4 position [[position]] , vertexPosition_ws;
    float2 uv,texture2UV;
    float hasLighting;
    float pointSize [[point_size]];
    float vertexDepth, reflection;
    float hasMeshColor;
    float shadowDarkness, transparency;
    float2 pointCoord [[ point_coord ]];
    float4 eyeVec, lightDir, lightColor;
    float3 meshColor;
    float3 T;
    float3 B;
    float3 N;
} ColorInOut;

vertex ColorInOut Mesh_Vertex(device vertex_t* vertex_array [[ buffer(0) ]],
                                constant float4x4& vp [[ buffer(SHADER_MESH_mvp) ]],
                                constant MatArray* model [[ buffer(SHADER_MESH_model) ]],
                                constant float4x4& lvp [[ buffer(SHADER_MESH_lightViewProjMatrix) ]],
                                constant packed_float3& eyePos [[ buffer(SHADER_MESH_eyePos) ]],
                                constant float& shadowDarkness [[ buffer(SHADER_MESH_shadowDarkness) ]],
                                constant float *hasMeshColor [[ buffer(SHADER_MESH_hasMeshColor) ]],
                                constant float *reflectionValue [[ buffer(SHADER_MESH_reflectionValue) ]],
                                constant float *transparency [[ buffer(SHADER_MESH_transparency) ]],
                                constant float *hasLighting [[ buffer(SHADER_MESH_hasLighting) ]],
                                constant float *uvScaleValue [[ buffer(SHADER_MESH_uvScale) ]],
                                constant float3Struct *meshColor [[buffer(SHADER_MESH_meshColor)]],
                                unsigned int vid [[ vertex_id ]],
                                unsigned int iId [[ instance_id ]]
                                )
{

    float4 vertexPosition_ws = model[iId].data * float4(float3(vertex_array[vid].position), 1.0);
    float4 vertColor = float4(vertex_array[vid].vertColor);
    float3 tangent = float3(vertex_array[vid].tangent);
    float3 bitangent = float3(vertex_array[vid].bitagent);
    float3 normal = float3(vertex_array[vid].normal);

    ColorInOut out;
    out.hasMeshColor = hasMeshColor[iId];
    out.meshColor = (meshColor[iId].data[0] == -1.0) ? vertColor.xyz : float3(meshColor[iId].data);
    out.reflection = reflectionValue[iId];

    out.T = normalize(float3(model[iId].data * float4(tangent, 0.0)));
    out.B = normalize(float3(model[iId].data * float4(bitangent, 0.0)));
    out.N = normalize(float3(model[iId].data * float4(normal,  0.0)));

    out.vertexPosition_ws = vertexPosition_ws;
    out.transparency = transparency[iId];
    out.hasLighting = hasLighting[iId];
    out.position = vp * vertexPosition_ws;
    out.uv = vertex_array[vid].texCoord1 * uvScaleValue[iId];
    out.shadowDarkness = 0.0;

    if(bool(hasLighting[iId])) {
        float4 vertexLightCoord = lvp * vertexPosition_ws;
        float4 texCoords = vertexLightCoord/vertexLightCoord.w;
        out.texture2UV = float4((texCoords / 2.0) + 0.5).xy;
        out.texture2UV.y = (1.0 - out.texture2UV.y); // need to flip metal texture vertically
        out.vertexDepth = texCoords.z;
        out.shadowDarkness = shadowDarkness;
    }

    float4 eye_position_ws = vertexPosition_ws - float4(float3(eyePos), 1.0);
    out.eyeVec = normalize(eye_position_ws);

    return out;
}