java 列表视图适配器不起作用,未调用 getView。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12038660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:22:35  来源:igfitidea点击:

List View adapter not working, getView not called.

javaandroidandroid-listviewandroid-arrayadapter

提问by Rafael Osuna Dominguez

I think i'm turning crazy. Something so simple has bind a custom adapter to a Listview is giving me a headache.

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

Post the code and explain then:

贴出代码然后解释一下:

MainActivity.java

主活动.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

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

}

The ListView is never show. And getView is never used, logCat doesn't show "Inside Getview".

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

What's wrong?

怎么了?

回答by rajpara

Main problem is

主要问题是

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

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

change it to

将其更改为

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

Check out this Vogella Tutorial on List View & List Activity, Try to use ViewHolderin List Adapter it will increase your view performance.

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

回答by Andy Res

This is because you left the default implementation of getCount()method which returns 0, so the Adapter thinks there are no elements to display in ListView.

这是因为您保留了getCount()返回 0的方法的默认实现,因此适配器认为ListView.

It should return mData.length

它应该返回 mData.length

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

回答by prolink007

You have not implemented getCount()and getItemId(...).

您尚未实施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.

暂时将它们取出,然后尝试。

回答by Tom

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.

这样做的正确方法是调用super(context, textViewResourceId, values)- 您已经在这样做了。在这种情况下,您可以完全删除getCount()getItemId(...)功能。您的 List 派生自的类将自动返回正确的值。

  • Cheers
  • 干杯