Android ListView无法显示内容

时间:2015-03-15 20:03:54

标签: java android listview android-listview

我正在关注Android开发的this教程:

我在本教程的ListView部分。到目前为止,我的应用程序有一个文本字段,用户输入一个名称。然后按下按钮,它们的名称应保存并显示在屏幕下半部分的ListView中。我使用与教程完全相同的代码,但我的问题是ListView在按下按钮后没有显示任何内容。以下是我的一些java和xml代码:

// MainActivity.java

    package com.theboss.jackli.omgandroid;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity implements View
        .OnClickListener
{
    TextView mainTextView;
    Button mainButton;
    EditText mainEditText;
    ListView mainListView;
    ArrayAdapter mArrayAdapter;
    ArrayList mNameList = new ArrayList();

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

        // 1. Access the TextView defined in layout XML
        // and then set its text
        mainTextView = (TextView) findViewById(R.id.main_textview);
        mainTextView.setText("Set in Java!");

        // 2. Access the Button defined in layout XML
        // and listen to it here
        mainButton = (Button) findViewById(R.id.main_button);
        mainButton.setOnClickListener((android.view.View.OnClickListener) this);

        // 3. Access the EditText defined in layout XML
        mainEditText = (EditText) findViewById(R.id.main_edittext);

        // todo ListView content not showing.
        // 4. Access the ListView
        mainListView = (ListView) findViewById(R.id.main_listview);

        // Create an ArrayAdapter for the ListView
        mArrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                mNameList);

        // Set the ListView to use the ArrayAdapter
        mainListView.setAdapter(mArrayAdapter);

        // Also add that value to the list shown in the ListView
        mNameList.add(mainEditText.getText().toString());
        mArrayAdapter.notifyDataSetChanged();

        // Test output to logs
        for (int i = 0; i < mNameList.size(); i++)
        {
            String contents = mNameList.get(i).toString();
            Log.d("OMG Android mNameList: ", contents);
        }
    }

    @Override
    public void onClick(View v)
    {
        // Take what was typed into the EditText and use in TextView
        mainTextView.setText(mainEditText.getText().toString()
                + " is yoloing on Android development!");
    }

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

这是xml代码:

<!-- activity_main.xml -->

   <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"
              tools:context=".MainActivity">

    <TextView
        android:id="@+id/main_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/textview"/>

    <!-- This nested layout contains views of its own -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!-- Set OnClickListener to trigger results when pressed -->
        <Button
            android:id="@+id/main_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:text="@string/button" />
        <!-- Shows an image from your drawable resources -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:src="@drawable/ic_launcher" />
        <!-- Closing tag for the horizontal nested layout -->
    </LinearLayout>

    <!-- Displays keyboard when touched -->
    <EditText
        android:id="@+id/main_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:hint="@string/hint" />

    <!-- List whose dataset is defined in code with an adapter -->
    <ListView
        android:id="@+id/main_listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="20dp" />
</LinearLayout>

我用Google搜索并尝试了大约两个小时的各种事情,但找不到问题。我选择的最低支持版本是4.0.3。我尝试从ListView中删除“layout_weight”并给出20dp的“layout_height”只是为了查看它是否是渲染问题但是仍然没有出现。无论我是否使用实际的layout_height或layout_weight,都会突出显示一个栏,如果单击它,它就会显示在ListView的位置。问题是那里没有显示任何内容。似乎是文本字段值被保存或传输到ListView的一些问题。任何有关这方面的见解都将受到高度赞赏!

1 个答案:

答案 0 :(得分:2)

您的mNameList最初为空。在onClick中,您应该将新项目添加到ArrayAdapter的数据集中。改变你的onClick,如:

    @Override
    public void onClick(View v)
    {
        // Take what was typed into the EditText and use in TextView
        mainTextView.setText(mainEditText.getText().toString()
                + " is yoloing on Android development!");
        mArrayAdapter.add(mainEditText.getText().toString());
    }

填写EditText,然后点击按钮