如果列表中没有项目,则不会显示空白视图。问题是什么?

主要活动

包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;
    }

}

最佳答案

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

CustomAdapter adapter = new CustomAdapter(this,words);

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

    listView.setAdapter(adapter);

关于android - 未显示自定义ListVIew,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45549958/

10-10 03:55