我想对checkbox
中的项目使用recyclerview
我从服务器4加载数据4(在看到最后一项他添加另外4个新项目之后)但添加新项目时的问题已检查的项目更改为另一个项目idon不知道为什么我要保存在SharedPreferences
上检查的每个项目以在命令活动中使用它们
一些图片:
enter image description here 在去了道具后,我发现了另一个项目,并且第一个未经检查:
enter image description here 的 card.xml
<android.support.v7.widget.CardView android:id="@+id/card_view" android:layout_width="wrap_content" android:layout_height="119dp" android:layout_gravity="center" android:elevation="3dp" card_view:cardCornerRadius="1dp" card_view:cardElevation="4dp" card_view:cardUseCompatPadding="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txt_idArticle1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="ID Article : " android:textColor="@android:color/black" android:textStyle="normal|bold" /> <TextView android:id="@+id/txt_reference1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/txt_des" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="5dp" android:text="Reference : " android:textColor="@android:color/background_dark" android:textStyle="normal|bold" /> <TextView android:id="@+id/txt_des1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/txt_prix" android:layout_alignBottom="@+id/txt_prix" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:text="Prix : " android:textColor="@android:color/background_dark" android:textStyle="normal|bold" /> <TextView android:id="@+id/txt_des" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@+id/txt_prix1" android:layout_toRightOf="@+id/txt_prix1" android:text="TextView" /> <TextView android:id="@+id/txt_prix" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/txt_prix1" android:layout_toEndOf="@+id/txt_des1" android:layout_toRightOf="@+id/txt_des1" android:text="TextView" /> <TextView android:id="@+id/txt_idArticle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toEndOf="@+id/txt_idArticle1" android:layout_toRightOf="@+id/txt_idArticle1" android:text="TextView" /> <TextView android:id="@+id/txt_reference" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/txt_reference1" android:layout_alignBottom="@+id/txt_reference1" android:layout_toEndOf="@+id/txt_reference1" android:layout_toRightOf="@+id/txt_reference1" android:text="TextView" /> <TextView android:id="@+id/txt_prix1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/txt_reference1" android:layout_marginBottom="5dp" android:text="Designation : " android:textColor="@android:color/black" android:textStyle="normal|bold" /> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="panier" android:textSize="10sp" /> </RelativeLayout> </android.support.v7.widget.CardView>
recherche_art.java
package com.example.bacha.pfe.activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.example.bacha.pfe.R;
import com.example.bacha.pfe.adapter.ArticleAdapter;
import com.example.bacha.pfe.classes.Article;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class recherche_art extends AppCompatActivity {
private RecyclerView recyclerView ;
private GridLayoutManager gridLayoutManager;
private ArticleAdapter adapter ;
private List<Article> data_list ;
private String recherche_article;
private ImageButton btnrechArt ;
private EditText arech;
int d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recherche_art);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
btnrechArt = (ImageButton) findViewById(R.id.btnrechArt);
recherche_art.this.onRestart();
btnrechArt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
d=0;
Log.d("PFE", String.valueOf(d));
arech= (EditText) findViewById(R.id.eTextRechArt);
data_list = new ArrayList<>();
recherche_article=arech.getText().toString();
load_article_from_server(d);
gridLayoutManager = new GridLayoutManager(recherche_art.this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new ArticleAdapter(recherche_art.this,data_list);
recyclerView.setAdapter(adapter);
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
d=data_list.size();
load_article_from_server(d);
Log.d("PFE", String.valueOf(d));
}
}
});
}
private void load_article_from_server(final int id) {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() {
@Override
protected Void doInBackground(Integer... Params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.print("End of content");
}
return null ;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}
activity_recherche_art.xml
<TextView android:text="Recherche Article :" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:textSize="24sp" android:textStyle="normal|bold" android:textAlignment="center" android:textColor="?attr/colorPrimary" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/recherche" android:id="@+id/btnrechArt" android:layout_marginRight="20dp" android:layout_marginEnd="20dp" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignTop="@+id/eTextRechArt" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/eTextRechArt" android:layout_below="@+id/textView" android:layout_toLeftOf="@+id/btnrechArt" android:layout_toStartOf="@+id/btnrechArt" /> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" article_view:cardUseCompatPadding="true" article_view:cardElevation="5dp" article_view:cardCornerRadius="5dp" android:scrollbars="vertical" android:layout_marginTop="18dp" android:layout_below="@+id/btnrechArt" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />
Article.java
package com.example.bacha.pfe.classes;
import java.io.Serializable;
/**
* Created by BACHA on 07/03/2017.
*/
public class Article implements Serializable {
private int id;
private String id_Article,Reference,Designation,PVTTC;
public Article(/*int id,*/ String id_Article, String reference, String designation, String PVTTC) {
// this.id = id;
this.id_Article = id_Article;
Reference = reference;
Designation = designation;
this.PVTTC = PVTTC;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getId_Article() {
return id_Article;
}
public void setId_Article(String id_Article) {
this.id_Article = id_Article;
}
public String getReference() {
return Reference;
}
public void setReference(String reference) {
Reference = reference;
}
public String getDesignation() {
return Designation;
}
public void setDesignation(String designation) {
Designation = designation;
}
public String getPVTTC() {
return PVTTC;
}
public void setPVTTC(String PVTTC) {
this.PVTTC = PVTTC;
}
}
ArticleAdapter.java
package com.example.bacha.pfe.adapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.bacha.pfe.R;
import com.example.bacha.pfe.activity.ArticleDetail;
import com.example.bacha.pfe.classes.Article;
import java.util.List;
import static com.example.bacha.pfe.R.layout.card;
/**
* Created by BACHA on 08/03/2017.
*/
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
private static Context context ;
private List<Article> my_data ;
public ArticleAdapter(Context context, List<Article> my_data) {
this.context = context;
this.my_data = my_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(card,parent,false);
return new ArticleAdapter.ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.id_Article.setText(my_data.get(position).getId_Article());
holder.Reference.setText(my_data.get(position).getReference());
holder.Designation.setText(my_data.get(position).getDesignation());
holder.PVTTC.setText(my_data.get(position).getPVTTC());
holder.root.setTag(my_data.get(position));
}
@Override
public int getItemCount() {
return my_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView id_Article,Reference,Designation,PVTTC;
View root;
public ViewHolder(View itemView) {
super(itemView);
root = itemView;
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Article art = (Article)v.getTag();
Intent intent = new Intent(context, ArticleDetail.class);
intent.putExtra("REFERENCE", art.getReference());
context.startActivity(intent);
}
});
id_Article=(TextView) itemView.findViewById(R.id.txt_idArticle);
Reference=(TextView) itemView.findViewById(R.id.txt_reference);
Designation=(TextView) itemView.findViewById(R.id.txt_des);
PVTTC=(TextView) itemView.findViewById(R.id.txt_prix);
}
@Override
public void onClick(View v) {
}
}
}