我的片段中的列表视图未显示任何数据

时间:2019-05-27 02:43:59

标签: android android-fragments

我正在创建一个CategoryFragment,它将显示从我的活动中添加的所有数据。代码工作正常,数据已添加到数据库中,我已经在设备文件资源管理器中检查了数据,添加的数据在那里,但它们未显示在列表视图中。这是代码:

CategoriesFragment.java

package com.example.devcash.Fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;

import com.example.devcash.ADD_UI.AddCategoryActivity;
import com.example.devcash.CustomAdapters.CategoryAdapter;
import com.example.devcash.Database.DatabaseHelper;
import com.example.devcash.Lists.CategoryList;
import com.example.devcash.R;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class CategoriesFragment extends Fragment implements SearchView.OnQueryTextListener {

    DatabaseHelper db;
    ListView lv;
    CategoryAdapter categoryAdapter;

    public CategoriesFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_categories, container, false);

        //add floating action button
        FloatingActionButton categories_fab = view.findViewById(R.id.addcategories_fab);
        categories_fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // when add fab is pressed, go to add product activity
                Intent addcategory = new Intent(getActivity(), AddCategoryActivity.class);
                startActivity(addcategory);
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //handles listview
        ListView lvcategories = (ListView) view.findViewById(R.id.categorylist_listview);
        db = new DatabaseHelper(getActivity());


        ArrayList<CategoryList> categoryListArrayList = new ArrayList<CategoryList>();

        lvcategories.setAdapter(categoryAdapter);

        categoryAdapter = new CategoryAdapter(getActivity(), categoryListArrayList);
        categoryListArrayList = db.getAllCategory();

        getActivity().setTitle("Categories");
    }

    //handles the search menu
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.searchmenu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("Search..");

        super.onCreateOptionsMenu(menu, inflater);
    }   
    }

fragmentcategories.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.CategoriesFragment"
    android:padding="@dimen/text_padding">

        <ListView
            android:id="@+id/categorylist_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="1dp"/>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/addcategories_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add"
        android:tint="@color/whiteBG"
        android:backgroundTint="@color/colorPrimary"/>

</FrameLayout>

CategoryAdapter.java

 package com.example.devcash.CustomAdapters;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

    import com.example.devcash.Lists.CategoryList;
    import com.example.devcash.R;

    import java.util.ArrayList;

    public class CategoryAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<CategoryList> categoryListArrayList;

    public CategoryAdapter(Context context, ArrayList<CategoryList> categoryListArrayList) {
        this.context = context;
        this.categoryListArrayList = categoryListArrayList;
    }

    @Override
    public int getCount() {
        return categoryListArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryListArrayList.get(position);
    }

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

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

        ViewHolder holder;

        if(convertView == null){
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.category_customlayout, null, true);

            holder.textCategoryName = (TextView) convertView.findViewById(R.id.txtcategory_name);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        //display the data into the CategoryFragment
        holder.textCategoryName.setText(categoryListArrayList.get(position).getCategory_name());

        return convertView;
    }

    private class ViewHolder{
        protected TextView textCategoryName;
    }
}

2 个答案:

答案 0 :(得分:1)

问题是您正在为适配器设置一个空列表,并且在第一次实例化它之前,请检查您的列表是否不为空,然后调用notifyDataSetChanged()

ArrayList<CategoryList> categoryListArrayList = new ArrayList<CategoryList>();
categoryAdapter = new CategoryAdapter(getActivity(), categoryListArrayList);
lvcategories.setAdapter(categoryAdapter);
categoryListArrayList = db.getAllCategory();
categoryAdapter.notifyDataSetChanged();

修改

首先,您应该在适配器构造函数中传递上下文而不是活动

categoryAdapter = new CategoryAdapter(getActivity().getApplicationContext(), categoryListArrayList);

然后在您的适配器中尝试将getview()更改为此

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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.category_customlayout, parent, true);

        TextView textCategoryName = (TextView) convertView.findViewById(R.id.txtcategory_name);  
        textCategoryName.setText(categoryListArrayList.get(position).getCategory_name());
    return convertView;
}

答案 1 :(得分:0)

首先,您需要重新排列此代码

ArrayList<CategoryList> categoryListArrayList = new ArrayList<CategoryList>(); 
lvcategories.setAdapter(categoryAdapter); 
categoryAdapter = new CategoryAdapter(getActivity(), categoryListArrayList);
categoryListArrayList = db.getAllCategory();

收件人

ArrayList<CategoryList> categoryListArrayList = new ArrayList<CategoryList>(); 
categoryListArrayList = db.getAllCategory();
categoryAdapter = new CategoryAdapter(getActivity(), categoryListArrayList);
lvcategories.setAdapter(categoryAdapter); 

并且在用于扩展视图的适配器代码中,您需要传递不为null的父值。

使用此

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_customlayout, parent, false); // You just pass null instead of parent

代替

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_customlayout, null, true);

希望这会有所帮助。