首页 > 解决方案 > 在我单击 SearchView 之前,ListView 不会显示

问题描述

我的 ListView 仅在我单击/激活 SearchView 后才会显示,我想它会以某种方式刷新视图并显示 listview。

我尝试刷新(重建)视图和列表视图。

这是我的列表视图:

sviEventiListView = this.findViewById(R.id.ListListaEvenata);

        sviEventiListView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return eventiList.size();
            }

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

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

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                if (convertView == null) {

                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = (View) inflater.inflate(R.layout.eventistavka, null);
                }
                final Eventi row = eventiList.get(position);
                TextView eventiName = (TextView) convertView.findViewById(R.id.EventNaziv);
                eventiName.setText(row.getNaziv());
                TextView eventiDesc = (TextView) convertView.findViewById(R.id.OpisEventa);
                eventiDesc.setText(row.getOpis());



                return convertView;
            }

        });

它在这部分代码之后运行(从数据库中获取数据,它应该正常工作):

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);

        AndroidNetworking.enableLogging();


AndroidNetworking.get(Config.url+"api/Eventi").build().getAsJSONArray(new JSONArrayRequestListener() {
    @Override
    public void onResponse(JSONArray response) {
        Gson gson = new Gson();
        try {
            Log.d("Response:", response.getString(0));

            JSONArray array = response;
            for (int i = 0; i < array.length(); i++) {

                JSONObject myData = array.getJSONObject(i);
                JsonObject element = gson.fromJson(myData.toString(), JsonObject.class);
                Eventi eventi = gson.fromJson(element, Eventi.class);

                eventiList.add(eventi);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < eventiList.size(); i++) {
            Log.wtf("TEST2", "onResponse: " + eventiList.get(i).getNaziv());
            Log.wtf("TEST2", "onResponse: " + eventiList.get(i).getOpis());

        }

    }

    @Override
    public void onError(ANError anError) {

    }

});

这也是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainPageActivity"
    android:padding="10dp"
    >


    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/PretragaBarId"
        android:inputType="text"
        android:queryHint="Pretraga"
        />

    <TextView
        android:id="@+id/noItems"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/noResult"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="30dp"
        android:visibility="gone"/>
    <ListView
        android:id="@+id/ListListaEvenata"
        android:layout_below="@id/PretragaBarId"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

</RelativeLayout>

预期的结果是在顶部显示空搜索栏,并在运行应用程序后立即显示包含 3 个成员的列表,但它仅在我单击 SearchView 后显示。

在点击搜索视图之前

点击搜索视图后

是的,它在关闭搜索视图后仍然存在,我没有将列表视图分配给它。

这是我的声明:

public class MainPageActivity extends AppCompatActivity {

    private ListView sviEventiListView;
    private BaseAdapter adapter;
    //private int myLastVisiblePos;
    TextView noItemsTextView;
    private ProgressBar mainProgressBar;
    private List<Eventi> eventiList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) { 

标签: javaandroid

解决方案


在将数据加载到您的列表后,我看不到执行哪个方法,但我认为您需要使用:

  BaseAdapter myAdapter =new BaseAdapter;
  myAdapter...//configure it
  sviEventiListView.setAdapter(myAdapter);
   myAdapter.notifyDataSetChanged();//call this after loading data into list view

由于这个原因,您应该首先声明一个适配器对象。


推荐阅读