当listView为空时正确显示消息

时间:2015-04-15 00:39:57

标签: android listview searchview

我有一个listView,在我的Android应用程序和listView上面填充了我现有的数据库,我有一个searchView,这样我就可以输入内容然后查询数据库,然后在listView中显示这些结果。首先,在我输入任何内容之前,我会在其中显示数据库中的所有项目。但是当我在searchView中放入一些东西时,它不会向我提供任何结果,我点击空白屏幕,屏幕崩溃,但它不应该这样做。我想要能够点击它我想要的所有内容,但也显示一条消息说没有找到结果。此外,当我清除searchView时,我想要我的原始结果,所有项目再次显示。我在下面发布了我的代码,所以任何帮助都会很棒。

shirtsActivity.java

package ankitkaushal.app.healthysizing;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.ArrayList;

public class shirtsActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shirts);

        final SearchView shirtViewShirts = (SearchView) findViewById(R.id.searchView3);
        final DatabaseHelper dbhelper;
        final ListView listView;
        final ListAdapter shirtsAdapter;

        dbhelper = new DatabaseHelper(getApplicationContext());
        try {
            dbhelper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }

        listView = (ListView) findViewById(R.id.listViewShirts);
        //List<Item> shirtsList = dbhelper.getAllShirts();
        ArrayList<Item> shirtsList = dbhelper.getAllShirts();

        if (shirtsList != null) {
            //shirtsAdapter = new ArrayAdapter<Item>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, shirtsList);
            shirtsAdapter = new ListItemAdapter(getApplicationContext(), shirtsList);
            listView.setAdapter(shirtsAdapter);
        }

        shirtViewShirts.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {

                query = query.toLowerCase();
                query = Character.toString(query.charAt(0)).toUpperCase()+query.substring(1);

                ListAdapter searchedShirtsAdapter;
                Log.e("Brand: ", query);
                ArrayList<Item> searchedShirtsList = dbhelper.getAllSearchedShirts(query);

                if (searchedShirtsList != null) {
                    searchedShirtsAdapter = new ListItemAdapter(getApplicationContext(), searchedShirtsList);
                    listView.setAdapter(searchedShirtsAdapter);
                }

                else if (searchedShirtsList.isEmpty()) {
                    searchedShirtsAdapter = new ListItemAdapter(getApplicationContext(), searchedShirtsList);
                    listView.setAdapter(searchedShirtsAdapter);
                }

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }

        });

    }

}

listItemAdapter.java

package ankitkaushal.app.healthysizing;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public final class ListItemAdapter extends ArrayAdapter<Item> implements View.OnClickListener{

    public ListItemAdapter(Context context, ArrayList<Item> shirtItems) {
        super(context, 0, shirtItems);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        Item item = getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout_shirts, parent, false);
        }

        // Lookup view for data population
        TextView brand = (TextView) convertView.findViewById(R.id.txt_shirt_brand);
        TextView price = (TextView) convertView.findViewById(R.id.txt_shirt_price);
        TextView store = (TextView) convertView.findViewById(R.id.txt_shirt_store);
        TextView description = (TextView) convertView.findViewById(R.id.txt_shirt_description);

        // Populate the data into the template view using the data object
        brand.setText("Brand:" + "  " + item.getBrand());
        price.setText("Price:" + "  " + item.getPrice());
        store.setText("Store:" + "  " + item.getStore());
        description.setText("Description:" + "  " + item.getDescription());

        // Return the completed view to render on screen
        return convertView;

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

activity_shirts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#29A9D2"
    android:weightSum="1"
    android:id="@+id/shirt"
    android:onClick="onClickSearch">

    <SearchView
        android:layout_width="352dp"
        android:layout_height="wrap_content"
        android:id="@+id/searchView3"
        android:background="#ffffffff"
        android:queryHint="Search for a specific brand" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listViewShirts"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true" />

        <TextView
            android:id="@+id/list_empty_shirts"
            android:text="No Results Found"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:textColor="#FFFFFF"
            android:gravity="center"
            android:textSize="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:visibility="invisible" />

    </RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

首先,在xml文件中创建一个纯文本视图,其中包含您的活动所需的消息。

然后尝试添加:

listView.setEmptyView(findViewById(R.id.___);

使用您创建的textview的ID填写此行的___部分。