简体   繁体   English

android:为什么没有元素出现在我的列表视图中?

[英]android: why are no elements appearing in my listview?

I'm trying to use a ListView as the main navigation feature in a magazine-reader app. 我正在尝试使用ListView作为杂志阅读器应用程序中的主要导航功能。 I want each View in the list to display the headline of a story below an image associated with it. 我希望列表中的每个视图在与其关联的图像下方显示一个故事的标题。 I have created an XML layout with a vertically-oriented LinearLayout with an ImageView on top and TextView on the bottom, and am trying to create a custom adapter that inflates this layout in the getView method and puts the image/text resources into it. 我创建了一个具有垂直方向的LinearLayout,顶部是ImageView,底部是TextView的XML布局,并且正在尝试创建一个自定义适配器,该适配器可以在getView方法中将此布局夸大并将图像/文本资源放入其中。 Here is my code: 这是我的代码:

package com.example.caliber;
import java.util.List;
import com.example.caliber.R;

public class MainActivity extends Activity {
Spinner issues, sections;
Button blog;
ListView mainGal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] issueList = {"issues", "fall '12", "spring '12", "fall '11", "spring '12", 
                            "fall '10", "spring '10"};
    String[] sectionList = {"sections", "campus", "entertainment", "lifestyle", "love&sex", "tech&web", "Current" };

    ArrayAdapter<String> aaIssues = new ArrayAdapter<String>(this, R.layout.spin_item, issueList);
    ArrayAdapter<String> aaSections = new ArrayAdapter<String>(this,   
R.layout.spin_item, sectionList);

    blog = (Button)findViewById(R.id.blogBtn);

    issues = (Spinner)findViewById(R.id.issueSpin);
    sections = (Spinner)findViewById(R.id.sectionSpin);             
    issues.setAdapter(aaIssues);
    sections.setAdapter(aaSections);


    OnClickListener mbListener = new OnClickListener() {
        @Override
            public void onClick(View v){
        issues.setAlpha((float) 0.5);
        sections.setAlpha((float) 0.5);
        blog.setAlpha((float) 0.5);
        }
     };


    ImageButton mainbtn = (ImageButton)findViewById(R.id.cbutton);      
    mainbtn.setOnClickListener(mbListener);

    mainGal = (ListView)findViewById(R.id.mainGal);
    GalAdapter ga = new GalAdapter();
    mainGal.setAdapter(ga);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

class GalAdapter extends ArrayAdapter<String> {
    Activity context;
    String[] headlines = {"Alpha bravo charlie delta", "Echo foxtrot golf 
hotel", "indigo juliette kilo lima"};
    int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};

    public GalAdapter() {
        super (MainActivity.this, R.layout.galleryview);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater li = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
        View story = li.inflate(R.layout.galleryview, parent);
        ImageView image = (ImageView)story.findViewById(R.id.image);
        TextView headline = (TextView)story.findViewById(R.id.headline);

        image.setImageResource(images[position]);
        headline.setText(headlines[position]);
        return story;       
}   

The app launches fine and I can see the listview's background, but there are no items on the list. 该应用程序可以正常启动,并且可以看到列表视图的背景,但是列表上没有任何项目。 Why is this? 为什么是这样? As is probably clear, I'm very new to android development, but have a good understanding of the fundamentals of Java. 很明显,我对android开发非常陌生,但是对Java的基础知识有很好的了解。 I'm aware this is due to a fundamental misunderstanding I have here, perhaps an error in one of the constructors (I'm still a little fuzzy on what some of the parameters are). 我知道这是由于我在这里有一个基本的误解,也许是一个构造函数中的一个错误(我对某些参数的含义还是有些模糊)。 Anyway, I'll be very appreciative if anyone can help me understand these issues more deeply. 无论如何,如果有人可以帮助我更深入地理解这些问题,我将非常感激。

Thanks in advance 提前致谢

ps is it poor form to categorize this under both android and java? PS在Android和Java下将其归类为可怜的形式吗? should I just use android in the future? 我将来应该只使用android吗?

---EDIT: - -编辑:

    class GalAdapter extends ArrayAdapter<String> {
    Activity context;
    String[] hlines;
    int[] imgs;

    public GalAdapter(String[] hlines, int[] imgs) {
        super (MainActivity.this, R.layout.galleryview , hlines);
        this.hlines = hlines;
        this.imgs = imgs;
    }       
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        if (convertView == null){
        LayoutInflater li = (LayoutInflater)context. getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.galleryview, parent);
        ImageView image = (ImageView)convertView.findViewById(R.id.image);
        TextView headline = (TextView)convertView.findViewById(R.id.headline);

        image.setImageResource(imgs[position]);
        headline.setText(hlines[position]);}
        return convertView;                                 
    }
    }

Now I'm getting a null pointer exception on the line where I call the LayoutInflator. 现在,我在调用LayoutInflator的那一行上得到了一个空指针异常。 What pointer is it referring to? 它指的是什么指针? I'm assuming I'm not passing the arrays (String[] hlines + int[] imgs) into it properly... Here is where I make the call: 我假设我没有正确地将数组(String [] hlines + int [] imgs)传递给它……这是我打电话的地方:

    String[] headlines ={ "Echo foxtrot golf hotel", "Echo foxtrot golf hotel", "indigo   
juliette kilo lima"};
    int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
            ....
    mainGal = (ListView)findViewById(R.id.mainGal);
    GalAdapter ga = new GalAdapter(headlines, images);
    mainGal.setAdapter(ga);

在这里看看https://github.com/cplain/custom-list它将能够为您提供一些帮助,您需要在getView()使用convertView ,我建议不要将列表传递到适配器中一个里面

Your adapter is not created properly. 您的适配器创建不正确。 In order to use ArrayAdapter , you must either: 为了使用ArrayAdapter ,您必须:

  1. Supply the list of items to the super implementation 向超级实现提供项目列表
  2. Add the items directly to the adapter, or 将项目直接添加到适配器,或
  3. Override all of the methods necessary to categorize the data the adapter holds, such as getCount() , getItem() , etc. 覆盖对适配器保存的数据进行分类的所有必需方法,例如getCount()getItem()等。

The simplest fix is to change your constructor like so: 最简单的解决方法是像这样更改构造函数:

public GalAdapter() {
    super(MainActivity.this, R.layout.galleryview, headlines);
}

What this does is allows the adapter implementation to use one of your arrays to determine how many items are in the list, and what data it should return when getItem() is called. 这样做是允许适配器实现使用您的数组之一来确定列表中有多少项,以及在调用getItem()时应返回什么数据。 Since you are overriding getView() , the second parameter isn't even really necessary and you could probably get away with super(MainActivity.this, 0, headlines) as well. 由于您要覆盖getView() ,因此实际上甚至没有必要使用第二个参数,并且您也可以摆脱super(MainActivity.this, 0, headlines)

If your final implementation it to gather data from multiple arrays like this, you may ultimately be better of creating your custom adapter as a subclass of BaseAdapter directly, as you are already bypassing most of the functionality ArrayAdapter provides as convenience. 如果最终实现是从这样的多个数组中收集数据,那么最终最好BaseAdapter直接将自定义适配器作为BaseAdapter的子类创建,因为您已经绕过了ArrayAdapter提供的大多数功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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