简体   繁体   中英

List view does not diplay items from custom array adapter

i have created a custom array adapter for my list view but for some reason the data is not getting displayed in listview item. even the log cat doesnt show anything when i put log statement in getView method of the custom adapter which extends ArrayAdapter. This is the activity that holds listview

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

   } 
}

this is my custom adapter

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

try to pass the parameter array or collection to ArrayAdapter constructor, Looks like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM