Android - 带有sqlite数据的listview自定义适配器

时间:2018-04-10 01:21:27

标签: android sqlite listview

我想在listview中显示sqlite数据。我使用自定义适配器来显示图像,名称和条形码数据。但是在创建活动时,它不显示数据,只显示布局,然后当我滚动列表视图时,它现在显示数据请参阅下面的代码。

MainActivity.java

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

    listView = (ListView)findViewById(R.id.lv_personnel);
    list = new ArrayList <>();
    adapter = new PersonnelAdapter(this, R.layout.personnel_list, list);
    listView.setAdapter(adapter);

    myDB = new DatabaseHelper(this);

    //ArrayList<String> theList = new ArrayList<>();
    Cursor res = myDB.getPersonnelList();
    list.clear();


    if(res.getCount() == 0){
        Toast.makeText(this,"No data", Toast.LENGTH_LONG).show();
    }else {
        while (res.moveToNext()){
            int ID = res.getInt(0);
            String name = res.getString(2);
            String barcode = res.getString(1);
            byte[] img = res.getBlob(5);

            list.add(new Personnel(ID, barcode, name, img));
            //ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,theList);
            //listView.setAdapter(listAdapter);
        }
        adapter.notifyDataSetChanged();
    }

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            //myDB = PersonnelAdapter.getItem(position);
            startActivity(new Intent(User.this, PersonnelDetails.class));
        }
    });
}

这是我的自定义适配器

public class PersonnelAdapter extends BaseAdapter{
private Context context;
private int layout;
private ArrayList<Personnel> personnelList;

public PersonnelAdapter(Context context, int layout, ArrayList<Personnel> personnelList) {
    this.context = context;
    this.layout = layout;
    this.personnelList = personnelList;
}

@Override
public int getCount() {
    return personnelList.size();
}

@Override
public Object getItem(int position) {
    return personnelList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

private class ViewHolder {
    ImageView imageView;
    TextView textName, textBarcode;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {

    View row = view;
    ViewHolder holder = new ViewHolder();

    if(row == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layout, null);

        holder.textName = (TextView) row.findViewById(R.id.txtName);
        holder.textBarcode = (TextView) row.findViewById(R.id.txtBarcode);
        holder.imageView = (ImageView) row.findViewById(R.id.iv_avatar);
        row.setTag(holder);
    }else {
        holder = (ViewHolder) row.getTag();

        Personnel personnel = personnelList.get(position);

        holder.textName.setText(personnel.getName());
        holder.textBarcode.setText(personnel.getBarcode());
        byte[] personnelImage = personnel.getImg();
        Bitmap bitmap = BitmapFactory.decodeByteArray(personnelImage, 0, personnelImage.length);
        holder.imageView.setImageBitmap(bitmap);

    }
    return row;
}
}

我的DatabaseHelper获取数据

public Cursor getPersonnelList(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " +TABLE_NAME, null);
    return res;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试更改:

}else {
    holder = (ViewHolder) row.getTag();

    Personnel personnel = personnelList.get(position);

    holder.textName.setText(personnel.getName());
    holder.textBarcode.setText(personnel.getBarcode());
    byte[] personnelImage = personnel.getImg();
    Bitmap bitmap = BitmapFactory.decodeByteArray(personnelImage, 0, personnelImage.length);
    holder.imageView.setImageBitmap(bitmap);

}

}else {
    holder = (ViewHolder) row.getTag(); 

}

    Personnel personnel = personnelList.get(position);

    holder.textName.setText(personnel.getName());
    holder.textBarcode.setText(personnel.getBarcode());
    byte[] personnelImage = personnel.getImg();
    Bitmap bitmap = BitmapFactory.decodeByteArray(personnelImage, 0, personnelImage.length);
    holder.imageView.setImageBitmap(bitmap);

我希望它可以帮助你解决问题!