如何将项目添加到RecylerView?

时间:2018-01-12 09:13:24

标签: android android-recyclerview android-adapter

我想在RecylerView按钮上添加和删除项目。但我无法添加项目。每次按“添加”图标时,都没有任何反应。任何人都可以确定我哪里出错了?

这是我的Recyclerview适配器:

public class ListAdapter extends 
 RecyclerView.Adapter<ListAdapter.ViewHolder>
 {

Context context;
ArrayList<String> steps;

public class ViewHolder extends RecyclerView.ViewHolder{
    ImageButton plus, minus;
    EditText step;

     public ViewHolder(View itemView) {
        super(itemView);
        plus = (ImageButton) itemView.findViewById(R.id.plus);
        minus = (ImageButton) itemView.findViewById(R.id.minus);
        step = (EditText) itemView.findViewById(R.id.step);

        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                try {
                    steps.remove(position);
                    notifyItemRemoved(position);
                }catch (ArrayIndexOutOfBoundsException e)
                  {e.printStackTrace();}
            }
        });

        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                try {
                    steps.add(position + 1, "");
                    notifyItemInserted(position + 1);
                }catch (ArrayIndexOutOfBoundsException e)
                 {e.printStackTrace();}
            }
        });

        step.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int 
             count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, 
             int count) {
                steps.set(getAdapterPosition(), s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
}


public ListAdapter(ArrayList<String> steps, Context context){
    this.steps = steps;
    this.context = context;
}


@Override
public int getItemCount() {
    return steps.size();
}


@Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
    return new ViewHolder(v);
}


@Override
public void onBindViewHolder(ListAdapter.ViewHolder holder, final int i) {

    int x = holder.getLayoutPosition();

    if(steps.get(x).length() > 0) {
        holder.step.setText(steps.get(x));
    }
    else{
        holder.step.setText(null);
        holder.step.setHint("Next Step");
        holder.step.requestFocus();
    }

}


public ArrayList<String> getStepList(){
    return steps;
}
}

这是我的ListActivity:

public class ListActivity extends AppCompatActivity {

final int LIST_RESULT = 100;

ArrayList<String> list;
RecyclerView recyclerView;
ListAdapter listAdapter;
LinearLayoutManager llm;
Button submitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    list = getIntent().getStringArrayListExtra("list");
    //To show at least one row
    if(list == null || list.size() == 0) {
        list = new ArrayList<>();
        list.add("");
    }

    submitButton = (Button) findViewById(R.id.submit_button);
    recyclerView = (RecyclerView) findViewById(R.id.rv);
    listAdapter = new ListAdapter(list, this);
    llm = new LinearLayoutManager(this);

    //Setting the adapter
    recyclerView.setAdapter(listAdapter);
    recyclerView.setLayoutManager(llm);

    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            list = listAdapter.getStepList();
            i.putStringArrayListExtra("list", list);
            setResult(LIST_RESULT, i);
            finish();
        }
    });
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

我的问题是,我想在点击加号图标时添加一个项目,但它没有添加。

1 个答案:

答案 0 :(得分:0)

重新构建适配器代码。 如果要将项添加到最后一个索引,则需要更改插入代码。

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>{

Context context;
List<String> steps;

public class ViewHolder extends RecyclerView.ViewHolder{
    ImageButton plus, minus;
    EditText step;

    public ViewHolder(View itemView) {
        super(itemView);
        plus = (ImageButton) itemView.findViewById(R.id.plus);
        minus = (ImageButton) itemView.findViewById(R.id.minus);
        step = (EditText) itemView.findViewById(R.id.step);

    }
}


public ListAdapter(List<String> steps, Context context){
    this.steps = steps;
    this.context = context;
}


@Override
public int getItemCount() {
    return steps.size();
}


@Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
    return new ViewHolder(v);
}


@Override
public void onBindViewHolder(ListAdapter.ViewHolder holder, final int i) {

    int x = holder.getLayoutPosition();

    holder.minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            try {
                steps.remove(position);
                notifyItemRemoved(position);
            }catch (ArrayIndexOutOfBoundsException e)
            {e.printStackTrace();}
        }
    });

    holder.plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            try {
                steps.add("");
                notifyItemInserted(steps.size()); // if insert not working then execute  notifyDataSetChanged(); 

            }catch (ArrayIndexOutOfBoundsException e)
            {e.printStackTrace();}
        }
    });

    holder.step.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int
                count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            steps.set(getAdapterPosition(), s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    if(steps.get(x).length() > 0) {
        holder.step.setText(steps.get(x));
    }
    else{
        holder.step.setText(null);
        holder.step.setHint("Next Step");
        holder.step.requestFocus();
    }

}


   public List<String> getStepList(){
      return steps;
   }
}

这会对你有帮助。