根据数据库查询更改颜色

时间:2016-06-18 21:58:20

标签: java android sqlite

Android的新手,很难确定如何改变颜色,具体取决于sqlite db中是否存在值,或者不在我的适配器中。基本上 fontFamily3 获取textview ID并将文本更改为fontawesome并将颜色设置为红色。如果' url'我想将颜色更改为蓝色。值已存在于sqlite db中。

我知道如何在Mysql和phpmyadmin中执行此操作,但相同的语法在Android studio中不起作用。

UsersAdapter.java

public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
private ArrayList<User> mDataSet;
private Context context;

private SQLiteDatabase db;

public UsersAdapter(ArrayList<User> mDataSet, Context context) {
    this.context = context;
    this.mDataSet = mDataSet;

    createDatabase();

}

public void createDatabase(){
    db=context.openOrCreateDatabase("xxx", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS faces(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,url VARCHAR);");
}


@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_favorites2, parent, false);
    UserViewHolder userViewHolder = new UserViewHolder(v);
    return userViewHolder;
}

@Override
public void onBindViewHolder(UserViewHolder holder, int position) {


    holder.name_entry.setText(mDataSet.get(position).getName());
    holder.email_entry.setText(mDataSet.get(position).getEmail());
    holder.icon_entry.setText(""+mDataSet.get(position).getName().charAt(0));

    String name = mDataSet.get(position).getName();
    String url = mDataSet.get(position).getEmail();

    Glide.with(context)
            .load(url)
            .asBitmap()
            .placeholder(R.drawable.ic_blank)
            .error(R.drawable.ic_blank)
            .into(holder.ivFavorites);
}

@Override
public int getItemCount() {
    return mDataSet.size();
}

public class UserViewHolder extends RecyclerView.ViewHolder {
    CardView cardView;
    TextView name_entry, email_entry, icon_entry;
    ImageView ivFavorites;

    UserViewHolder(View itemView) {
        super(itemView);
        cardView = (CardView) itemView.findViewById(R.id.user_layout);
        name_entry = (TextView) itemView.findViewById(R.id.name_entry);
        email_entry = (TextView) itemView.findViewById(R.id.email_entry);
        icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
        ivFavorites = (ImageView) itemView.findViewById(R.id.ivFavorites);

        Typeface fontFamily2 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
        TextView fontFamilyText2 = (TextView) itemView.findViewById(R.id.fontAwesomeDirectLink);
        fontFamilyText2.setTypeface(fontFamily2);
        fontFamilyText2.setText("\uf0c1");
        fontFamilyText2.setTextColor(ContextCompat.getColor(context, R.color.bg_screen3));
        fontFamilyText2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                context = view.getContext();
                final User mDataSet2 = mDataSet.get(getAdapterPosition());;
                String url = mDataSet2.getEmail();
                String name = mDataSet2.getName();
                ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("xxx direct link", url);
                clipboard.setPrimaryClip(clip);
                Snackbar snackbar = Snackbar
                        .make(view, "Copied " + name + "!", Snackbar.LENGTH_LONG)
                        .setActionTextColor(context.getResources().getColor(R.color.bg_screen3))
                        .setAction("ADD IMG TAGS", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                context = view.getContext();
                                final User mDataSet2 = mDataSet.get(getAdapterPosition());;
                                String url = mDataSet2.getEmail();
                                String name = mDataSet2.getName();
                                ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData clip = ClipData.newPlainText("xxx direct link with image tags", "[img]" + url + "[/img]");
                                clipboard.setPrimaryClip(clip);
                                Snackbar snackbar1 = Snackbar.make(view, "Added image tags!", Snackbar.LENGTH_SHORT);
                                snackbar1.show();
                            }
                        });

                snackbar.show();
            }
        });

        Typeface fontFamily3 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
        TextView fontFamilyText3 = (TextView) itemView.findViewById(R.id.fontAwesomeImageTagLink);
        fontFamilyText3.setTypeface(fontFamily3);
        fontFamilyText3.setText("\uf00d");
        fontFamilyText3.setTextColor(ContextCompat.getColor(context, R.color.dot_dark_screen1));
        fontFamilyText3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                context = view.getContext();
                final User mDataSet3 = mDataSet.get(getAdapterPosition());;
                String url = mDataSet3.getEmail();
                String name = mDataSet3.getName();
                String query = "DELETE FROM faces WHERE url = ('"+url+"');";
                db.execSQL(query);
                mDataSet.remove(getAdapterPosition());
                notifyDataSetChanged();
                Snackbar snackbar = Snackbar
                        .make(view, name + " Removed!", Snackbar.LENGTH_LONG)
                        .setActionTextColor(context.getResources().getColor(R.color.dot_dark_screen1))
                        .setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                context = view.getContext();
                                final User mDataSet3 = mDataSet.get(getAdapterPosition());;
                                String url = mDataSet3.getEmail();
                                String name = mDataSet3.getName();
                                String query = "INSERT INTO faces (name,url) VALUES('"+name+"', '"+url+"');";
                                db.execSQL(query);
                                Snackbar snackbar3 = Snackbar.make(view, name + " Favorited!", Snackbar.LENGTH_SHORT);
                                snackbar3.show();
                            }
                        });
                snackbar.show();
            }
        });


        Typeface fontFamily4 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
        TextView fontFamilyText4 = (TextView) itemView.findViewById(R.id.fontAwesomeShare);
        fontFamilyText4.setTypeface(fontFamily4);
        fontFamilyText4.setText("\uf1e0");
        fontFamilyText4.setTextColor(ContextCompat.getColor(context, R.color.bg_screen3));
        fontFamilyText4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                context = view.getContext();
                final User mDataSet2 = mDataSet.get(getAdapterPosition());;
                String url = mDataSet2.getEmail();
                String name = mDataSet2.getName();
                Drawable mDrawable = ivFavorites.getDrawable();
                Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
                Bitmap imageWithBG = Bitmap.createBitmap(600, 600, mBitmap.getConfig());  // Create another image the same size
                imageWithBG.eraseColor(Color.WHITE);  // set its background to white, or whatever color you want
                Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
                canvas.drawBitmap(mBitmap, 175, 100, null); // draw old image on the background
                String path = MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        imageWithBG, "xxx", null);
                Uri uri = Uri.parse(path);
                final Intent shareIntent = new Intent(Intent.ACTION_SEND); // THIS IS SLOW 
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.setType("image/*");
                context.startActivity(shareIntent);
            }
        });

    }
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

0 个答案:

没有答案