Search code examples
androidcustom-lists

change specific item text in custom listview


i have custom list view that has 3 views two text view and one list view. i want to change one of text views in specific position,i want to set one of items text view to owner but the correct one and the last item text view is set to owner. this is my code : thanks

        @override           

        public View getView(int position, View convertView, ViewGroup parent) {
        db.open();
        View row=convertView;
        if(row==null){
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.custom_list_main, parent, false);
        }
        //final String o=objects.get(position);
        //if(o!=null){
            TextView name=(TextView) row.findViewById(R.id.name);
            String nameString=mAdapter.getItem(position);
            name.setText(nameString);
            // ImageView photo= (ImageView) row.findViewById(R.id.photo);
            TextView ownerSign=(TextView) row.findViewById(R.id.owner_text);
            //String value =getItem(position);
            if(getPosition()!=-1 ){
                if(position==getPosition())
                    ownerSign.setText(getResources().getString(R.string.owner));
            }

        //}

        db.close();
        return row;

    }
    public int getPosition(){
        int position=0;
        Cursor c=db.getAllPerson();
        if(c.moveToFirst()){
            do{

                if(c.getInt(c.getColumnIndex(Constants.OWNER))>0){
                    return position;
                }
                ++position;
            }while(c.moveToNext());

        }
        position=0;
        return -1;
    }

Solution

  • i got the answer i should add an else statement to if here is my code:

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            db.open();
            View row=convertView;
            if(row==null){
            LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflater.inflate(R.layout.custom_list_main, parent, false);
            }
            //final String o=objects.get(position);
            //if(o!=null){
                TextView name=(TextView) row.findViewById(R.id.name);
                String nameString=mAdapter.getItem(position);
                name.setText(nameString);
                // ImageView photo= (ImageView) row.findViewById(R.id.photo);
                TextView ownerSign=(TextView) row.findViewById(R.id.owner_text);
                //String value =getItem(position);
    
                if(checkOwner()!=null && getItem(position).equals(checkOwner())){
                        ownerSign.setText(getResources().getString(R.string.owner));
                }
                else{
                    ownerSign.setText(getResources().getString(R.string.nothing));
                }
    
            //}
    
            db.close();
            return row;
    
        }
    
    
        public String checkOwner(){
            Cursor c=db.getAllPerson();
            if(c.moveToFirst()){
                do{
    
                    if(c.getInt(c.getColumnIndex(Constants.OWNER))>0){
                        return c.getString(c.getColumnIndex(Constants.PERSON_NAME));
                    }
                }while(c.moveToNext());     
            }
            return null;
        }