簡體   English   中英

列表視圖不會顯示來自自定義陣列適配器的項目

[英]List view does not diplay items from custom array adapter

我已經為列表視圖創建了一個自定義數組適配器,但是由於某種原因,數據沒有顯示在列表視圖項中。 當我將log語句放入擴展ArrayAdapter的自定義適配器的getView方法中時,甚至連日志貓也什么都不顯示。 這是持有列表視圖的活動

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

嘗試將參數數組或集合傳遞給ArrayAdapter構造函數,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM