目标:使用RecyclerView将EditText字段添加到活动
发生了什么:我希望只要"添加"就可以将带有ImageView和EditText的LinearLayouts(水平)添加到屏幕上。单击按钮。他们直到我才出现:
-1)输入edittext字段并单击“完成”
-2)点击添加
-3)单击我已输入的EditText字段,然后再次单击“完成”,或单击“关闭”
它应该在第2步之后添加,而不是第3步。我的想法是它与notifyDataSetChanged有关,但我不确定是什么?
研究完成:花了最近几天浏览文档,教程,YouTube视频和stackoverflow搜索
测试完成:尝试移动调用notifyDataSetChanged()的方法的移动位置; 试图改变布局的尺寸 尝试更改inputType
MyAdapter.java:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private ArrayList<LinearLayout> mDataSet;
private Context mContext;
private Random mRandom = new Random();
public MyAdapter(Context context){
mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public EditText mTextEditName, mTextEditLocation;
public LinearLayout mRelativeLayout; //from the (Main)Activity XML
public ViewHolder(View v){
super(v);
mTextEditName = (EditText) v.findViewById(R.id.nameOfBusinessET);
mRelativeLayout = (LinearLayout) v.findViewById(R.id.rl);
}
}
void updateDataSet(ArrayList<LinearLayout> myArrayList) {
mDataSet = myArrayList;
notifyDataSetChanged();
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
// Create a new View
View v = LayoutInflater.from(mContext).inflate(R.layout.row_layout,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextEditName.setText("");
}
@Override
public int getItemCount(){
return mDataSet.size();
}
}
RegisterBusinessActivity.java:
public class RegisterBusinessActivity extends Activity {
EditText businessLocationET;
//FIREBASE ITEMS
private static FirebaseUser currentUser;
private static final String TAG = "RealtimeDB";
private FirebaseDatabase database;
private DatabaseReference dbRef;
private EditText businessName;
//RECYCLERVIEW ITEMS
private Context mContext;
LinearLayout mLinearLayout;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
int position;
ArrayList<LinearLayout> linearLayoutList = new ArrayList<LinearLayout>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_businessprofile);
Button addLocationBtn = (Button) findViewById(R.id.addLocationBtn);
businessLocationET = (EditText) findViewById(R.id.businessLocationET);
// Get the widgets reference from XML layout
mLinearLayout = (LinearLayout) findViewById(R.id.rl);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mContext = getApplicationContext(); // Get the application context
linearLayoutList.add(mLinearLayout);
// Define a layout for RecyclerView
mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
// Initialize a new instance of RecyclerView Adapter instance
mAdapter = new MyAdapter(mContext);
// Set the adapter for RecyclerView
mRecyclerView.setAdapter(mAdapter);
mAdapter.updateDataSet(linearLayoutList);
//FIREBASE FIELDS
businessName = (EditText) findViewById(R.id.nameOfBusinessET);
database = FirebaseDatabase.getInstance();
dbRef = database.getReference("/data");
currentUser =
FirebaseAuth.getInstance().getCurrentUser();
//ADD EXTRA LOCATIONS
addLocationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
linearLayoutList.add(mLinearLayout);
}
});
DatabaseReference.CompletionListener completionListener =
new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError,
DatabaseReference databaseReference) {
if (databaseError != null) {
notifyUser(databaseError.getMessage());
}
}
};
private void notifyUser(String message) {
Toast.makeText(RegisterBusinessActivity.this, message,
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
在添加&#39;之后,您似乎没有更新您的回收商。点击事件。您只需将项目添加到列表中,而无需更新适配器/ recyclerview。
一种选择就是使用您添加的特定位置:
mAdapter.notifyItemInserted(yourItems.size() - 1);
或优于使用:
mAdapter.notifyDataSetChanged();
所以它看起来像那样:
addLocationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
linearLayoutList.add(mLinearLayout);
mAdapter.updateDataSet(linearLayoutList);
}
});