我是新手android RecyclerView,当我尝试将Edittext字段添加到Recycler时,它会出现Error.I无法找到如何执行此操作。任何人请解决这个问题,并提前感谢:) :)这是我的主要活动代码:
app.controller("serviceHistoryController",function($scope){
var menuitems = [{"sutid":"1","name":"AA"},
{"sutid":"2","name":"BB"},
{"sutid":"3","name":"CC"},
{"sutid":"4","name":"DD"},
{"sutid":"4","name":"EE"}]
$scope.sutlist = menuitems;
$scope.selectedSUT;
$scope.sutselected = function(sutname){
$scope.selectedSUT = sutname;
alert("dropdown sut selected:"+$scope.selectedSUT.name);
}
});
}
CustomRecyclerAdapter是
public class MainActivity extends AppCompatActivity{
private RecyclerView mRecyclerView;
private CustomRecyclerAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private EditText mText;
private EditText mColor;
private List<Data> mData = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing views.
mText = (EditText) findViewById(R.id.textEt);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
// If the size of views will not change as the data changes.
mRecyclerView.setHasFixedSize(true);
// Setting the LayoutManager.
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// Setting the adapter.
mAdapter = new CustomRecyclerAdapter();
mRecyclerView.setAdapter(mAdapter);
}
// Called when add button is clicked.
public void addItem(View view) {
// Add data locally to the list.
Data dataToAdd = new Data(
mText.getText().toString());
mData.add(dataToAdd);;
// Update adapter.
mAdapter.addItem(mData.size() - 1, dataToAdd);
}
}
Data类只包含
package com.example.niroj.jsonandsqlite;
public class CustomRecyclerAdapter extends
RecyclerView.Adapter<RecyclerViewHolder> {
private List<Data> mData = Collections.emptyList();
public CustomRecyclerAdapter() {
// Pass context or other static stuff that will be needed.
}
public void updateList(List<Data> data) {
mData = data;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.list_item, viewGroup, false);
return new RecyclerViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position) {
viewHolder.title.setText(mData.get(position).text);
}
public void addItem(int position, Data data) {
mData.add(position, data);
notifyItemInserted(position);
}
public void removeItem(int position) {
mData.remove(position);
notifyItemRemoved(position);
}
}
答案 0 :(得分:3)
这个怎么样?这是我的代码: -
这是MainActivity.java
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.OnItemClickListener{
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;
EditText nameField;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
/*
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
*/
myRecyclerViewAdapter = new RecyclerViewAdapter(this);
myRecyclerViewAdapter.setOnItemClickListener(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
nameField = (EditText)findViewById(R.id.namefield);
btnAdd = (Button)findViewById(R.id.addbutton);
btnAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String newName = nameField.getText().toString();
if(!newName.equals("")){
if(myRecyclerViewAdapter.getItemCount()>1){
myRecyclerViewAdapter.add(1, newName);
}else{
myRecyclerViewAdapter.add(0, newName);
}
}
}
});
}
@Override
public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
Toast.makeText(this,
"Remove " + position + " : " + item.getItemName(),
Toast.LENGTH_SHORT).show();
myRecyclerViewAdapter.remove(position);
}
}
这是RecyclerViewAdapter.java
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {
private List<String> itemsName;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public RecyclerViewAdapter(Context context){
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
}
@Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.list_item, parent, false);
return new ItemHolder(itemView, this);
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
holder.setItemName(itemsName.get(position));
}
@Override
public int getItemCount() {
return itemsName.size();
}
public void setOnItemClickListener(OnItemClickListener listener){
onItemClickListener = listener;
}
public OnItemClickListener getOnItemClickListener(){
return onItemClickListener;
}
public interface OnItemClickListener{
public void onItemClick(ItemHolder item, int position);
}
public void add(int location, String iName){
itemsName.add(location, iName);
notifyItemInserted(location);
}
public void remove(int location){
if(location >= itemsName.size())
return;
itemsName.remove(location);
notifyItemRemoved(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RecyclerViewAdapter parent;
TextView textItemName;
public ItemHolder(View itemView, RecyclerViewAdapter parent) {
super(itemView);
itemView.setOnClickListener(this);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.item_name);
}
public void setItemName(CharSequence name){
textItemName.setText(name);
}
public CharSequence getItemName(){
return textItemName.getText();
}
@Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if(listener != null){
listener.onItemClick(this, getPosition());
}
}
}
}