我是android工作室的新手,遇到了一个问题。我目前正在创建一个功能,其中将有一个回收器视图包含常见问题,一旦点击常见问题行,用户将被带到一个新的活动,那里将有更多的信息。
我是如何尝试通过类中的arraylist来实现这一点的,在这里我存储了将传递给行的信息,然后是最后一个活动。但是,由于某种原因,当我完成代码编写后,在最后一个活动中只显示arraylist中的最后一个元素(即,我将在recyclerview中显示5个不同的元素,但无论单击哪一行,都只显示最后一个元素的信息)
请给我一些想法,因为这种方法的代码已经工作过,我不知道为什么它现在不工作。
这是我输入数据的类
import java.util.ArrayList;
public class InfoPageClass {
public InfoPageClass(String infoCode, String infoName, String infoCategory, String infoContent){
this.infoCode = infoCode;
this.infoName = infoName;
this.infoCategory = infoCategory;
this.infoContent = infoContent;
}
private String infoCode;
private String infoName;
private String infoCategory;
private String infoContent;
public String getInfoName() {
return infoName;
}
public void setInfoName(String infoName) {
this.infoName = infoName;
}
public String getInfoCategory() {
return infoCategory;
}
public void setInfoCategory(String infoCategory) {
this.infoCategory = infoCategory;
}
public String getInfoContent() {
return infoContent;
}
public void setInfoContent(String infoContent) {
this.infoContent = infoContent;
}
public String getInfoCode() {
return infoCode;
}
public void setInfoCode(String infoCode) {
this.infoCode = infoCode;
}
public static ArrayList<InfoPageClass> getInfoPage(){
ArrayList<InfoPageClass> infoPage = new ArrayList<>();
// infoPage.add(new InfoPageClass("111", "How do I use this app?", "General", "This app was created with the objective to promote healthy-eating and improve the general " + "quality of life by educating individuals on what they include in their diet. \n" +
// "This is achieved via key lessons teaching the basics of healthy eating, as well as a database that details the nutritional value" +
// "of the foods available to individuals. Finally, through gamification, the app aims to cement this knowledge by testing individuals through an interactive quiz format"));
infoPage.add(new InfoPageClass("222", "blah1","blah1", "blah1"));
infoPage.add(new InfoPageClass("333", "blah2","blah2", "sfdsdfwesfwefwdfsdfsdf"));
return infoPage;
}
public static InfoPageClass getInfoPage(String infoCode){
ArrayList<InfoPageClass> infoPage = InfoPageClass.getInfoPage();
for (final InfoPageClass infoName : infoPage){
if(infoName.getInfoCode().equals(infoCode)){
return infoName;
}
}
return infoPage.get(infoPage.size()-1);
}
}
这是我的回收代码
public class InfoPageList extends AppCompatActivity {
RecyclerView mRecyclerView3;
private InfoPageAdapter mAdapter3;
private RecyclerView.LayoutManager layoutManager3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_page_list);
mRecyclerView3 = findViewById(R.id.recyclerView3);
mRecyclerView3.setHasFixedSize(true);
layoutManager3 = new LinearLayoutManager(this);
mRecyclerView3.setLayoutManager(layoutManager3);
InfoPageAdapter.Listener listener = new InfoPageAdapter.Listener() {
@Override
public void onClick(View view, String infoCode) {
launchDetailActivity(infoCode);
}
};
mAdapter3 = new InfoPageAdapter(InfoPageClass.getInfoPage(), listener);
mRecyclerView3.setAdapter(mAdapter3);
}
//create method to change to infopagedetailed activity where more faq questions are answered
private void launchDetailActivity(String message){
Intent intent = new Intent(InfoPageList.this, InfoPageDetailed.class);
intent.putExtra(InfoPageDetailed.INTENT_MESSAGE, message);
startActivity(intent);
}
}
这是设置上一个活动的文本的代码(其中显示了错误的信息)
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class InfoPageDetailed extends AppCompatActivity {
public static final String INTENT_MESSAGE = "au.edu.unsw.infs3634.gamifiedlearning.intent_message";
TextView mInfoName;
TextView mInfoContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_page_detailed);
Intent intent = getIntent();
String infoCode = intent.getStringExtra(INTENT_MESSAGE);
final InfoPageClass infoPageClass = InfoPageClass.getInfoPage(infoCode);
mInfoName = findViewById(R.id.tvInfoTitle2);
mInfoContent = findViewById(R.id.tvInfoContent);
//Set the values
mInfoName.setText(infoPageClass.getInfoName());
mInfoContent.setText(infoPageClass.getInfoContent());
}
}
非常感谢您的帮助,谢谢!
编辑:我认为这可能是infopageclass中for循环的一个问题,但是同样,我也不确定有什么问题,因为这个方法以前已经工作过了
暂无答案!
目前还没有任何答案,快来回答吧!