自定义列表视图文字颜色未更改

时间:2019-06-25 06:29:35

标签: java android custom-lists

你好朋友,我正在使用数组采用器的自定义列表视图,我的问题是我试图以编程方式更改文本颜色,但对自定义列表视图没有任何影响,请帮助我,我是初学者,我需要你的帮助,我不知道不知道如何解决此问题,您可以在下面看到我的代码,现在检查这里的问题是什么,在此先感谢您的帮助! 这是我的代码:

  <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:fab="http://schemas.android.com/apk/res-auto"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:descendantFocusability="blocksDescendants"
            android:id="@+id/layout"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="20dp"
                    android:textAlignment="center"
                    android:layout_height="20dp"
                    android:textColor="@color/darkcolor"
                    android:id="@+id/versenumber"
                    android:background="@drawable/texview_design"
                    android:text="1"/>
                <TextView
                    android:layout_width="match_parent"

                    android:id="@+id/verse"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:textColor="@color/darkcolor"
                    android:textSize="23sp"
                    android:text="@string/versedisplay"/>
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_gravity="top"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:id="@+id/share"
                        android:layout_gravity="left"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/ic_share_black_24dp"/>
                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ToggleButton
                    android:id="@+id/adbookmark"
                    android:layout_width="18dp"
                    android:layout_height="18dp"
                    android:layout_gravity="right"

                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/toggle_selector"
                    android:textOff=""
                    android:textOn="" />
            </LinearLayout>
        </LinearLayout>

自定义采用者:

    package bible.swordof.God;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.graphics.Color;
    import android.opengl.Visibility;
    import android.os.Vibrator;
    import android.preference.PreferenceManager;
    import android.provider.CalendarContract;
    import android.speech.tts.TextToSpeech;
    import android.support.v4.content.ContextCompat;
    import android.support.v4.content.res.ResourcesCompat;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.Display;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.CompoundButton;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.ToggleButton;

    import com.amulyakhare.textdrawable.TextDrawable;
    import com.amulyakhare.textdrawable.util.ColorGenerator;


    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Locale;
    import java.util.Set;

    import es.dmoral.toasty.Toasty;
    import petrov.kristiyan.colorpicker.ColorPicker;

    import static android.content.Context.MODE_PRIVATE;
    import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE;
    import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
    import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
    import static android.support.constraint.Constraints.TAG;
    import static android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext;
    import static android.support.v4.content.ContextCompat.getColor;
    import static android.support.v4.content.ContextCompat.startActivity;
    import static android.view.View.getDefaultSize;

    public class FullverseAdopter extends ArrayAdapter<String> {
        private ALLVERSE activity;
        private List<String> versenumber;
        private List<String>verseid;
        private List<String> verselist;
        private List<String> refernce;
        TextToSpeech textToSpeech;
        private DatabaseHelper mDBHelper;
        private SQLiteDatabase mDb;
        private  String selectcolor;
        ArrayList<String>colors;
        Context context;





        public FullverseAdopter(ALLVERSE context, int resource, List<String> versenumber, List<String> verselist, List<String> refernce, List<String>verseid) {
            super(context, resource);
            this.activity = context;
            this.versenumber = versenumber;
            this.verselist = verselist;
            this.refernce = refernce;
            this.verseid=verseid;
        }


        @Override
        public int getCount() {
            return versenumber.size();
        }

        @Override
        public String getItem(int position) {
            return versenumber.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            final ViewHolder holder;

            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            // If holder not exist then locate all view from UI file.
            if (convertView == null) {
                // inflate UI from XML file
                 convertView = inflater.inflate(R.layout.versedisplayrow, parent, false);
                // get all UI view
                holder = new ViewHolder(convertView);
                // set tag for holder
                   holder.versenumber = (TextView) convertView.findViewById(R.id.versenumber);
                holder.verselist = (TextView) convertView.findViewById(R.id.verse);


    //COLOR NOT CHANGED here 
    holder.verselist.setTextColor(Color.parseColor("#fe00fb"));



              /*  holder.addfavoruite=(ToggleButton)convertView.findViewById(R.id.adbookmark);*/




                convertView.setTag(holder);
            } else {
                // if holder created, get tag from view
                holder = (ViewHolder) convertView.getTag();
            }

            colors=new ArrayList<>();
            colors.add("#e0e0eb");
            colors.add("#ccffff");
            colors.add("#ffe6ff");
            colors.add("#ffffcc");
            colors.add("#ccffcc");
            colors.add("#e6f2ff");




            holder.versenumber.setText(versenumber.get(position));

            holder.verselist.setText(verselist.get(position));

            //check if id is exits in db
            if(CheckIsDataAlreadyInDBorNot("highlight","verseid",verseid.get(position)))
            {
                if(verseid.get(position).equals(verseid.get(position))){

                   holder.linearLayout.setBackgroundColor(Color.parseColor("#e6f2ff"));

                }
            }

    //check if is  in favourite
            if(Checkisfavourite("favourite","versenumber",versenumber.get(position)))
            {
                if(versenumber.get(position).equals(versenumber.get(position))){

                   holder.addfavoruite.setChecked(true);

                }
            }





    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(CheckIsDataAlreadyInDBorNot("highlight","verseid",verseid.get(position)))
            {
                if(verseid.get(position).equals(verseid.get(position))){
                    mDBHelper = new DatabaseHelper(activity);
                    mDb = mDBHelper.getWritableDatabase();
                    long delete= mDb.delete("highlight","verseid=?",new String[]{verseid.get(position)});

                }




            }else {
                Toast.makeText(activity, "NO HIGHLIGHTS FOR REMOVE", Toast.LENGTH_SHORT).show();
            }




          //  holder.linearLayout.setBackgroundColor(selectcolor);


        }
    });


    //here color picker dialog popup for chosing color

    holder.linearLayout.setOnLongClickListener(new View.OnLongClickListener() {


        @Override
        public boolean onLongClick(View v) {

            //Toasty.success(activity, "PICK COLOR", Toast.LENGTH_SHORT, true).show();
            Vibrator vibe = (Vibrator)activity.getSystemService(Context.VIBRATOR_SERVICE);
            vibe.vibrate(100);

            mDBHelper = new DatabaseHelper(activity);
            mDb = mDBHelper.getWritableDatabase();
            ContentValues contentValues=new ContentValues();
            contentValues.put("verseid",verseid.get(position));
            final long check=mDb.insert("highlight",null,contentValues);


            //id get




            //postion
            //Toast.makeText(activity, ""+getItemId(position), Toast.LENGTH_SHORT).show();

            ColorPicker colorPicker = new ColorPicker(activity);


            colorPicker.setColors(colors).setColumns(4).setTitle("HIGHLIGHT VERSE").setRoundColorButton(true).setOnChooseColorListener(new ColorPicker.OnChooseColorListener() {
                @Override
                public void onChooseColor(int position, int color) {


                   holder.linearLayout.setBackgroundColor(Color.parseColor(colors.get(position)));
                }

                @Override
                public void onCancel() {

                }
            }).show();






            return false;
        }
    });
            /*holder.verselist.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    holder.verselist.setBackgroundColor(Color.parseColor("#e0e0eb"));
                    return false;
                }
            });*/




            //share verse
            holder.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toasty.info(activity, "Sharing a verse.", Toast.LENGTH_SHORT, true).show();
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, refernce.get(position) + ":" + versenumber.get(position) + '\n'   + verselist.get(position));
                    sendIntent.setType("text/plain");
                    activity.startActivity(sendIntent);

                }
            });

    //add in favourite
          holder.addfavoruite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked){
                        mDBHelper = new DatabaseHelper(activity);
                        mDb = mDBHelper.getWritableDatabase();
                        ContentValues contentValues=new ContentValues();
                        contentValues.put("id",verseid.get(position));
                         contentValues.put("bookname",refernce.get(position));
                         contentValues.put("versenumber",versenumber.get(position));

                       contentValues.put("verse",verselist.get(position));


                       long check=mDb.insert("favourite",null,contentValues);


                       Log.d("MY_TAG","DB IS NOW "+check);
                       Toasty.success(activity, "Added in favouite", Toast.LENGTH_SHORT, true).show();


                    }else {


                        mDBHelper = new DatabaseHelper(activity);
                        mDb = mDBHelper.getWritableDatabase();
                        long delete= mDb.delete("favourite","id=?",new String[]{verseid.get(position)});
                       Toasty.error(activity, "Remove in favouite", Toast.LENGTH_SHORT, true).show();
                    }


                }
            });

           /* textToSpeech = new TextToSpeech(activity, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {

                    if (status == TextToSpeech.SUCCESS) {
                        result = textToSpeech.setLanguage(Locale.ENGLISH);
                    } else {

                        Toast.makeText(activity, "YOUR DEVICE NOT SUPPORTED", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    */



            //My toggle button








           /* holder.speakverse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(activity, "I AM CLICKED", Toast.LENGTH_SHORT).show();

                    if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA) {

                        Toast.makeText(activity, "Language not supported or Missing", Toast.LENGTH_SHORT).show();

                    } else {


                        textToSpeech.speak(verselist.get(position), TextToSpeech.QUEUE_FLUSH, null);

                    }
                }
            });*/




            return convertView;
        }

        static class ViewHolder {
            private TextView versenumber;
            private TextView verselist;

            private ImageView share;
            private  ToggleButton addfavoruite;
            private ImageView speakverse;
            private LinearLayout linearLayout;




            public ViewHolder(View v) {
                versenumber = (TextView) v.findViewById(R.id.versenumber);
                verselist = (TextView) v.findViewById(R.id.verse);
               share = (ImageView) v.findViewById(R.id.share);
                /*speakverse = (ImageView) v.findViewById(R.id.speakverse);*/
      /*      addfavoruite=(ToggleButton)v.findViewById(R.id.adbookmark);*/
            linearLayout=(LinearLayout)v.findViewById(R.id.layout);


            }

         }




        public boolean CheckIsDataAlreadyInDBorNot(String TableName,  String dbfield, String fieldValue) {

            mDBHelper = new DatabaseHelper(activity);
            mDb = mDBHelper.getReadableDatabase();
            String Query = "Select * from " + TableName + " where " + dbfield + " = " + fieldValue;
            Cursor cursor = mDb.rawQuery(Query, null);
            if(cursor.getCount() <= 0){
                cursor.close();

               // Toast.makeText(activity, "false", Toast.LENGTH_SHORT).show();
                return false;
            }else {

                  // Toast.makeText(activity, "TRUE", Toast.LENGTH_SHORT).show();

            }
            cursor.close();


            return true;
        }


        public boolean Checkisfavourite(String TableName,  String dbfield, String fieldValue) {

            mDBHelper = new DatabaseHelper(activity);
            mDb = mDBHelper.getReadableDatabase();
            String Query = "Select * from " + TableName + " where " + dbfield + " = " + fieldValue;
            Cursor cursor = mDb.rawQuery(Query, null);
            if(cursor.getCount() <= 0){
                cursor.close();

                // Toast.makeText(activity, "false", Toast.LENGTH_SHORT).show();
                return false;
            }else {

                // Toast.makeText(activity, "TRUE", Toast.LENGTH_SHORT).show();

            }
            cursor.close();


            return true;
        }


    }

活动xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:id="@+id/color"
        tools:context=".ALLVERSE">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorAccent"
            app:popupTheme="@style/AppTheme.PopupOverlay">
            <TextView
                android:id="@+id/bookname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="20sp"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:text="ALL VERESE" />
        </android.support.v7.widget.Toolbar>

        <ListView
            android:layout_below="@+id/toolbar"
            android:id="@+id/mylistview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您正在将颜色更改置于convertView == null的条件内,只需将代码移到这样的条件之外

if (convertView == null) {
...
} else {
...
}

holder.verselist.setTextColor(Color.parseColor("#fe00fb"));
colors=new ArrayList<>();
...

答案 1 :(得分:0)

尝试将此Color.parseColor("#fe00fb"更改为ContextCompat.getColor(this,RGB_COLOR_CODE)