android studio网格视图在我运行应用程序时不显示

drnojrws  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(493)

我创建了一个java类,它应该显示如下内容:

但这是我运行应用程序时得到的结果:

它没有显示我想要的,androidstudio也没有显示logcat中的任何错误,所以我不知道为什么它不工作。
这是setsactivity类的代码:

package com.example.myquiz;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.Toolbar;

public class SetsActivity extends AppCompatActivity {
   private GridView sets_grid;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_sets);

       androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.set_toolbar);
       //setSupportActionBar(toolbar);

       String title = getIntent().getStringExtra("CATEGORY");
       getSupportActionBar().setTitle(title);

       getSupportActionBar().setDisplayHomeAsUpEnabled(true);

       sets_grid =  findViewById(R.id.sets_gridview);
       SetsAdapter adapter = new SetsAdapter(2);
       sets_grid.setAdapter(adapter);

   }

   @Override
   public boolean onOptionsItemSelected(@NonNull MenuItem item) {
      if(item.getItemId() == android.R.id.home)
      {
          SetsActivity.this.finish();
      }
       return super.onOptionsItemSelected(item);
   }
}

我还创建了一个适配器类setsadapter,所以我也要发布它的代码:

package com.example.myquiz;

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

public class SetsAdapter extends BaseAdapter {
   private int numOfSets;

   public SetsAdapter(int numOfSets) {
       this.numOfSets = numOfSets;
   }

   @Override
   public int getCount() {
       return 0;
   }

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

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

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

       View view;

       if (convertView == null) {
           view = LayoutInflater.from(parent.getContext()).inflate(R.layout.set_item_layout, parent, false);
       } else {
           view = convertView;
       }

       view.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(parent.getContext(),QuestionActivity.class);
               parent.getContext().startActivity(intent);
           }
       });

       ((TextView) view.findViewById(R.id.setNo_tv)).setText(String.valueOf(position + 1));

       return view;
}
}

下面是xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".SetsActivity"
   android:orientation="vertical">

   <androidx.appcompat.widget.Toolbar
       android:id="@+id/set_toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="@color/colorPrimary"
       android:theme="@style/ThemeOverlay.AppCompat.Dark"
       ></androidx.appcompat.widget.Toolbar>

   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Sets"
       android:textSize="26sp"
       android:textStyle="bold"
       android:padding="16dp" />

   <GridView
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:id="@+id/sets_gridview"
       android:layout_weight="1"
       android:gravity="center"
       android:horizontalSpacing="16dp"
       android:verticalSpacing="16dp"
       android:padding="16dp"
       android:columnWidth="100dp"
       android:numColumns="auto_fit"
       ></GridView>

</LinearLayout>

以下是xml文件集\项\布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="100dp"
   android:layout_height="100dp"
   android:orientation="vertical"
   android:gravity="center"
   android:background="@drawable/round_corner"
   android:backgroundTint="#FFC3C3"
   >
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="1"
       android:id="@+id/setNo_tv"
       android:textStyle="bold"
       android:textColor="@android:color/black"
       android:textSize="45sp"></TextView>
</LinearLayout>
jaql4c8m

jaql4c8m1#

这个 getCount() 中的方法 SetsAdapter 你应该回来 numOfSets 不是 0 .

@Override
 public int getCount() {
   return numOfSets;
 }

相关问题