ArrayAdapter的getView()未被调用

时间:2014-10-26 02:22:14

标签: android listview android-arrayadapter

我不知道为什么。 getCount()返回正确的列表大小。即使我在getCount()中打印一些项目,一切似乎都是正确的。然而,getView()没有被调用。

包含列表片段的活动:

public class MainActivity extends FragmentActivity {

static final int GET_COMMAND_REQUEST = 1;  // The request code
private ImageButton mMic;
private Interpreter interpreter;
private CommunicationList mList;

/**
 * Restores the list or makes new list
 * adds listeners
 * initializes receiver
 * @param [Bundle] savedInstanceState to restore the state if necessary.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMic = (ImageButton) findViewById(R.id.mic);//get the button
    addListenerOnMic();
    mList = new CommunicationList();
    if (savedInstanceState == null)
    {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.listfragment, mList)
                .commit();
    }
    interpreter = new Interpreter(this,mList);

}

ListFragment:

public class CommunicationList extends ListFragment {

private ListAdapter mListAdapter;
private ListView mListView;
private ArrayList<ListItem> mList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    System.out.println("-------------------\ncreateView\n----------");
    View v = inflater.inflate(R.layout.list_fragment, container, false);

    if(savedInstanceState == null || !savedInstanceState.containsKey("list"))
        mList = new ArrayList<ListItem>();
    else
        mList = savedInstanceState.getParcelableArrayList("list");
    /** Defining the ArrayAdapter to set items to ListView */
    mListView = (ListView) v.findViewById(android.R.id.list);
    mListAdapter = new ListAdapter(getActivity(),mList);
    /** Setting the adapter to the ListView */

    return v;
}
/**
 * Add an element to the list and show it
 * @param [ListItem] item is the item we want to add.
 */
public void add(ListItem item)
{
    mList.add(item);
    updateList();
}

/**
 * Update the list
 * created so that ProxAlert can update the distances
 */
public void updateList(){
    mListAdapter.notifyDataSetChanged();
    mListView.setSelection(mListAdapter.getCount() - 1);
}
/**
 * Save the state of the list
 * @param savedInstanceState
 */
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putParcelableArrayList("list",mList);
    super.onSaveInstanceState(savedInstanceState);
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.

}

}

ArrayAdapter:

package com.example.breght.askme;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * @author Breght Van Baelen
 * @since 10/8/2014
 * @version 1.0 creation of class
 * @version 2.0 distance added
 *
 * class for custom list with to-do messages
 */
public class ListAdapter extends ArrayAdapter{
    private final Activity mContext;
    private ArrayList<ListItem> mList;

    /**
     * Constructor
     * @param [Activity] context is used to get the layoutInflater.
     * @param [ArrayList<item>] list is used to retrieve information from the items
     */
    public ListAdapter(Activity context,ArrayList<ListItem> list) {
        super(context,R.layout.message_client, list);
        mList = list;
        mContext = context;
    }

    /**
     * Vul de informatie van een item in in de lijst.
     * @param position is de positie van het item in de lijst
     * @param view
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View view, ViewGroup parent) {

        int layoutID = mList.get(position).getLayoutID();
        String messageText = mList.get(position).getMessage();

        LayoutInflater inflater = mContext.getLayoutInflater();
        View rowView = inflater.inflate(layoutID, null, true);

        TextView message = (TextView) rowView.findViewById(R.id.itemMessage);
        message.setText(messageText);
        return rowView;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

}

XML: activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/listfragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/menu"
    android:layout_alignParentTop="true"
    />
<LinearLayout
    android:id="@+id/menu"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:gravity="center"
    android:background="@color/menu">
    <ImageButton
        android:layout_width="80dp"
        android:layout_height="75dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/mic"
        android:background="@drawable/mic_button"/>
</LinearLayout>

XML:list_fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="#00000000"
/>

修改 找到解决方案,我忘了把setListAdapter(mListAdapter)。愚蠢的错误,我的坏。

1 个答案:

答案 0 :(得分:1)

更改此  mListView = (ListView) v.findViewById(android.R.id.list);

mListView = (ListView) v.findViewById(R.id.list);

R 应该是您的应用 R ,而不是Android R

<强>的ListView:

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="#00000000"
/>