java - 自定义适配器未显示片段中的任何内容
问题描述
我有一个名为的自定义适配器MyAdapter
,我试图加载一个应该插入到列表视图中的布局,但即使 Android 没有给出错误,视图也显示为空白。
在下一个屏幕截图中,我展示了构建我的应用程序后得到的结果。
我期望加载的是我的布局到我的列表视图中:
列表显示
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.NewsFragment">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/list_item"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageabdcf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="25dp"
tools:srcCompat="@android:drawable/btn_star_big_on" />
<TextView
android:id="@+id/textabcdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
新闻片段.java
public class NewsFragment extends Fragment {
private ListView listView;
private List<String> names;
public NewsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_news, container, false);
listView = (ListView) v.findViewById(R.id.listView);
names = new ArrayList<String>();
names.add("Fernando");
names.add("Roberto");
names.add("Torres");
names.add("Urban");
ArrayAdapter<String> adapter = new ArrayAdapter<>(v.getContext(), android.R.layout.simple_list_item_1, names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getView().getContext(), "Clicked: " + names.get(position), Toast.LENGTH_LONG).show();
}
});
MyAdapter myAdapter = new MyAdapter(v.getContext(), R.layout.card_view_news, names);
listView.setAdapter(myAdapter);
// Inflate the layout for this fragment
return v;
}
}
我的适配器.java
public class MyAdapter extends BaseAdapter {
private Context context;
private int layout;
private List<String> names;
public MyAdapter(Context context, int layout, List<String> names) {
this.context = context;
this.layout = layout;
this.names = names;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return this.names.get(position);
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = convertView;
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
v = layoutInflater.inflate(R.layout.fragment_news, null);
String currentName = names.get(position);
//currentName = (String) getItem(position);
TextView textView = (TextView) v.findViewById(R.id.textabcdf);
textView.setText(currentName);
return v;
}
}
我期待的是:
解决方案
推荐阅读
- permissions - 无法更改 AppleScript 中文件夹的权限根目录
- c - 将 C 转换为 MIPS 汇编语言
- ios - 仅在来自 TestFlight 的发布方案上的公共端点上出现 403 错误
- python - 如何在python中匹配正则表达式?
- python - 在发布之前检查用户是否在组中。如何选择我的功能中的所有可用组?
- discord - Discord.js 变量在定义时不存在?
- python - 如果当天没有事件,则在数据框中添加单个“错过”事件
- nginx - Nginx 附加到原始网址
- python - 将数据转换为逗号分隔
- javascript - 侧栏中的自定义图标Vuejs导航