我想显示图片+电话号码。我已经为联系人创建了一个类:
public class Contact {
private int CTC_ID;
private byte[] CTC_IMAGE;
public String CTC_NOM;
public String CTC_NUMERO;
public Contact(){
}
public Contact(byte[] CTC_IMAGE, String CTC_NOM, String CTC_NUMERO){
this.CTC_IMAGE = CTC_IMAGE;
this.CTC_NOM = CTC_NOM;
this.CTC_NUMERO = CTC_NUMERO;
}
public Contact(String CTC_NOM, String CTC_NUMERO){
this.CTC_NOM = CTC_NOM;
this.CTC_NUMERO = CTC_NUMERO;
}
public int getCTC_ID(){
return CTC_ID;
}
public byte[] getCTC_IMAGE(){return CTC_IMAGE; }
public String getCTC_NOM() {
return CTC_NOM;
}
public String getCTC_NUMERO() {
return CTC_NUMERO;
}
public void setCTC_ID(int CTC_ID) {
this.CTC_ID = CTC_ID;
}
public void setCTC_IMAGE(byte[] CTC_IMAGE) { this.CTC_IMAGE = CTC_IMAGE;}
public void setCTC_NOM(String CTC_NOM) { this.CTC_NOM = CTC_NOM;}
public void setCTC_NUMERO(String CTC_NUMERO) {
this.CTC_NUMERO = CTC_NUMERO;
}
public String toString(){
return "ID : "+CTC_ID+"\nimage : "+CTC_IMAGE+"\nnom : "+CTC_NOM+"\nnumero : "+CTC_NUMERO;
}
}
要将联系人添加到列表中,我在methode fetchAllContactDetail中有我的databaseHelper:
public ArrayList<Contact> fetchAllContactDetail() {
SQLiteDatabase db = this.getWritableDatabase();
ArrayList<Contact> contactListe = new ArrayList<>();
Cursor mCursor = db.query(TABLE_CONTACT, new String[]{"CTC_ID _id", KEY_CTC_IMAGE, KEY_CTC_IMAGE, KEY_CTC_NOM, KEY_CTC_NUMERO}, null, null, null, null, null);
if (mCursor.moveToFirst()) {
do {
byte[] image = mCursor.getBlob(mCursor.getColumnIndex(KEY_CTC_IMAGE));
String name = mCursor.getString(mCursor.getColumnIndex(KEY_CTC_NOM));
String numero = mCursor.getString(mCursor.getColumnIndex(KEY_CTC_NUMERO));
contactListe.add(new Contact(image, name, numero));
} while (mCursor.moveToNext());
}
return contactListe;
}
我创建了ContactAdapter extends BaseAdapter:
public class ContactAdapter extends BaseAdapter {
private Context context;
private ArrayList<Contact> contactList = new ArrayList<Contact>();
// Constructor
public ContactAdapter(Context context, ArrayList<Contact> contactList) {
this.context = context;
this.contactList.addAll(contactList);
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView img;
TextView name;
}
@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(R.layout.contact_item, null);
}
Contact contact = contactList.get(position);
if (contactList != null) {
holder.name = (TextView) row.findViewById(R.id.name);
holder.img = (ImageView) row.findViewById(R.id.img);
holder.name.setText(contact.getCTC_NUMERO());
byte[] contactImage = contact.getCTC_IMAGE();
Bitmap bitmap = BitmapFactory.decodeByteArray(contactImage, 0, contactImage.length);
holder.img.setImageBitmap(bitmap);
row.setTag(holder);
}
return row;
}
}
我在mainActivity中将其返回:
public class MainActivity extends AppCompatActivity {
private ListView lstViewContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstViewContact = (ListView) findViewById(R.id.lvContact);
displayContact();
}
public void displayContact() {
//On ouvre la base de données pour écrire dedans
DatabaseHelper dbHelper = new DatabaseHelper(this);
final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.fetchAllContactDetail());
lstViewContact.setAdapter(contactAdapter);
}
}
我的数据库有2个联系人,它通过contactListe返回
在我的ContactAdapter中
@Override
public int getCount() {
return contactList.size();
}
返回2没关系。
我没有说出问题出在哪里。
谢谢你的帮助。
答案 0 :(得分:0)
替换
ViewHolder holder = new ViewHolder();
if(row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_item, null);
}
带
ViewHolder holder;
if(row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_item, viewGroup, false);
holder = new ViewHolder();
// TODO: Initialise holder values, like
// holder.name = row.findViewById(R.id.name);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
答案 1 :(得分:0)
您的问题出在您的getView()实现
中@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = null;
if(row == null){
holder=new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_item, null);
holder.name = (TextView) row.findViewById(R.id.name);
holder.img = (ImageView) row.findViewById(R.id.img);
row.setTag(holder);
}else{
holder=row.getTag();}
Contact contact = contactList.get(position);
if (contactList != null) {
holder.name.setText(contact.getCTC_NUMERO());
byte[] contactImage = contact.getCTC_IMAGE();
Bitmap bitmap = BitmapFactory.decodeByteArray(contactImage, 0, contactImage.length);
holder.img.setImageBitmap(bitmap);
}
return row;
}
这是关于持有人初始化问题。
粘贴上面的代码并尝试。