【问题标题】:Custom ListVIew Is not displayed an empty screen is displaying自定义 ListVIew 不显示 正在显示一个空屏幕
【发布时间】:2023-03-22 08:30:01
【问题描述】:

当列表中没有项目时,不会出现空视图。有什么问题?

Main_Activity

包 com.example.android.customadapter;

public class MainActivity extends AppCompatActivity {

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


        ArrayList<Players> objects = new ArrayList<>();


        objects.add(new Players("Shikhar Dhawan",R.drawable.shikhar));
        objects.add(new Players("Virat Kohli",R.drawable.kohli));

    }
}

我的模型课

Players.java

包 com.example.android.customadapter;

public class Players {

    private String mName;
    private int image;


    Players(String name,int res)
    {
        mName = name;
        image = res;
    }

    public String getName()
    {
        return mName;
    }

    public int getImage()
    {
        return image;
    }
}

CustomAdapter 类

包 com.example.android.customadapter;

我的自定义数组列表适配器

public class CustomAdapter extends ArrayAdapter<Players> {

    public CustomAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Players> objects) {
        super(context, resource, objects);
    }


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

        View v = convertView;
        if(v==null)
        {
            v = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }

        Players p = getItem(position);

        TextView t = (TextView)v.findViewById(R.id.miwok_text_view);

        t.setText(p.getName());

        ImageView i = (ImageView)v.findViewById(R.id.image);

        i.setImageResource(p.getImage());


        return v;
    }

}

【问题讨论】:

  • 您没有在 OnCreate 中初始化您的 Listview,也没有为其设置适配器
  • Olena Y 感谢您的回复,但我该怎么做
  • CustomAdapter customAdapter = new CustomAdapter(this, words); ListView lv = (ListView) findViewById(R.id.list); lv.setAdapter(customAdapter );

标签: android listview custom-adapter


【解决方案1】:

我已经在main_activity的ArrayList末尾解决了 在里面。 OnCreate 方法中的 Listview 并将数组适配器设置为 listView

CustomAdapter 适配器 = new CustomAdapter(this, words);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);

【讨论】:

    【解决方案2】:

    从您的 activity_main.xml 中获取 listView。他们为该列表设置了适配器。更多详情:http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

    【讨论】:

      【解决方案3】:

      您收到错误是因为您没有从类中获取值。 这是我使用的代码

       @NonNull
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View view =convertView;
          LayoutInflater layoutInflater = LayoutInflater.from(getContext());
          if (convertView==null){
              view=layoutInflater.inflate(R.layout.customplaylist,parent,false);
          }
          Playlistfileinfo playlistfileinfo =(Playlistfileinfo)getItem(position);
          TextView textView =(TextView)view.findViewById(R.id.textView7);
          textView.setTypeface(typeface);
          textView.setText(playlistfileinfo.getName());
          return view;
      }
      

      你可以看到Playlistfileinfo playlistfileinfo =(Playlistfileinfo)getItem(position);希望这对你有帮助。

      【讨论】:

      • 感谢大家理解并解决了这个问题
      • 我忘记在 onCreate 方法中初始化列表视图,也忘记将数组适配器设置为列表视图
      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多