Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

android - Why is my ListView not showing anything?

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();

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>

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

And the Adapter 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;
    }

}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A few things I can think of.

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 Solution

<?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 Solution:

<?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>

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() tidy up:

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

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 Weight Solution:

<?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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...