简体   繁体   中英

Custom ArrayAdapter showing a blank screen in my Fragment

So I've just been messing around with android for a little bit and I've run into a bit of a snag. The fragment where I am instantiating ListOfJokesTypesAdapter for some reason is not displaying a listview populated with the Data from my JokeData class.

All that I get is a blank screen (no errors or anything of that nature).

This is just a proof of concept thing I've been working on so any help would be greatly appreciated. Why is it that this particular custom adapter is not working while my MainClassAdapter is working just fine.

The Code

My Joke Class:

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;
 }
}

My JokeListClass

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;
}
}

My Actual Joke Data

public class JokeData {



private static List<Joke> dogJokes = new ArrayList<Joke>(){

    {
        add(new Joke("Dogs", "Bark"));
        add(new Joke("Dogs", "Woof"));
        add(new Joke("Dogs", "Howl"));
        add(new Joke("Dogs", "Sniff"));
    }
};


private static List<Joke> catJokes = new ArrayList<Joke> (){
    {
        add(new Joke("Cats", "woof"));
        add(new Joke("Dogs", "Meow"));
    }
};

static List<JokeListData> dataOfJokeList = new ArrayList<JokeListData>();

public static void addEntries(){
    dataOfJokeList.add(new JokeListData("Cat Jokes", catJokes));
    dataOfJokeList.add(new JokeListData("Dog Jokes", dogJokes));
 }

}

The Adapter

public class ListOfJokeTypesAdapter extends ArrayAdapter<JokeListData> {

Context mContext;
int mLayoutId;
List<JokeListData> mList;

public ListOfJokeTypesAdapter(Context context, int resource, List<JokeListData> objects) {
    super(context, resource, objects);
    this.mContext = context;
    this.mLayoutId = resource;
    this.mList = objects;
}


@Override
public int getCount() {
    return super.getCount();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    if(convertView == null){
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mLayoutId,parent,false);

        holder = new ViewHolder();

        holder.mTextView = (TextView) convertView.findViewById(R.id.rowForMainList);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    JokeListData jokeListData = mList.get(position);

    holder.mTextView.setText(jokeListData.getListName());

    return convertView;
}

private static class ViewHolder{
    TextView mTextView;
 }
}

The Fragment which utilizes the adapter

public class ListOfJokeTypesFragment extends Fragment {

ListView mListView;
ListOfJokeTypesAdapter listOfJokeTypesAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.joke_type_fragment,container,false);
    JokeData.addEntries();
    mListView = (ListView)view.findViewById(R.id.jokeTypeListView);
    listOfJokeTypesAdapter = new ListOfJokeTypesAdapter(getActivity().getApplicationContext(),R.layout.row,JokeData.dataOfJokeList);
    mListView.setAdapter(listOfJokeTypesAdapter);

    return view;
 }
}

The Fragment Manager package com.example.taranveer.jokeapplicationactual;

import android.app.Activity; import android.os.Bundle;

/** * Created by Taranveer on 2014-07-22. */ public class TheFragmentActivityManager extends Activity {

@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();
            }
        }
    }

}
}

The relevant XML:

joke_type_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>

row.xml

<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>
beforr@Override
public int getCount() {
    return super.getCount();
}

remove this line

better to put these lines JokeListData jokeListData = mList.get(position);

holder.mTextView.setText(jokeListData.getListName()); 

inside if(convertView == null) condition

You need to override getCount() in your custom adapter. It will return the number of entries in your ListView. Replace

@Override
public int getCount() {
    return super.getCount();
}

With something like

@Override
public int getCount() {
    return mySourceArrayList.getCount();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM