在我的应用程序中,我有一个listView,显示员工,每个项目都有自己的名字,电子邮件和缩略图。一切正常,但缩略图。我的ArrayAdapter不是将所有员工个人资料图片显示为缩略图,而是这样:
"android.resource://com.witchking.modiryar/" + R.drawable.default_pic;
这张照片是为了让您更好地了解我所面对的情况:
我的适配器问题:
这是我的代码:
ArrayAdapter getView()方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
String path ="android.resource://com.witchking.modiryar/" + R.drawable.default_pic;
Uri uri = null;
if (myView == null)
{
LayoutInflater layoutInflater;
layoutInflater = LayoutInflater.from(getContext());
myView = layoutInflater.inflate(R.layout.item_listviewitem, null);
}
PersonHandler myPerson = getItem(position);
if (myPerson != null){
TextView firstLine = (TextView) myView.findViewById(R.id.firstLine);
TextView secondLine = (TextView) myView.findViewById(R.id.secondLine);
ImageView profilePicture = (ImageView) myView.findViewById(R.id.icon);
if (myPerson.getPersonProfilePictureUrl() != null){
uri = Uri.parse(myPerson.getPersonProfilePictureUrl());
}else {
uri = Uri.parse(myPerson.getPersonProfilePictureUrl());
}
firstLine.setText(myPerson.getPersonFullName());
profilePicture.setImageURI(uri);
switch (personType)
{
case 'S': //person type student
secondLine.setText(myPerson.getPersonStudentNumber());
break;
case 'E': //person type employee
secondLine.setText(myPerson.getPersonEmail());
break;
case 'M': //person type Masters
secondLine.setText(myPerson.getPersonEmail());
break;
}
}
return myView;
}
PersonHandler
是一个基于不同活动的人员类型处理查询的类。这是getPersonList()方法:
public List<PersonHandler> getPersonList(SQLiteDatabase sqldb, char personType){
Cursor cursor;
String fullName = null;
String stdNumber = null;
String email = null;
String query = null;
String path = "android.resource://com.witchking.modiryar/" + R.drawable.exclamation;
String[] columnName = new String[3];
List<PersonHandler> listPerson=new ArrayList<PersonHandler>();
/*
* dar in ghesmat query motenaseb ba personType
* ejra migardad
* query selected by person type
*/
switch (personType) {
case 'S':
query = "select * from " + "tbl_Students";
break;
case 'E':
query = "select * from " + "tbl_Employees";
break;
case 'M':
query = "select * from " + "tbl_Master";
break;
}
cursor = sqldb.rawQuery(query, null);
/*
* dar in ghesmat barname check mikonad ke agar cursor khali bood
* yani hich daneshjoo, karmand, ya ostadi add nashode
* pas bar asase personType tasmim migirad ke peyghame monaseb ra namayesh dahad
* checking if cursor is null and displaying proper message
* the message is added as an item in list
*/
int cnt = cursor.getCount();
if (cursor.getCount() <= 0){
PersonHandler newPerson=new PersonHandler();
newPerson.setPersonProfilePictureUrl(path);
switch (personType) {
case 'S':
fullName = "هیچ دانشجویی اضافه نشده";
stdNumber = "از منو گزینه دانشجوی جدید را انتخاب نمایید";
newPerson.setPersonFullName(fullName);
newPerson.setPersonStudentNumber(stdNumber);
break;
case 'E':
fullName = "هیچ کارمندی اضافه نشده";
email = "از منو گزینه کارمند جدید را انتخاب نمایید";
newPerson.setPersonFullName(fullName);
newPerson.setPersonEmail(email);
break;
case 'M':
fullName = "هیچ استادی اضافه نشده";
email = "از منو گزینه استاد جدید را انتخاب نمایید";
newPerson.setPersonFullName(fullName);
newPerson.setPersonEmail(email);
break;
}
listPerson.add(newPerson);
/*
* dar in ghesmat bar asase personType barname tasmim migirad
* ke field haye monasb ra bar asase query voroodi az cursor
* farakhani konad
* if cursor is not null, app selects proper columns based on person type
*/
}else {
cursor.moveToFirst();
while (!cursor.isAfterLast()){
PersonHandler newPerson=new PersonHandler();
switch (personType) {
case 'S':
newPerson.setPersonFirstName(cursor.getString(1)); //First Name Column
newPerson.setPersonLastName(cursor.getString(2)); //Last Name Column
newPerson.setPersonEmail(cursor.getString(5)); //Email or StudentNumber Column
newPerson.setPersonProfilePictureUrl(cursor.getString(7)); //Picture Url Column
break;
case 'E':
newPerson.setPersonFirstName(cursor.getString(1)); //First Name Column
newPerson.setPersonLastName(cursor.getString(2)); //Last Name Column
newPerson.setPersonEmail(cursor.getString(3)); //Email or StudentNumber Column
newPerson.setPersonProfilePictureUrl(cursor.getString(7)); //Picture Url Column
break;
case 'M':
newPerson.setPersonFirstName(cursor.getString(1)); //First Name Column
newPerson.setPersonLastName(cursor.getString(2)); //Last Name Column
newPerson.setPersonEmail(cursor.getString(3)); //Email or StudentNumber Column
newPerson.setPersonProfilePictureUrl(cursor.getString(7)); //Picture Url Column
break;
}
newPerson.setPersonFullName(newPerson.getPersonFirstName() + " " + newPerson.getPersonLastName());
listPerson.add(cursor.getPosition(), newPerson);
cursor.moveToNext();
newPerson = null;
}
}
return listPerson;
}
最后我在活动中的onCreate()方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employees);
context = this;
SQLiteDatabase myDB = openOrCreateDatabase("ModirYar.db", MODE_PRIVATE, null);
char personType = 'E';
PersonHandler EmployeePerson = new PersonHandler();
List<PersonHandler> myPerson = new ArrayList<PersonHandler>();
myPerson = EmployeePerson.getPersonList(myDB, personType);
//get the list view reference
listViewEmployeesList = (ListView) findViewById(R.id.lstView_Employees);
sampleListAdapter myAdapter = new sampleListAdapter(this, R.layout.item_listviewitem, myPerson);
myAdapter.personType = personType;
listViewEmployeesList.setAdapter(myAdapter);
}
我真的很困惑。是什么导致了这个问题? 提前谢谢......
答案 0 :(得分:0)
尝试使用通用图像加载程序从drawable加载图像。 这样的事情:
ImageLoader.getInstance().displayImage(context, "drawable://" + uri, profilePicture);
uri只会包含资源ID。