Question

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;
    }
Was it helpful?

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

OTHER TIPS

@SuppressLint("SimpleDateFormat")
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;

        if (convertView == null)
            vi = inflater.inflate(R.layout.coustomgrid, null);
        Typeface tf = Typeface.createFromAsset(activity.getAssets(),
                "fonts/digital-7.ttf");
        TextView name = (TextView) vi.findViewById(R.id.date);
        TextView freq = (TextView) vi.findViewById(R.id.freq);
        name.setTypeface(tf);

        HashMap<String, String> details = new HashMap<String, String>();
        details = data.get(position);
        name.setText(details.get("freq"));
        Logger.infoMessage("Position"+position);
        if(this.selectedPosition!=-1){
        if (position == 2){

            name.setTextColor(Color.parseColor("#7FFF00"));

        }
        }

        freq.setText(Html.fromHtml("<font color=#32cadb>" + details.get("mhz")
                + "</font>"));

        return vi;
    }

It change the text color on second position in listview.Based on that we change the textcolor or row color at any position.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top