gpt4 book ai didi

java - 运行应用程序后 RecyclerView 列表不显示任何内容

转载 作者:行者123 更新时间:2023-12-02 02:13:44 25 4
gpt4 key购买 nike

我的应用程序应该采用字符串数组并在 RecyclerView 中查看它。一切看起来都很好,但是当我运行该应用程序时,它什么也没显示!
首先,我在 String.xml 文件中声明一个名为 "array_countries" 的字符串数组
这个字符串文件

<resources>
<string name="app_name">StringArrayWithRecyclerView</string>

<string-array name="array_countries">
<item>Australia</item>
<item>Canada</item>
<item>Cambodia</item>
<item>China</item>
<item>England</item>
<item>France</item>
<item>Japan</item>
<item>Rusia</item>
<item>USA</item>
</string-array>

country_row.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dummy"
android:textSize="30sp"
android:textColor="@color/black"
/>

</LinearLayout>

然后我创建CountriesAdapter

    package pro.cs_is.com.stringarraywithrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;


public class CountriesAdapter extends RecyclerView.Adapter<CountriesAdapter.CountriesHolder> {

private List<String> list = Collections.emptyList();
private Context context;

public CountriesAdapter(Context context, List list) {
this.context = context;
this.list = list;

}

@Override
public CountriesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.countries_row, parent, false);
CountriesHolder holder = new CountriesHolder(view);
return holder;

}

@Override
public void onBindViewHolder(final CountriesHolder holder, int position) {
String country = list.get(position);
holder.textView.setText(country);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(context, "You clicked " +
holder.textView.getText(), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});

}

@Override
public int getItemCount() {
return list.size();
}


class CountriesHolder extends RecyclerView.ViewHolder {
TextView textView;

public CountriesHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
}

最后是MainActivity

    package pro.cs_is.com.stringarraywithrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CountriesAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> countries = Arrays.asList(getResources().getStringArray(R.array.array_countries));
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
adapter = new CountriesAdapter(this, countries);
adapter.notifyDataSetChanged();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
DividerItemDecoration dividerItemDecoration =
new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(adapter);


}
}

最佳答案

您忘记添加

recyclerView.setLayoutManager(linearLayoutManager);

关于java - 运行应用程序后 RecyclerView 列表不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49591129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com