我有一个Android应用程序,用户可以喜欢而不像图像。我面临的问题是,当用户第一次启动应用程序时加载图像时,我想将已经喜欢的图像显示为彩色,在Instagram中,我加载图像时,我之前喜欢的图像会被着色。如何在我的应用中执行此功能?
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private String imageUrl;
private ImageLoader imageLoader,imageLoader1;
private Context context;
String Load;
private static ClickListener c;
public static final String uu = "uu";
String number;
public static final String UserNum = "UserNum";
SharedPreferences sharedPref;
private String[] pos;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
// JSON parser class
JSONParser jsonParser = new JSONParser();
String url,imgoffset;
//testing from a real server:
private static final String LIKE_URL = "http://svastek.in/insta/like.php";
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
List<SuperHeroes> superHeroes1;
//List of superHeroes
List<SuperHeroes> superHeroes;
private RecyclerView recyclerView;
public CardAdapter() {
}
public CardAdapter(List<SuperHeroes> superHeroes, Context context) {
super();
//Getting all the superheroes
this.superHeroes = superHeroes;
superHeroes1= superHeroes;
this.context = context;
sharedPref =context.getSharedPreferences(UserNum, 0);
number = sharedPref.getString(uu, "");
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.superheroes_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
SuperHeroes superHero = superHeroes.get(position);
Log.d("position", String.valueOf(position));
Log.d("url", superHero.getImageUrl());
// Load = superHero.getImageUrl();
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
imageLoader1 = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader1.get(superHero.getProurl(), ImageLoader.getImageListener(holder.thumbNail, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
Log.d("i am ob", "ob");
holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
holder.thumbNail.setImageUrl(superHero.getProurl(), imageLoader1);
holder.textViewName.setText(superHero.getName());
holder.textViewRank.setText(String.valueOf(superHero.getRank()));
if(superHero.getstat())
{
Log.d("i am g","g");
Log.d("i am value", String.valueOf(superHero.getstat()));
holder.like.setImageResource(R.drawable.plike);
}
else
{
Log.d("i am p","p");
holder.like.setImageResource(R.drawable.glike);
}
// holder.textViewPowers.setText(powers);
holder.like.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.d("i m in like","i m in like");
SuperHeroes superHero = superHeroes.get(position);
Load = superHero.getImageUrl();
Log.d("like position", String.valueOf(position));
new LikeIt().execute();
if(dudo!=null) {
Log.d("i m in like1", "i m in like1");
dudo.onlike(v, position);
}} });
}
public void refresh(List<SuperHeroes> superHeroes){
Log.d("Refresh","Refresh is called ");
this.superHeroes=superHeroes;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return superHeroes.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public NetworkImageView imageView;
public CircularNetworkImageView thumbNail;
public TextView textViewName;
public TextView textViewuserName;
public TextView textViewRank;
public ImageButton like;
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.username_row);
//textViewuserName = (TextView) itemView.findViewById(R.id.username_row);
textViewRank = (TextView) itemView.findViewById(R.id.textViewRank);
thumbNail = (CircularNetworkImageView) itemView.findViewById(R.id.thumbnail);
like = (ImageButton) itemView.findViewById(R.id.button_like);
// itemView.setOnClickListener( this);
}
@Override
public void onClick(View v) {
if (dudo != null) {
Log.d("i m in like1", "i m in like1");
dudo.onlike(v, 2);
}
}
}
class LikeIt extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String Imgurl = Load;
Log.d("request like url!", Imgurl);
// try {
// Building Parameters
HashMap<String, String> Params = new HashMap<String, String>();
Params.put("Imgurl", Imgurl);
Params.put("user", number);
Log.d("request!", "starting");
String encodedStr = getEncodedData(Params);
//Will be used if we want to read some data from server
BufferedReader reader = null;
//Connection Handling
try {
//Converting address String to URL
URL url = new URL(LIKE_URL);
//Opening the connection (Not setting or using CONNECTION_TIMEOUT)
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//Post Method
con.setRequestMethod("POST");
//To enable inputting values using POST method
//(Basically, after this we can write the dataToSend to the body of POST method)
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
//Writing dataToSend to outputstreamwriter
writer.write(encodedStr);
//Sending the data to the server - This much is enough to send data to server
//But to read the response of the server, you will have to implement the procedure below
writer.flush();
//Data Read Procedure - Basically reading the data comming line by line
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while((line = reader.readLine()) != null) { //Read till there is something available
sb.append(line + "\n"); //Reading and saving line by line - not all at once
}
line = sb.toString(); //Saving complete data received in string, you can do it differently
//Just check to the values received in Logcat
Log.i("custom_check","The values :");
Log.i("custom_check", line);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close(); //Closing the
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Same return null, but if you want to return the read string (stored in line)
//then change the parameters of AsyncTask and return that type, by converting
//the string - to say JSON or user in your case
return null;
}
}
private String getEncodedData(Map<String,String> data) {
StringBuilder sb = new StringBuilder();
for(String key : data.keySet()) {
String value = null;
try {
value = URLEncoder.encode(data.get(key), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(sb.length()>0)
sb.append("&");
sb.append(key + "=" + value);
}
return sb.toString();
}
/**
* After completing background task Dismiss the progress dialog
**/
protected void onPostExecute() {
// dismiss the dialog once product deleted
}
private static ClickListener dudo ;
public interface ClickListener{
void onlike(View v,int pos);
}
public void setClickListener(ClickListener dudo){
this.dudo=dudo;
Log.d("i m in interface","i m in interface");
}
}
我有数据库表存储喜欢图像的用户ID
答案 0 :(得分:0)
我使用内置的sqlite db获得了解决方案。