我尝试使用recylerView创建一个不同公司的列表。所以发生了什么我显示底部工作表片段,其中我填充了不同公司名称的列表。我要求用户添加任何公司的不同名称,如果他们想要的。我从对话框中获得公司名称,并将该名称添加到recylerView列表。
但是当底部表单消失时,我丢失了用户添加的公司名称。我希望保留用户添加的公司名称。基本上,我希望创建一个列表视图,并希望使用对话框从用户那里获得列表视图项。
希望我能详细解释我的问题。下面是我的代码。
public class Item
{
public String companyName;
public Item(String companyName) {
this.companyName = companyName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;}}`
在它下面我的适配器类
public class CustomAdapter_1 extends RecyclerView.Adapter<CustomAdapter_1.myViewHolder> {
List<Item> customAdapters;
Context context;
public CustomAdapter_1(Context context, List<Item> customAdapters) {
this.customAdapters = customAdapters;
this.context = context;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.custom_adapter_1, parent, false );
myViewHolder myViewHolder = new myViewHolder ( v );
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
holder.radioButton.setText ( customAdapters.get ( position ).getCompanyName () );
}
@Override
public int getItemCount() {
return customAdapters.size ();
}
public class myViewHolder extends RecyclerView.ViewHolder {
RadioButton radioButton;
TextView textView;
public myViewHolder(@NonNull View itemView) {
super ( itemView );
radioButton = (RadioButton) itemView.findViewById ( R.id.rdbtn1 );
}
}
public void AddItem(Item item, int position) {
customAdapters.add ( position, item );
}}
这是我的碎片
public class CustomFragment_1 extends BottomSheetDialogFragment {
private RecyclerView recyclerView;
private CustomAdapter_1 mAdapter;
ArrayList<Item> listOfItem = new ArrayList<> ();
String cName, listString;
StringBuffer sb = new StringBuffer ();
Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate ( R.layout.fragment_custom_1, container, false );
recyclerView = (RecyclerView) view.findViewById ( R.id.customRV );
btn = (Button) view.findViewById ( R.id.button1 );
LinearLayoutManager linearLayoutManager = new LinearLayoutManager ( getContext () );
recyclerView.setLayoutManager ( linearLayoutManager );
mAdapter = new CustomAdapter_1 ( getContext (), listOfItem );
recyclerView.setAdapter ( mAdapter );
listOfItem.add ( new Item ( "Company 1" ) );
listOfItem.add ( new Item ( "Company 2" ) );
listOfItem.add ( new Item ( "Company 3" ) );
btn.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View view) {
final FlatDialog flatDialog = new FlatDialog ( getActivity () );
flatDialog.setTitle ( "Company Name" )
.setSubtitle ( "Please add the name of the company" )
.setFirstTextFieldHint ( "Company Name" )
.setFirstButtonText ( "ADD" )
.setSecondButtonText ( "CANCEL" )
.withFirstButtonListner ( new View.OnClickListener () {
@Override
public void onClick(View view) {
cName = flatDialog.getFirstTextField ();
listOfItem.add ( new Item ( flatDialog.getFirstTextField () ) ) ;
flatDialog.dismiss ();
}
} )
.withSecondButtonListner ( new View.OnClickListener () {
@Override
public void onClick(View view) {
flatDialog.dismiss ();
}
} )
.show ();
}
} );
return view;
}}
我真的很感谢你的时间,我感谢所有的时间。谢谢
1条答案
按热度按时间yfjy0ee71#
试试看:
在对话框的提交按钮上单击:
相反,如果将“Company 1”作为一个值,您可以将该值从您正在从中获取输入的edittext中输入。