Java 自定义ArrayAdapter在我的片段中显示空白屏幕
所以我只是在安卓上混了一段时间,遇到了一点小麻烦。出于某种原因,我实例化ListOfJokesTypesAdapter的片段没有显示一个用我的JokeData类中的数据填充的listview 我得到的只是一个空白屏幕(没有错误或任何类似的性质) 这只是我一直在做的一件概念验证的事情,所以任何帮助都将不胜感激。为什么当我的MainClassAdapter工作正常时,这个特定的自定义适配器不工作 代码 我的笑话课:Java 自定义ArrayAdapter在我的片段中显示空白屏幕,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,所以我只是在安卓上混了一段时间,遇到了一点小麻烦。出于某种原因,我实例化ListOfJokesTypesAdapter的片段没有显示一个用我的JokeData类中的数据填充的listview 我得到的只是一个空白屏幕(没有错误或任何类似的性质) 这只是我一直在做的一件概念验证的事情,所以任何帮助都将不胜感激。为什么当我的MainClassAdapter工作正常时,这个特定的自定义适配器不工作 代码 我的笑话课: public class Joke { private String jokeSe
public class Joke {
private String jokeSetup;
private String jokePunchline;
public Joke(String jokeSetup, String jokePunchline) {
this.jokeSetup = jokeSetup;
this.jokePunchline = jokePunchline;
}
public String getJokeSetup() {
return jokeSetup;
}
public void setJokeSetup(String jokeSetup) {
this.jokeSetup = jokeSetup;
}
public String getJokePunchline() {
return jokePunchline;
}
public void setJokePunchline(String jokePunchline) {
this.jokePunchline = jokePunchline;
}
}
我的笑话课堂
public class JokeListData {
private String listName;
private List<Joke> arrayListOfJokes;
public JokeListData(String listName, List<Joke> arrayListOfJokes) {
this.listName = listName;
this.arrayListOfJokes = arrayListOfJokes;
}
public String getListName() {
return listName;
}
public void setListName(String listName) {
this.listName = listName;
}
public List<Joke> getArrayListOfJokes() {
return arrayListOfJokes;
}
public void setArrayListOfJokes(ArrayList<Joke> arrayListOfJokes) {
this.arrayListOfJokes = arrayListOfJokes;
}
}
片段管理器
package com.example.taranveer.jokeapplicationactual
导入android.app.Activity;
导入android.os.Bundle
/**
*由塔兰维尔于2014年7月22日创建。
*/
公共类FragmentActivityManager扩展活动{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container);
Bundle args = getIntent().getExtras();
if(findViewById(R.id.container) != null){
if(args != null){
if(args.getInt("randomjoke") == 1){
RandomJokeFragment randomJokeFragment = new RandomJokeFragment();
getFragmentManager().beginTransaction()
.replace(R.id.container, randomJokeFragment)
.commit();
}
}
}
if(findViewById(R.id.container) != null){
if(args!=null){
if(args.getInt("listofjoketypes") == 2){
ListOfJokeTypesFragment listOfJokeTypesFragment = new ListOfJokeTypesFragment();
getFragmentManager().beginTransaction()
.replace(R.id.container,listOfJokeTypesFragment)
.commit();
}
}
}
}
}
相关XML:
joke\u type\u fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/jokeTypeListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_margin="5dp"
android:gravity="center_vertical"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#eeeeee"
android:textStyle="bold"
android:layout_margin="5dp"
android:padding="5dp"
android:background="#2299dd"
android:textSize="20sp"
android:text="Main Activity Items"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rowForMainList"/>
</LinearLayout>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/jokeTypeListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_margin="5dp"
android:gravity="center_vertical"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#eeeeee"
android:textStyle="bold"
android:layout_margin="5dp"
android:padding="5dp"
android:background="#2299dd"
android:textSize="20sp"
android:text="Main Activity Items"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rowForMainList"/>
</LinearLayout>
</LinearLayout>
删除此行最好将这些行
JokeListData JokeListData=mList.get(位置)
在if(convertView==null)条件中,您需要覆盖自定义适配器中的
getCount()
。
它将返回ListView中的条目数。
替换
有点像
@Override
public int getCount() {
return mySourceArrayList.getCount();
}
@重写公共int getCount(){return mList.getCount();}删除此行将不会显示任何内容。适配器需要知道它需要显示多少数据。特别是当它基于列表显示时,我尝试了这一点,但没有效果。我添加了一个Log.e函数,似乎我的JokeData类中的数据没有显示。有什么想法吗?在您的onCreate()中有多个if条件,因此对于测试,请尝试删除所有条件并仅测试添加:ListOfJokeTypesFragment ListOfJokeTypesFragment=new ListOfJokeTypesFragment();getFragmentManager().beginTransaction().replace(R.id.container,listOfJokeTypesFragment).commit();它还应显示检查列表的texcolor和背景色是否相同尝试使列表的列表背景色和文本颜色不同。
beforr@Override
public int getCount() {
return super.getCount();
}
holder.mTextView.setText(jokeListData.getListName());
@Override
public int getCount() {
return super.getCount();
}
@Override
public int getCount() {
return mySourceArrayList.getCount();
}