Как использовать общие настройки в RecyclerView.Adapter ..? Я использовал общее значение предпочтений в RecyclerView.Adapter..b ничего не сохраняет в общих предпочтениях .. где именно я должен использовать общие предпочтения .. в RecyclerView.Adapter или активность ..?Как использовать общие настройки в RecyclerView.Adapter?
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private ImageLoader imageLoader;
private Context context;
private String gd;
public static final String SHARED_PREF_NAME = "myloginapp";
//We will use this to store the boolean in sharedpreference to track user is loggedin or not
public static final String LOGGEDIN_SHARED_PREF = "loggedin";
public static final String GROUPSNAME_SHARED_PREF = "groupname";
public CardAdapter(Context context) {
this.context = context;
}
//List of superHeroes
List<Group> groups;
public CardAdapter(List <Group> groups, Context context) {
super();
//Getting all the superheroes
this.groups = groups;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder (ViewGroup parent,int viewType){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.groups_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder (ViewHolder holder,int position){
Group group1 = groups.get(position);
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(group1.getPic(), ImageLoader.getImageListener(holder.imageView, R.drawable.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.imageView.setImageUrl(group1.getPic(), imageLoader);
holder.groupname.setText(group1.getGname());//i want to store this value in shared preference..
holder.groupinfo.setText(group1.getGinfo());
gd = holder.groupname.getText().toString();//variable gd is storing any value.
holder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Viewgroup1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return groups.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public NetworkImageView imageView;
public TextView groupname;
public TextView groupinfo;
public Button add;
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
groupname = (TextView) itemView.findViewById(R.id.textViewName);
groupinfo = (TextView) itemView.findViewById(R.id.textViewRank);
add = (Button) itemView.findViewById(R.id.button7);
//String gd = holder.groupname.getText().toString();
SharedPreferences sharedPreferences1 = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sharedPreferences1.edit();
//Adding values to editor
editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
editor.putString(GROUPSNAME_SHARED_PREF, gd);
}
}
}
Viewgroup.java
public class Viewgroup extends AppCompatActivity {
//Creating a List of superheroes
private List<Group> listSuperHeroes;
private String vault;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle);
//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//Initializing our superheroes list
listSuperHeroes = new ArrayList<>();
SharedPreferences sharedPreferences = getSharedPreferences(ProfileLogin.SHARED_PREF_NAME, MODE_PRIVATE);
vault = sharedPreferences.getString(ProfileLogin.EMAIL_SHARED_PREF,"Not Available");
//Calling method to get data
getData();
}
//This method will get data from the web api
private void getData(){
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL+vault,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData(JSONArray array){
for(int i = 0; i<array.length(); i++) {
Group group1 = new Group();
JSONObject json = null;
try {
json = array.getJSONObject(i);
group1.setPic(json.getString(Config.TAG_IMAGE_URL));
group1.setGname(json.getString(Config.TAG_NAME));
group1.setGinfo(json.getString(Config.TAG_INFO));
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(group1);
}
//Finally initializing our adapter
adapter = new CardAdapter(listSuperHeroes, this);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
/* recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final Button add = (Button) v.findViewById(R.id.button7);
final TextView txtStatusChange = (TextView) v.findViewById(R.id.textViewName);
//final TextView txtStatusChange1 = (TextView) v.findViewById(R.id.textView33);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Viewgroup.this, Viewgroup1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// Log.e(TAG_IMAGE_URL, "hello text " + txtStatusChange.getText().toString() + " TAG " + txtStatusChange.getTag().toString());
Toast.makeText(Viewgroup.this, txtStatusChange.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return false;
}
});*/
}
}
Где вызова '' commit' или apply' метода, чтобы сохранить изменения? –
oh my bad ... спасибо за то, что нашел ошибку .. попробуем сейчас .. –
@prosper K..why общее предпочтение не обновляется ..? Я использовал общие предпочтения в holder.add.setOnClickListener (new View .OnClickListener() .. но это значение не обновляется .. –