1

私はアンドロイドRecyclerViewを初めて使っていますが、RecyclerにEdittextフィールドを追加しようとしたときにエラーが発生します。これを行う方法が見つかりません。リサイクラーにEditTextを追加

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

}

CustomRecyclerAdapterが

データクラスが持っている}

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

です:誰でもこの問題と:)事前に感謝を解決してください:)これは私のmainactivityコードです単に含有物

public class Data { 
public String text; 

public Data(String text) { 
    this.text = text; 

} 

public String getText() { 
    return text; 
} 

public void setText(String text) { 
    this.text = text; 
} 

}

答えて

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

} 
} 

これは

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

     } 

    } 

} 

} 
RecyclerViewAdapter.java

です