嗯,我想将字符串从适配器解析为片段,我该怎么做?因为我的应用程序上有一个空格字符串。
这是我适配器中的代码
适配器
public class listAdapter extends RecyclerView.Adapter<listAdapter.ViewHolder> {
private Simpleway simpleway = new Simpleway();
private ArrayList<DataFirebase> dataList;
private AppCompatActivity someActivity;
private TitleFragment fragmentTitle = new TitleFragment();
private ModelData model = new ModelData();
public listAdapter(ArrayList<DataFirebase> dataList, AppCompatActivity someActivity) {
this.dataList = dataList;
this.someActivity = someActivity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_label, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int positions) {
final DataFirebase labelList = dataList.get(positions);
viewHolder.textLabel.setText(labelList.getLabel());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String enough = labelList.getLabel();
model.setLabel(enough);
simpleway.toastMessage(someActivity, model.getLabel());
someActivity.getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right)
.replace(R.id.container_layout, fragmentTitle)
.addToBackStack(null)
.commit();
}
});
}
@Override
public int getItemCount() {
return dataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout linearLabel;
public TextView textLabel;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textLabel = itemView.findViewById(R.id.text_label);
linearLabel = itemView.findViewById(R.id.linearLabel);
}
}
}
所以我的适配器代码通常会打开我的titlefragment,并使用pojo传递字符串,但是,我在pojo上没有得到任何数据字符串,因此在这里查看我的TitleFragment类
标题片段
public class TitleFragment extends BaseFragment {
private DatabaseReference databaseReference;
private RecyclerView titleRecycleView;
private ArrayList<DataFirebase> listLabel;
private com.example.parzival.flashcard.adapter.titleAdapter titleAdapter;
private DataFirebase dataFirebase = new DataFirebase();
private ModelData modelData = new ModelData();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_title, container, false);
findView(view);
check();
// setDataToList(view);
initListener(view);
putLayoutManager(view);
return view;
}
private void check() {
if (modelData.getLabel().equals("")) {
simpleway.toastMessage(getContext(), "null");
} else {
simpleway.toastMessage(getContext(), modelData.getLabel());
}
}
private void setDataToList(View view) {
databaseReference.child(modelData.getLabel()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listLabel = new ArrayList<>();
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
DataFirebase dataText = dataSnap.getValue(DataFirebase.class);
dataText.setTitle(dataSnap.getKey());
listLabel.add(dataText);
}
titleAdapter = new titleAdapter(listLabel, TitleFragment.this);
titleRecycleView.setAdapter(titleAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
System.out.print(databaseError.getDetails() + "" + databaseError.getMessage());
}
});
}
private void putLayoutManager(View view) {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
titleRecycleView.setLayoutManager(layoutManager);
titleRecycleView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void findView(View view) {
databaseReference = FirebaseDatabase.getInstance().getReference();
titleRecycleView = view.findViewById(R.id.titleRecycleView);
}
@Override
public void initListener(View view) {
}
}
运行完唯一的东西之后,我从model.getLabel()
得到一个空对象怎么办?
我已经在模型上输入了字符串,不是吗?在model.setLabel(enough)
那为什么我会收到此错误?
编辑
我可能会尝试解释有关我的活动的更多信息。主要问题是我正在使用一个片段传递数据,并且尝试发布一些Activity类。
ListActivity
public class ListActivity extends BaseActivity{
private DatabaseReference databaseReference;
private RecyclerView labelRecycleView;
private ArrayList<DataFirebase> listLabel;
private listAdapter listAdapter;
private Button bInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
findView();
setDataToList();
initListener();
putLayoutManager();
}
private void setDataToList() {
simpleway.progressDialog(ListActivity.this);
databaseReference.child("Username").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listLabel = new ArrayList<>();
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
DataFirebase modelClass = dataSnap.getValue(DataFirebase.class);
modelClass.setLabel(dataSnap.getKey());
listLabel.add(modelClass);
}
listAdapter = new listAdapter(listLabel, ListActivity.this);
labelRecycleView.setAdapter(listAdapter);
simpleway.stopProgressDialog();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
System.out.println(databaseError.getDetails() + " " + databaseError.getMessage());
simpleway.stopProgressDialog();
}
});
}
private void putLayoutManager() {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
labelRecycleView.setLayoutManager(layoutManager);
labelRecycleView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void findView() {
databaseReference = FirebaseDatabase.getInstance().getReference();
labelRecycleView = findViewById(R.id.listRecyclerVIew);
bInput = findViewById(R.id.inputData);
}
@Override
public void initListener() {
bInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simpleway.startnextActivity(ListActivity.this, InputActivity.class);
}
});
}
}
我使用ListActivity传递的someActivity
而不是片段,这就是原因
答案 0 :(得分:0)
public interface CallBackListener {
void onCallBack(String data);// pass any parameter in your onCallBack which you want to return
}
内部片段创建匿名对象
CallBackListener listener=new CallBackListener() {
@Override
public void onCallBack(String data) {
// TODO Auto-generated method stub
}
};
从您的适配器传递侦听器的引用,并在onClick内调用onCallBack(data);
答案 1 :(得分:0)
首先创建一个界面
public interface PassBack {
void valueFromAdapter(String value);
}
然后像这样在您的片段中实现
public class TitleFragment extends BaseFragment implements PassBack{
private DatabaseReference databaseReference;
private RecyclerView titleRecycleView;
private ArrayList<DataFirebase> listLabel;
private com.example.parzival.flashcard.adapter.titleAdapter titleAdapter;
private DataFirebase dataFirebase = new DataFirebase();
private ModelData modelData = new ModelData();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_title, container, false);
findView(view);
check();
// setDataToList(view);
initListener(view);
putLayoutManager(view);
return view;
}
private void check() {
if (modelData.getLabel().equals("")) {
simpleway.toastMessage(getContext(), "null");
} else {
simpleway.toastMessage(getContext(), modelData.getLabel());
}
}
private void setDataToList(View view) {
databaseReference.child(modelData.getLabel()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listLabel = new ArrayList<>();
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
DataFirebase dataText = dataSnap.getValue(DataFirebase.class);
dataText.setTitle(dataSnap.getKey());
listLabel.add(dataText);
}
titleAdapter = new titleAdapter(listLabel, TitleFragment.this);
titleRecycleView.setAdapter(titleAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
System.out.print(databaseError.getDetails() + "" + databaseError.getMessage());
}
});
}
private void putLayoutManager(View view) {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
titleRecycleView.setLayoutManager(layoutManager);
titleRecycleView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void findView(View view) {
databaseReference = FirebaseDatabase.getInstance().getReference();
titleRecycleView = view.findViewById(R.id.titleRecycleView);
}
@Override
public void initListener(View view) {
}
@Override
public void valueFromAdapter(String value) {
//here you will get the value
}
}
然后在适配器中像这样使用它
public class listAdapter extends RecyclerView.Adapter<listAdapter.ViewHolder> {
private Simpleway simpleway = new Simpleway();
private ArrayList<DataFirebase> dataList;
private AppCompatActivity someActivity;
private TitleFragment fragmentTitle = new TitleFragment();
private ModelData model = new ModelData();
public listAdapter(ArrayList<DataFirebase> dataList, AppCompatActivity someActivity) {
this.dataList = dataList;
this.someActivity = someActivity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_label, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int positions) {
final DataFirebase labelList = dataList.get(positions);
viewHolder.textLabel.setText(labelList.getLabel());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String enough = labelList.getLabel();
model.setLabel(enough);
simpleway.toastMessage(someActivity, model.getLabel());
someActivity.valueFromAdapter(enough)//this is how you send your value
}
});
}
@Override
public int getItemCount() {
return dataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout linearLabel;
public TextView textLabel;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textLabel = itemView.findViewById(R.id.text_label);
linearLabel = itemView.findViewById(R.id.linearLabel);
}
}
}
答案 2 :(得分:0)
在适配器中
A。创建界面
public interface Listener {
void callback(String data);
}
B。创建字段
private Listener listener;
C。在onCreateViewHolder上初始化监听器
listener = (Listener)viewGroup.getContext()
D。设定值
String enough = labelList.getLabel();
listener.callback(enough) // Set the value here
在您的片段中
(单向)
(另一种方式)
答案 3 :(得分:0)
解决方案
我已经尝试使用回答的问题并陷入困境。我认为它主要用于活动(如果我错了,请纠正我)。因此,我通过使用参数来解决这种情况的唯一方法
import React, { Component } from 'react';
import { Block } from 'vcc-ui';
class ImportStep extends Component {
render() {
return (
<Block>
<h3>{this.props}</h3>
</Block>
)
}
}
export default ImportStep;
之后,我使用Bundle放置参数
String enough = labelList.getLabel();
TitleFragment titleFragment = new TitleFragment();
FragmentTransaction fragmentTransaction = someActivity
.getSupportFragmentManager()
.beginTransaction();
然后我尝试打开片段
Bundle bundle = new Bundle();
bundle.putString("Check", enough);
titleFragment.setArguments(bundle);
所以我使用标题片段上的参数作为字符串
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right,
R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right)
.replace(R.id.container_layout, titleFragment)
.addToBackStack(null)
.commit();
及其完成