8

我想我快疯了。将自定义适配器绑定到 Listview 如此简单的事情让我很头疼。

贴出代码然后解释一下:

MainActivity.java

package com.example.pruebalist;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private static String[] data = new String[] {"0","1","2","3"};

    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.v("MainActivity","Inside MainActivity");
        setContentView(R.layout.main);
        ListView lstView = (ListView)findViewById(R.id.listNoticias);

        ArrayAdapter<String> adapter = new LstAdapter(this, R.layout.row, data);
        lstView.setAdapter(adapter);
    }

}

LstAdapter.java

package com.example.pruebalist;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LstAdapter extends ArrayAdapter<String>{

    private String[] mData;
    private Context mContext;
    int layoutResourceId;

    public LstAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId, values);
        mContext = context;
        mData = values;
        layoutResourceId = textViewResourceId;

        Log.v("LstAdapter","Inside LstAdapter");
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        Log.v("LstAdapter","Inside getView");

        if(v==null){
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            v = inflater.inflate(layoutResourceId, parent,false);
        }

        String item = mData[position];
        if(item!=null){
            TextView txtItem = (TextView)v.findViewById(R.id.texto);
            if(txtItem!=null){
                txtItem.setText(item);
            }
        }

        return v;
    }

}

ListView 永远不会显示。并且从未使用过 getView,logCat 不显示“在 Getview 内部”。

怎么了?

4

4 回答 4

28

主要问题是

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

将其更改为

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.length;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return mData[arg0];
    }

查看这个关于列表视图和列表活动的 Vogella 教程,尝试ViewHolder在列表适配器中使用它会提高您的视图性能。

于 2012-08-20T13:35:01.920 回答
10

这是因为您保留了返回 0 的方法的默认实现getCount(),因此适配器认为没有要显示的元素ListView

它应该返回mData.length

@Override
public int getCount() {
   return mData.length;
}
于 2012-08-20T13:34:57.237 回答
3

你还没有实现getCount()getItemId(...)

You can just take out those methods and use the defaults if you do not plan on needing custom implementation for them.

Take them out for now and then try.

于 2012-08-20T13:38:31.437 回答
2

The right way of doing this is to call super(context, textViewResourceId, values) - which you are already doing. In that case you can remove the getCount() and getItemId(...) functions completely. The class which your List is derrived from will automatically return the correct values.

  • Cheers
于 2014-02-13T18:42:29.700 回答