PDA

View Full Version : RecyclerView



Ruhallah.Ahmadian
سه شنبه 21 اردیبهشت 1395, 18:25 عصر
public class SIMcardsAdapterDELETED extends RecyclerView.Adapter<SIMcardsAdapterDELETED.ViewHolder> {

private SIMcards SimCard = new SIMcards();

private boolean LockRefresh = false;
private int NumberLastRecive;
public getSIMcards task;
public int Page = 1;

private Context context;
private SharedPreferences prefs;

SIMcardsAdapterDELETED(Context context) {
this.context = context;
prefs = context.getSharedPreferences("com.myrond", context.MODE_PRIVATE);
SimCard.data.clear();
task = new getSIMcards();
}

@Override
public SIMcardsAdapterDELETED.ViewHolder onCreateViewHolder(ViewGroup parent, final int position) {
View v = null;
v = LayoutInflater.from(parent.getContext()).inflate(R .layout.item_sim_card, parent, false);
ViewHolder vhItem = new ViewHolder(v,position);
return vhItem;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.ShowNumber.setText(SimCard.data.get(positio n).getShowNumber());
holder.Name.setText(SimCard.data.get(position).get Price());
holder.City.setText(SimCard.data.get(position).get City());
holder.LastModified.setText(SimCard.data.get(posit ion).getLastModified());
int OperatorId = Integer.parseInt(SimCard.data.get(position).getOpe ratorId());
switch(OperatorId) {
case 1:
Picasso.with(holder.Logo.getContext()).load(R.draw able.hamrahe_aval).into(holder.Logo);
break;
case 2:
Picasso.with(holder.Logo.getContext()).load(R.draw able.irancel).into(holder.Logo);
break;
case 3:
Picasso.with(holder.Logo.getContext()).load(R.draw able.talia).into(holder.Logo);
break;
case 4:
Picasso.with(holder.Logo.getContext()).load(R.draw able.raytel).into(holder.Logo);
break;
}
holder.fave.setVisibility(View.VISIBLE);
if(SimCard.data.get(position).getFavoritte()) {
Picasso.with(holder.fave.getContext()).load(R.draw able.fav).into(holder.fave);
}else {
Picasso.with(holder.fave.getContext()).load(R.draw able.unfav).into(holder.fave);
}
holder.Star.setRating(Integer.parseInt(SimCard.dat a.get(position).getStar()));
}

@Override
public int getItemCount() {
return SimCard.data.isEmpty() ? 0 : SimCard.data.size();
}

@Override
public int getItemViewType(int position) {
return position;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
TextView ShowNumber;
ImageView Logo;
TextView Name;
TextView City;
TextView LastModified;
MyRatingbar Star;
ImageButton fave;

public ViewHolder(View itemView,int ViewType) {
super(itemView);
ShowNumber = (TextView) itemView.findViewById(R.id.ISCnumber);
Name = (TextView) itemView.findViewById(R.id.ISCprice);
City = (TextView) itemView.findViewById(R.id.ISCcity);
LastModified = (TextView) itemView.findViewById(R.id.ISCdate);
Logo = (ImageView) itemView.findViewById(R.id.ISClogo);
fave = (ImageButton) itemView.findViewById(R.id.ISCcheck);
Star = (MyRatingbar) itemView.findViewById(R.id.ISCrate);
}
}

public void loadData() {
if(task != null && task.getStatus() == Status.RUNNING)
task.cancel(true);
task = new getSIMcards();
task.execute();
}

public boolean isLockRefresh() {
return LockRefresh;
}

public void setLockRefresh(boolean lockRefresh) {
LockRefresh = lockRefresh;
}

public int getNumberLastRecive() {
return NumberLastRecive;
}

public void ClearData() {
SimCard.ClearData();
Page = 1;
notifyDataSetChanged();
}

public void setData(String data) {
Gson gson = new Gson();
SIMcards NewSimCard = gson.fromJson(data, SIMcards.class);
SimCard.addData(NewSimCard.data);
notifyDataSetChanged();
}

public int getSize() {
return SimCard.data.size();
}

public class getSIMcards extends AsyncTask<String,String,Boolean>{

@Override
public void onPreExecute() {
super.onPreExecute();
setLockRefresh(true);
//Log.e("Page",Page+"");
}

@Override
public Boolean doInBackground(String... args) {
String NAMESPACE = "http://tempuri.org/";
String MAIN_REQUEST_URL = "http://myrond.com/controllers/webapi/simkarts.asmx";
String SOAP_ACTION = "http://tempuri.org/ListSimKart";
String METHODNAME = "ListSimKart";

Bundle bundle = new Bundle();
bundle.putString("NAMESPACE", NAMESPACE);
bundle.putString("MAIN_REQUEST_URL" ,MAIN_REQUEST_URL);
bundle.putString("SOAP_ACTION" ,SOAP_ACTION);
bundle.putString("METHODNAME" ,METHODNAME);

ArrayList<HashMap<String, String>> Properties = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hash = new HashMap<String, String>();
hash.put("name", "page");
hash.put("value", Page+"");
Page++;
Properties.add(hash);

hash = new HashMap<String, String>();
hash.put("name", "sort");
hash.put("value", "-1");
Properties.add(hash);

hash = new HashMap<String, String>();
hash.put("name", "province");
hash.put("value", prefs.getInt("Province",-1)+"");
Properties.add(hash);

hash = new HashMap<String, String>();
hash.put("name", "userId");
hash.put("value", "-1");
Properties.add(hash);

hash = new HashMap<String, String>();
hash.put("name", "prenumberId");
hash.put("value", prefs.getString("PreNumberId","-1"));
Properties.add(hash);

hash = new HashMap<String, String>();
hash.put("name", "operatorId");
hash.put("value", prefs.getString("OperatorId","-1"));
Properties.add(hash);

hash = new HashMap<String, String>();
hash.put("name", "simType");
hash.put("value", prefs.getString("OperatorType","-1"));
Properties.add(hash);

SoapRequests soap = new SoapRequests();
String data = soap.getData(bundle, Properties);

if(data != null) {
Gson gson = new Gson();
SIMcards NewSimCard = gson.fromJson(data, SIMcards.class);
SimCard.addData(NewSimCard.data);

NumberLastRecive = NewSimCard.data.size();
return true;
}

return false;
}

@Override
public void onPostExecute(Boolean b) {
setLockRefresh(false);
if(b)
notifyItemInserted(SimCard.data.size());
else {
Toast.makeText(context, "اختلال در شبکه مجدد تلاش کنید", Toast.LENGTH_SHORT).show();
}
//loadData();
}
}
}


سلام این کد بنظرتون مشکلی داره؟
آخه با اسکرول کردن به پایین حافظه پر میشه تا اینکه کلا اررور میده