使用ArrayAdapter将视图添加到GridView

时间:2018-07-31 08:29:02

标签: java android view

我试图将视图添加到具有ArrayAdapter的网格视图中。逻辑是,如果数据库中没有条目,则会添加一个按钮,提示用户添加一个条目,该条目就是此按钮:

enter image description here

但是启动应用程序时,没有添加按钮。我也没有任何错误。这是我的代码:

FragmentFood.java

public class FragmentFood extends Fragment {
    LinearLayout layout;
    ArrayList<Items> food = new ArrayList<>();
    ItemsAdapter itemsAdapter;
    GridView gridView;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_food, container, false);
        layout = (LinearLayout)view.findViewById(R.id.layout);
        gridView = (GridView) view.findViewById(R.id.gridView);
        populateFoods();
        ItemsAdapter itemsAdapter = new ItemsAdapter(this.getContext(), food);
        gridView.setAdapter(itemsAdapter);
        return view;
    }

    private void populateFoods() {
        DBHelper dbHelper = new DBHelper(this.getContext());
        try {
            Cursor c = dbHelper.getAllData("food");
            if(c != null) {
                if(c.moveToFirst()) {
                    do {
                        String name = c.getString(c.getColumnIndex("name"));
                        String type = c.getString(c.getColumnIndex("type"));
                        double price = c.getDouble(c.getColumnIndex("price"));
                        food.add(new Items(name, type, price));
                    } while (c.moveToNext());
                }
            } else {
                Button btn = new Button(this.getContext());
                btn.setBackgroundResource(R.drawable.no_item);
                btn.setText("");
                gridView.addView(btn);
            }
        } catch (SQLiteException sql) {
            Log.e("error", " error sql", sql);
        }
    }
}

适配器

public class ItemsAdapter extends ArrayAdapter<Items> {


    public ItemsAdapter(Context context, ArrayList<Items> item) {
        super(context, 0, item);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Items 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.custom_button, parent, false);
        }
        // Lookup view for data population
        Button itemBtn = (Button)convertView.findViewById(R.id.itemBtn);
        // Populate the data into the template view using the data object
        itemBtn.setText(item.getName() + " " + item.getType());
        // Return the completed view to render on screen
        return convertView;
    }
}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

我的数据库中没有数据条目,因此应显示addItem按钮。我尝试将视图添加到gridview的父布局中,但是也没有按钮出现。请帮助

3 个答案:

答案 0 :(得分:0)

是的,因为您的适配器大小为零,所以适配器不会生成任何视图,因此会发生这种情况。在这种情况下,您需要在适配器中传递至少一项以显示按钮。如果不想在GridView下面的片段中放置类似的put按钮。

您可以像这样将View添加到网格视图:

    gridView = (GridView) view.findViewById(R.id.gridView);
    populateFoods();
    ItemsAdapter itemsAdapter = new ItemsAdapter(this.getContext(), food);
    gridView.setAdapter(itemsAdapter);
    View btnView = LayoutInflater.from(getContext()).inflate(R.layout.custom_button,null);
    gridView.addView(btnView);
    Button itemBtn = (Button)convertView.findViewById(R.id.itemBtn);

答案 1 :(得分:0)

在您的else代码部分中,您未设置按钮的宽度和高度,因此它不会显示,请尝试以下操作:-

      //set the properties for button

       Button btn = new Button(this.getContext());
       btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

       btn.setBackgroundResource(R.drawable.no_item);
       btn.setText("");
       gridView.addView(btn);

答案 2 :(得分:0)

添加 在arraylist中添加一个新项并调用adapter.notifydatasetchanged