我有一个活动。我正在创建一个File,它在创建活动时有一些数据。正确创建所有文件并正确写入数据。 以下是我的代码
<div class="section_1">
Starter item 1 Start Price 1 Starter Description 1
Starter item 2 Start Price 2 Starter Description 2
</div>
<div class="section_2">
Starter item 1 Start Price 1 Starter Description 1
Starter item 2 Start Price 2 Starter Description 2
</div>
我正确编写的Toast显示DEF0和HIJ 0,但它们不会显示在recyclerview中。以下是屏幕截图
以下是我的适配器类
public class MyRecordingsActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecordingsAdapter recordingsAdapter;
private ArrayList<Recording> recordingArrayList;
private File userRecordingFile;
private static final String USER_MIX_DIR = "UserMix";
private String lines[]=new String[]{};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_recordings);
for(int i=0;i<5;i++){
try{
userRecordingFile = new File(createRecordingFiles(), "Recording"+i+".txt");
FileWriter writer = new FileWriter(userRecordingFile);
writer.append("DEF"+i+"\nHIJ "+i);
writer.flush();
writer.close();
}
catch (Exception e){
e.printStackTrace();
}
}
getSupportActionBar().hide();
recordingArrayList=new ArrayList<>();
recyclerView=findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);
readFiles();
Toast.makeText(getApplicationContext(),lines[0],Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),lines[1],Toast.LENGTH_SHORT).show();
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[0],lines[1]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[2],lines[3]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[4],lines[5]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[6],lines[7]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[8],lines[9]));
recordingsAdapter=new RecordingsAdapter(recordingArrayList);
recyclerView.setAdapter(recordingsAdapter);
}
public File createRecordingFiles() {
File dirRoot = getExternalCacheDir();
File workDir = new File(dirRoot, USER_MIX_DIR);
//Toast.makeText(getApplicationContext(), "HI", Toast.LENGTH_SHORT).show();
if (!workDir.exists()) {
workDir.mkdirs();
File recordingFile = new File(workDir, "Recording File ");
try {
recordingFile.createNewFile();
} catch (IOException e) {
}
}
return workDir;
}
public void readFiles(){
StringBuilder text = new StringBuilder();
BufferedReader br=null;
try {
File dirRoot = getExternalCacheDir();
File workDir = new File(dirRoot, USER_MIX_DIR);
for(int i=0;i<5;i++){
File file = new File(workDir,"Recording"+i+".txt");
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
lines=text.toString().split("\n");
}
}
br.close() ;
}catch (IOException e) {
e.printStackTrace();
}
}
}
在recyclerview中不显示DEF0和HIJ 0。我无法理解为什么他们没有在我的recyclerview中显示。我的日志中没有错误。任何帮助将不胜感激
答案 0 :(得分:3)
你这样做是错误的......
您在recyclerview
中添加了五个项目,但首先考虑的是标题。
因此,这会将您的第一个项目作为标题,并且您会看到其他四个项目。
您需要做的是,将虚拟物品作为第一项传递,然后添加您的5项。
这样你可以解决问题。您可以尝试这种方式:
recordingArrayList.add(new Recording(R.drawable.ic_launcher,"", ""));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[0],lines[1]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[2],lines[3]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[4],lines[5]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[6],lines[7]));
recordingArrayList.add(new Recording(R.drawable.ic_launcher,lines[8],lines[9]));
或者你也可以这样做:
更改适配器代码
@Override
public int getItemCount() {
return recordingArrayList.size() + 1;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Recording recording;
if(holder.view_type==TYPE_LIST){
recording=recordingArrayList.get(position - 1);
...
}else if (holder.view_type == TYPE_HEAD){
holder.typeHeaderTextView.setText("TYPE");
holder.titleHeaderTextView.setText("TITLE");
holder.dateTimeHeaderTextView.setText("DATE/TIME");
}
}
但您需要以这种方式验证空列表
答案 1 :(得分:2)
您的回收商视图可能隐藏在工具栏下方。给出56dp的上限。
android:layout_marginTop="56dp"
答案 2 :(得分:2)
试试这个你的第一个位置是你的标题。
使用此
@Override
public int getItemViewType(int position) {
return TYPE_LIST;
}
而不是
@Override
public int getItemViewType(int position) {
if(position==0){
return TYPE_HEAD;
}
return TYPE_LIST;
}
答案 3 :(得分:1)
对我有用的是,首先从数组中删除任何空值(因为第一个元素可能为空),然后再次添加空值。这样,我避免了重复空值。我不使用“ if”或任何适配器实现的方法,因此使我的应用程序更高效。 我的代码如下:
myArray.remove(null); // here I remove any preview added null value as first (0 position) element
myArray.add(0, null); //now I add null as first element
//this way I am avoiding showing content with a null values
我希望这对某人有帮助
答案 4 :(得分:0)
显示toast是因为你的卡片在那里,但它被工具栏隐藏了。您可以为RecyclerView添加一些保证金:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:app="schemas.android.com/apk/res-auto"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_marginTop:"?android:attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView" />
</RelativeLayout>
答案 5 :(得分:0)
如果你想在不传递活动的第一个空白数据的情况下添加标题,那么你需要在适配器的第一个位置添加空白条目,如下所示。
在适配器中替换此代码:
public RecordingsAdapter(ArrayList<Recording> recordingArrayList) {
this.recordingArrayList = recordingArrayList;
this.recordingArrayList.add(0,null);
}