Android - 没有内容显示arrayadapter没有显示错误

时间:2016-10-22 10:40:09

标签: java android android-arrayadapter

Numbers活动中没有显示任何内容。过去两天我一直在努力解决这个问题。我搜索了很多,但没有找到任何东西。只有我在问。

这是我的代码。 的 NumbersActivity.java

package com.example.android.miwok;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;

public class NumbersActivity extends AppCompatActivity 

{

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

    // Create a list of words
    ArrayList<Word> words = new ArrayList<Word>();

    words.add(new Word("one","lutti"));
    words.add(new Word("two","otikko"));
    words.add(new Word("three","tolokooosu"));
    words.add(new Word("four","oyyisa"));
    words.add(new Word("five","massokka"));
    words.add(new Word("six","temmokka"));
    words.add(new Word("seven","kenakaku"));
    words.add(new Word("eight","kawinta"));
    words.add(new Word("nine","woe"));
    words.add(new Word("ten","naaaache"));


    WordAdapter adapter = new WordAdapter(this,R.layout.list_item,words);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);

}}

activity_numbers.xml

<?xml version="1.0" encoding="utf-8"?>


<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.miwok.NumbersActivity"/>

WordAdapter.java

package com.example.android.miwok;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.util.ArrayList;



public class WordAdapter extends ArrayAdapter<Word> {
    public WordAdapter(Context context, int resource) {
        super(context, resource);
    }

    public WordAdapter(NumbersActivity context, int resource, ArrayList<Word> words) {
        super(context, resource, words);
    }




    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }
        return listItemView;
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/miwok_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="lutti"/>

    <TextView
        android:id="@+id/default_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="one"/>
</LinearLayout>

Word.java

package com.example.android.miwok;


public class Word {
    private String mDefaultTranslation;
    private String mMiwokTranslation;

    public Word(String mDefaultTranslation, String mMiwokTranslation){
        this.mDefaultTranslation = mDefaultTranslation;
        this.mMiwokTranslation = mMiwokTranslation;
    }

    public String getDefaultTranslation(){
        return mDefaultTranslation;
    }

    public String getmMiwokTranslation(){
        return mMiwokTranslation;
    }
}

我的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:0)

您没有在 getView 方法中设置数据。首先,您需要在适配器中创建自定义列表的变量,然后在构造函数中初始化它,您正在传递数据但不在adatper中设置任何位置。然后在getView方法中设置该数据。 您还需要创建一个 viewholder 类,以便在适配器中创建textview的引用