البته این کد اگه استرینگ خالی بنویسی بزنی ادد یوزر اپ کرش میکنه!!
new->valuse resource file -> dimens:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="small_space">9dp</dimen>
</resources>
user.java
package ir.ben.practice;
public class user {
private String name;
private Integer score;
private Integer rank;
public String getName() {
return name;
}
public int getScore() {
return score;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int score) {
this.score = score;
}
}
MainActivity
package ir.ben.practice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button bt,sho;
Integer n=9;
user hooloo;
ranklist rr=new ranklist();
EditText tv,tnumv;
String ss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hooloo=new user();
hooloo.setScore(23);
hooloo.setName("OKKKK");
bt=findViewById(R.id.button);
sho=findViewById(R.id.openlist);
tv=findViewById(R.id.tv);
tnumv=findViewById(R.id.num);
sho.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyFragment frag=new MyFragment();
getSupportFragmentManager().beginTransaction().add(R.id.frame,frag).addToBackStack(null).
commit();
}
});
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hooloo.setName("*"+tv.getText().toString());
hooloo.setScore(Integer.valueOf("1"+tnumv.getText().toString()));
GoodShared.getInstance(MainActivity.this).addToWinnersNames(hooloo);
rr=GoodShared.getInstance(MainActivity.this).getWinnersNames();
tv.setText("SharedPrefrense ranklist size="+rr.getPeople().size());
Log.d("starlog","kooft");
for(user u:rr.getPeople())
{
Log.d("win= ",u.getName()+" score: "+u.getScore());
}
}
});
}
}
Goodshared
package ir.ben.practice;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.google.gson.Gson;
public class GoodShared {
private static GoodShared instance=null;
private SharedPreferences folder=null;
private SharedPreferences .Editor writer=null;
private ranklist rn=new ranklist();
private Gson gg=new Gson();
public static GoodShared getInstance(Context context)
{
if(instance==null)
instance=new GoodShared(context);
return instance;
}
private GoodShared(Context context)
{
folder=context.getSharedPreferences("winners",Context.MODE_PRIVATE);
writer=folder.edit();
// rn=new ranklist();
}
public ranklist getWinnersNames()
{
return gg.fromJson(folder.getString("winners",null),ranklist.class);
}
public void addToWinnersNames(user hooloo)
{
String ss=folder.getString("winners",null);
if(ss!=null)
rn=gg.fromJson(ss,ranklist.class);
else
rn=new ranklist();
rn.addnewuser(hooloo);
// writer.putString("winners",hooloo.getName());
writer.putString("winners",gg.toJson(rn,ranklist.class));
writer.apply();
}
}
ranklist
package ir.ben.practice;
import java.util.ArrayList;
import java.util.List;
public class ranklist {
private List<user> people;
public ranklist() {
people=new ArrayList<>();
}
public void addnewuser(user u){
people.add(u);
}
public List<user> getPeople() {
return people;
}
public void setPeople(List<user> people) {
this.people = people;
}
}
Myfragment
package ir.ben.practice;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
public class MyFragment extends Fragment {
RecyclerView frend;
assistanUserAdapter assisstan;
ranklist mellat;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
frend=view.findViewById(R.id.bicycle);
////// frend=viewfind
mellat=GoodShared.getInstance(getActivity()).getWinnersNames();
Comparator<user> how=new Comparator<user>() {
@Override
public int compare(user x, user y) {
if(x.getScore()>y.getScore())
return -1;
else if (x.getScore()<y.getScore())
return 1;
return 0;
}
};
Collections.sort(mellat.getPeople(),how);
assisstan=new assistanUserAdapter(mellat.getPeople());
Log.d("mellat people size","="+mellat.getPeople().size());
//// assisstan.notifyDataSetChanged();
frend.setLayoutManager(new LinearLayoutManager(getActivity()));
frend.setAdapter(assisstan);
// TextView tv=view.findViewById(R.id.hey);
///// tv.setText(this.getArguments().getString("namekey",null));
}
}
assistantuser(adapter)
package ir.ben.practice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class assistanUserAdapter extends RecyclerView.Adapter<assistanUserAdapter.ViewHolder> {
private List<user> hulua;
public assistanUserAdapter(List<user> items)
{
hulua=items;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View vv= LayoutInflater.from(parent.getContext()).inflate(R.layout.onerow,parent,false);
return new ViewHolder(vv);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.name.setText(hulua.get(position).getName());
holder.rank.setText(String.valueOf(position+1));
holder.score.setText(String.valueOf(hulua.get(position).getScore()));
}
@Override
public int getItemCount() {
return hulua.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView name;
public TextView score;
public TextView rank;
public ViewHolder(View heh){
super(heh);
score=heh.findViewById(R.id.adad);
name=heh.findViewById(R.id.bach);
rank=heh.findViewById(R.id.rank);
}
}
}