为什么我的ListView没有显示什么? [英] Why is my ListView not showing anything?

查看:213
本文介绍了为什么我的ListView没有显示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的一个非常基本的ListView,并设置一个非常基本的适配器。我的问题是,在列表视图不显示任何东西,而不管在适配器和notifyDataSetChanged()的;

I have a very basic ListView in android and had set a very basic adapter. My problem is that the list view does not show anything, regardless of the adapter and the notifyDataSetChanged();

下面是我的code: XML:

Here is my code: XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView android:text="@string/app_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  ></TextView>
  <ListView android:id="@+id/selectView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    </ListView>
</RelativeLayout>

该活动code:

The Activity code:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.androidcourse.phonemapper.R;
import com.androidcourse.phonemapper.model.SelectViewAdapter;

public class SelectActivity extends Activity {

    private ListView mListView;

    private SelectViewAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.select_activity);
        initializeListView();

    }

    private void initializeListView() {
        mListView = (ListView) findViewById(R.id.selectView);
        mAdapter = new SelectViewAdapter(this);
        mListView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}

和适配器code:

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SelectViewAdapter extends BaseAdapter {

    private Context mContext;
    private TextView mMockTextView;

    public SelectViewAdapter(Context cnt) {
        mContext = cnt;
        mMockTextView = new TextView(mContext);
        mMockTextView.setText("Test text");
        mMockTextView.setBackgroundColor(Color.CYAN);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int position) {
        return mMockTextView;
    }

    @Override
    public long getItemId(int position) {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return mMockTextView;
    }

}

的问题是,没有显示在屏幕上。黑屏(从XML的第一个文本视图)是我得到的。我看不到mockTextView和它的文本。显然,我做的东西很错的,但我不能想出什么。

The problem is that nothing is shown on the screen. A black screen (and the first text view from the XML) is all I get. I cannot see the mockTextView and its text. Apparently I am doing something quite wrong, but I cant figure out what.

推荐答案

几件事我能想到的。

首先,你的RelativeLayout的没有相对定位信息。我会假设你的意思是把这个与取向的LinearLayout设置为垂直从你的描述。我的猜测是,该名单实际上没有被吸引,因为它甚至没有锚定到当前RelativeLayout的任何东西。如果您坚持使用RelativeLayout的,一定要放在一个id在APP_NAME TextView的和定位的ListView下,它通过layout_below。

First, Your RelativeLayout has no relative positioning information. I would assume you meant to put this in a LinearLayout with orientation set to vertical from what you describe. My guess is that the list is not actually being drawn since it isn't even anchored to anything in the current RelativeLayout. If you stick with the RelativeLayout, make sure to put an id on the app_name TextView and position the ListView under it via layout_below.

的LinearLayout解决方案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView android:text="@string/app_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  ></TextView>
  <ListView android:id="@+id/selectView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    </ListView>
</LinearLayout>

RelativeLayout的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView
  android:id="@+id/app_name_text" 
  android:text="@string/app_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  ></TextView>
  <ListView android:id="@+id/selectView"
    android:layout_below="@id/app_name_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    </ListView>
</RelativeLayout>

接下来,您getView()返回相同的TextView的所有3个指标。这不是一个问题,但是有三个列表大小显示了同样的观点在多个指标,我把赌注押在屏幕上可以显示所有三个在同一时间。而且,由于一个视图不能在多个位置上的时间,我居然会想到这会失败,所以我怀疑它甚至让这个code呢。尝试创建一个新的TextView的每个getView()。此外,您的MockTextView没有它自己的布局PARAMS。所以铺设出来在一个ListView电池可能不会发生任何。所以,你可以给它PARAMS型AbsListView.LayoutParams(WRAP_CONTENT,WR​​AP_CONTENT)的。再次,虽然我希望这错误,如果它到了原来的code。

Next, your getView() returns the same textView for all 3 indexes. It's not a problem to display the same view over multiple indexes however with a list size of three, I am betting that the screen can display all three at the same time. And since a View can't be in more than one position at a time, I actually would expect this to fail so I doubt it is even getting to this code yet. Try creating a new TextView for each getView(). Also your MockTextView doesn't have layout params of it's own. So laying it out within a listView cell might not be happening either. So you can give it params of type AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT). Again though I would expect this to error if it got to the original code.

getView()整理:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       if(convertView == null) {
          TextView textView = new TextView(parent.getContext());
          textView.setText("Position is:"+position);
          textView.setBackgroundColor(Color.CYAN);
          textView.setLayoutParams(new AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
          convertView = textView;
        }
        return mMockTextView;
    }

和最后的名单WRAP_CONTENT高度有时会出现问题。我不知道所有的场景。如果你最终更改为的LinearLayout试试你的列表视图layout_height设置为0,然后设置layout_weight = 1。这迫使线性布局膨胀成更多的空间。

And lastly the wrap_content height of your list can sometimes be problematic. I am not aware of all the scenarios. If you end up changing to a LinearLayout try setting your layout_height of the list view to 0 and then set the layout_weight=1. This forces the linear layout to inflate it into more space.

的LinearLayout重量解决方法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView android:text="@string/app_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  ></TextView>
  <ListView android:id="@+id/selectView"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:layout_width="wrap_content">
    </ListView>
</LinearLayout>

这篇关于为什么我的ListView没有显示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆