Hallo Ich habe ein Problem mit RecyclerView
. Ich habe eine RecyclerView
mit Zeilenlayout und interface
mit meiner Aktivität für den Klick auf die Schaltfläche und eine Anfrage an db für Update-Namen.Aktualisieren Element in Adapter RecyclerView geändert
Ich habe in meinem row_layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/change_name"
android:text="@string/change"
/>
</RelativeLayout>
Und ich habe meine Adapter, dass bei Klick auf den Button Namen auf DB und auf Inhaber ändern sollte.
Adapter
public class ListAlertsAdapter extends RecyclerView.Adapter<ListAlertsAdapter.MyViewHolder> {
private JSONArray dataSource;
private LayoutInflater inflater;
private AdapterCallback mAdapterCallback;
public interface AdapterCallback {
void handleName(View caller, JSONObject obj, JSONArray data, int position);
}
public ListAlertsAdapter(JSONArray dataArgs, Context context){
dataSource = dataArgs;
inflater= LayoutInflater.from(context);
this.mAdapterCallback = ((AdapterCallback) context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout_row,parent,false);
MyViewHolder holder=new MyViewHolder(view, new ListAlertsAdapter.MyViewHolder.IMyViewHolderClicks() {
public void handleName(View caller, int position) {
JSONObject itemClicked;
try {
itemClicked = dataSource.getJSONObject(position);
mAdapterCallback.handleName(caller, itemClicked, dataSource, position);
/** maybe in this point i could modify itemClicked and notifyDataSetChanged() but not working */
itemClicked.put("name","exampleName Marco");
notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return holder;
}
public void onBindViewHolder(MyViewHolder holder, int position) {
try {
object = dataSource.getJSONObject(position);
holder.name.setText(object.getString("name"));
}catch (JSONException e) {
e.printStackTrace();
}
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
Button handle_name;
public IMyViewHolderClicks mListener;
public MyViewHolder(View itemView, IMyViewHolderClicks listener) {
super(itemView);
mListener = listener;
handle_name=(Button)itemView.findViewById(R.id.change_name);
handle_name.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mListener.handleName(v, position);
}
}
public interface IMyViewHolderClicks {
public void handleName(View caller, int position);
}
Also, wenn ich auf meiner Schaltfläche klicken nenne ich eine Funktion in meine Aktivität Gefallen Sie diesen
public void handleName(View caller, final JSONObject obj, JSONArray data, int position) {
SystemService.getInstance().handleName("example name");
}
, die einen volleyrequest und Update-Namen in db mit Beispiel machen "Marco" und so meine Fragen sind, Wie kann Schaltfläche ausblenden, die ich geklickt habe und Name in Echtzeit nach Anruf db? Entschuldigung für so viel Code! Danke euch allen!