在Adapter RecyclerView中修改的刷新元素

时间:2016-09-20 11:24:15

标签: android android-recyclerview adapter

您好我RecyclerView有问题。 我有一个RecyclerView行布局和interface我的活动点击按钮并向db请求更新名称。

我的 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>

我有我的适配器,当点击按钮时应更改数据库和持有者的名称。

适配器

    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);
}

所以,当我点击我的按钮时,我会在我的活动中调用一个函数就像这样

public void handleName(View caller, final JSONObject obj, JSONArray data, int position) {
   SystemService.getInstance().handleName("example name");
}

使用示例“Marco”在db中进行volleyrequest和更新名称,所以我的问题是,如何隐藏我点击的按钮并在调用db后实时设置名称? 对不起这么多代码!谢谢你们!

1 个答案:

答案 0 :(得分:0)

尝试创建包含适配器中每个按钮可见性的列表。单击按钮后,将点击位置的按钮的可见性更改为View.GONE,然后刷新适配器。