ArrayAdapter仅在列表视图

时间:2015-12-01 17:18:53

标签: java android listview android-arrayadapter

在我的应用程序中,我有一个listView,显示员工,每个项目都有自己的名字,电子邮件和缩略图。一切正常,但缩略图。我的ArrayAdapter不是将所有员工个人资料图片显示为缩略图,而是这样:

  • 会显示所有默认图片,当用户没有为特定员工选择任何图片时,会自动选择这些图片。 (默认图片位于可绘制文件夹中:"android.resource://com.witchking.modiryar/" + R.drawable.default_pic;
  • 确实显示最后插入的员工图片为缩略图
  • 如果我回到之前的活动并返回列表,列表将不会显示最后的图片。
  • 如果我有2名或更多员工拥有相同的图片,并且我使用相同的图片再次创建另一名员工,列表将显示所有员工的图片与上次插入的员工图片相同!

这张照片是为了让您更好地了解我所面对的情况:

我的适配器问题:

image

  1. 员工哈利波特添加了默认图片
  2. 员工Frodo bagins添加了名为“A”的图片
  3. 返回主要活动并返回此活动后的员工清单
  4. 员工钢铁侠添加了名为“B”的图片
  5. 员工超人再次添加了名为“A”的图片
  6. 这是我的代码:

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

    我真的很困惑。是什么导致了这个问题? 提前谢谢......

1 个答案:

答案 0 :(得分:0)

尝试使用通用图像加载程序从drawable加载图像。 这样的事情:

ImageLoader.getInstance().displayImage(context, "drawable://" + uri, profilePicture);

uri只会包含资源ID。