【问题标题】:List view does not diplay items from custom array adapter列表视图不显示来自自定义数组适配器的项目
【发布时间】:2019-02-24 09:56:40
【问题描述】:

我为我的列表视图创建了一个自定义数组适配器,但由于某种原因,数据没有显示在列表视图项中。当我将日志语句放在扩展 ArrayAdapter 的自定义适配器的 getView 方法中时,即使日志猫也没有显示任何内容。 这是持有listview的activity

public class booktable extends AppCompatActivity {
    ListView mylv;
    int[] array = new int[5];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booktable);
        mylv = findViewById(R.id.mylv);
        array = new int[]{1, 0, 0, 0, 1};

        ListAdapter la = new customadapter(getApplicationContext(),R.layout.activity_booktable,array);
        mylv.setAdapter(la);

   } 
}

这是我的自定义适配器

public class customadapter extends ArrayAdapter {
    LayoutInflater li;
    int[] table = new int[5];

    public customadapter(@NonNull Context context,int resource, int [] array) {
        super(context,resource);
        li = LayoutInflater.from(context);
        table = array;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View v  = li.inflate(R.layout.custom_row,parent);
        ImageView img = v.findViewById(R.id.img);
        TextView tv = v.findViewById(R.id.tv);
        if(table[position]==0)
        {
            tv.setText("FULL");
            tv.setTextColor(Color.RED);
        }
        if(table[position]==1)
        {
            tv.setText("AVAILABLE");
            tv.setTextColor(Color.GREEN);
        }
        return v;
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    尝试将参数数组或集合传递给 ArrayAdapter 构造函数,如下所示:

    public customadapter(@NonNull Context context,int resource, int [] array) {
        super(context, resource, array);
        li = LayoutInflater.from(context);
        table = array;
    }
    

    【讨论】:

    • 是的,我试过了..不仅我什至放了一个硬编码文本,但它甚至不显示..我什至把日志语句放在 getview 方法中,即使它没有出现在 logcat 中
    猜你喜欢
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多