我创建了一个输入类,例如3个输入Map:String
,List
,List
,
然后在RecyclerView中使用它
在我的构造函数中,我在列表中输入字符串然后清除它,所以我必须从mainExampleClass
我该如何访问它?例如,在包含示例列表的列表中
List<Example> exampleList;
exampleList.get(0);
如何在get(i)
之后访问数据?
RecylerView数据对象:
public class Example {
private static MainExampleObject exampleObject;
private static String StepName;
private static List<String> TemporaryCode = new ArrayList<>(), TemporaryExplanation = new ArrayList<>();
public Example(MainExampleObject exampleObject) {
this.exampleObject = exampleObject;
}
public static void addCode(String code) {
TemporaryCode.add(code);
}
public static void addExplanation(String explanation) {
TemporaryExplanation.add(explanation);
}
public static void setStepName(String stepName) {
StepName = stepName;
}
public static MainExampleObject getExampleObject() {
return exampleObject;
}
static List<String> getTemporaryCode() {
return TemporaryCode;
}
static List<String> getTemporaryExplanation() {
return TemporaryExplanation;
}
static String getStepName() {
return StepName;
}
public static void addExample(){
exampleObject = new MainExampleObject(StepName, TemporaryCode, TemporaryExplanation);
TemporaryCode.clear();
TemporaryExplanation.clear();
}
}
示例对象类:
class MainExampleObject {
private static String StepName;
private static List<String> Code, Explanation;
MainExampleObject(String stepHeader, List<String> code, List<String> explanation) {
StepName = stepHeader;
Code = code;
Explanation = explanation;
}
public static String getStepNamex() {
return StepName;
}
}
更多详情
我将数据添加到列表
的方法public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Example.setStepName(String.valueOf(dataSnapshot.getKey()));
for (DataSnapshot childSnapshot : dataSnapshot.child("Code").getChildren()) {
Example.addCode(String.valueOf(childSnapshot.getValue()));
}
for (DataSnapshot childSnapshot : dataSnapshot.child("Explaination").getChildren()) {
Example.addExplanation(String.valueOf(childSnapshot.getValue()));
}
addExample();
exampleList.add(new Example(getExampleObject()));
adapter.notifyDataSetChanged();
}
适配器
List<Example> exampleList;
ViewLesson viewLesson;
public interface OnItemSelectedListenerCustom {
void onItemClicked(int selectedPosition);
}
public class ExampleHolder extends RecyclerView.ViewHolder { // here is where you define what text have value
CardView cv;
LinearLayout ll;
public ExampleHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.CV);
ll = (LinearLayout) itemView.findViewById(R.id.CV_LL);
}
}
public ExampleAdapter(ViewLesson viewLesson, List<Example> exampleList) {
this.viewLesson = viewLesson;
this.exampleList = exampleList;
}
@Override
public ExampleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
return new ExampleHolder(itemView);
}
@Override
public void onBindViewHolder(final ExampleHolder holder, int position) {
TextView tv = new TextView(holder.cv.getContext());
if (Example.getStepName() != null) {
tv.setText(Example.getStepName());
holder.ll.addView(tv);
}
if (Example.getTemporaryCode() != null && Example.getTemporaryExplanation() != null) {
int i = 0;
for (String code : Example.getTemporaryCode()) {
tv = new TextView(holder.cv.getContext());
tv.setText(code);
holder.ll.addView(tv);
tv = new TextView(holder.cv.getContext());
tv.setText(Example.getTemporaryCode().get(i));
holder.ll.addView(tv);
i++;
}
tv = new TextView(holder.cv.getContext());
tv.setText(String.valueOf(exampleList.get(0).getClass().toString()));
holder.ll.addView(tv);
tv = new TextView(holder.cv.getContext());
tv.setText(String.valueOf(exampleList.get(1).getClass().toString()));
holder.ll.addView(tv);
}
}
@Override
public int getItemCount() {
return exampleList.size();
}
}
这是我想要单独获取此数据示例
的确切行StepName = 2 Adding b, TemporaryCode = [1aaaa, 2baaa, 3caaa], TemporaryExplanation = [1sttt, 2nddd, 3rddd]
其中
2 Adding b
[1aaaa, 2baaa, 3caaa]
[1sttt, 2nddd, 3rddd]
The Line
tv.setText(String.valueOf(exampleList.get(0).getClass().toString()));
答案 0 :(得分:3)
基于这一行,你似乎想要一个String表示你班级中的所有变量。
exampleList.get(0).getClass().toString()
好吧,getClass()
会返回一个Java类变量,Class
上的toString不会告诉你的字段。
请参阅here并将其应用到您的班级之后您修复了您认为自己需要的所有内容static
无处不在
如果操作正确,这将有效。
setText(String.valueOf(exampleList.get(0)))