0

现在我的 ListView 出现问题,当我加载列表活动时,第一行显示,但其他行为空白,当我向下滚动并返回更多行时弹出。我的列表视图正在获取正确的数据而且我的删除方法仍然有效,但唯一的问题是在我向下滚动和向上滚动之前没有行,并且只有 2-3 行显示,除非我使 textSize 更大,然后会显示更多行。

管理类

package com.fullfrontalgames.numberfighter;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

public class Admin extends ListActivity {
    private DBAdapter db;

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

        DBAdapter db = new DBAdapter(this);
        db.open();
        getListView().setAdapter(new PlayerAdapter(this, db.getAllPlayers()));
    }

    private class PlayerAdapter extends BaseAdapter implements OnClickListener, ListAdapter {
        private Cursor mPlayers;

        private Context mContext;

        public PlayerAdapter(Context context, Cursor players) {
            mContext = context;
            mPlayers = players;
        }

        @Override
        public int getCount() {
            return mPlayers.getCount();
        }

        @Override
        public PlayerInfo getItem(int position) {
            if (mPlayers.moveToPosition(position)) {
                String name = mPlayers.getString(mPlayers.getColumnIndex("USERNAME"));
                String password = mPlayers.getString(mPlayers.getColumnIndex("PASSWORD"));
                String email = mPlayers.getString(mPlayers.getColumnIndex("EMAIL"));

                return new PlayerInfo(name, password, email);
            }

            return null;
        }

        @Override
        public long getItemId(int position) {
            mPlayers.moveToPosition(position);
            return mPlayers.getLong(mPlayers.getColumnIndex("_id"));
        }

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

            PlayerInfo playerInfo = getItem(position);
            TextView nameTextView = ViewHolder.get(convertView, R.id.AdminUsername);
            TextView passwordTextView = ViewHolder.get(convertView, R.id.AdminPassword);
            TextView emailTextView = ViewHolder.get(convertView, R.id.AdminEmail);
            Button deleteButton = ViewHolder.get(convertView, R.id.PlayerDelete, position);
            deleteButton.setOnClickListener(this);

            nameTextView.setText(playerInfo.getName());
            passwordTextView.setText(playerInfo.getPassword());
            emailTextView.setText(playerInfo.getEmail());
            return convertView;
        }

        @Override
        public void onClick(View v) {

            db.open();

            int position = (Integer)v.getTag();
            mPlayers.moveToPosition(position);
            long id = mPlayers.getLong(mPlayers.getColumnIndex("_id"));
            db.deleteFriend();
            mPlayers = db.getAllPlayers();
            notifyDataSetChanged();

            db.close();

        }
    }

}

管理员 xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black" >


    <ListView 
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center" >

    </ListView>





</LinearLayout>

管理列表项 xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:padding="5dp" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/AdminUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />

    <TextView 
        android:id="@+id/AdminPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />

    <TextView 
        android:id="@+id/AdminEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />


</LinearLayout>

<Button
        android:id="@+id/PlayerDelete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Delete" />

</LinearLayout>

ViewHolder 类

    package com.fullfrontalgames.numberfighter;

import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

@SuppressWarnings("unchecked")
public class ViewHolder {

    private static final String TAG = "View Holder";

    public static <T extends View> T get(View view, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>)view.getTag();
        if (viewHolder == null) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T)childView;
    }

    public static <T extends View> T get(View view, int id, int position) {
        SparseArray<View> viewHolder = (SparseArray<View>)view.getTag();
        if (viewHolder == null) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);

        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        childView.setTag(position);
        return (T)childView;
    }

    public TextView text1;

    public Button button;
}

PlayerInfo 类

package com.fullfrontalgames.numberfighter;

public class PlayerInfo {
    private String mName;

    private String mPassword;

    private String mEmail;

    public PlayerInfo(String name, String password, String email) {
        mName = name;
        mPassword = password;
        mEmail = email;
    }

    public String getName() {
        return mName;
    }

    public String getPassword() {
        return mPassword;
    }

    public String getEmail() {
        return mEmail;
    }

}

日志猫

    04-21 02:23:21.563: E/AndroidRuntime(23401): FATAL EXCEPTION: main
04-21 02:23:21.563: E/AndroidRuntime(23401): java.lang.NullPointerException
04-21 02:23:21.563: E/AndroidRuntime(23401):    at com.fullfrontalgames.numberfighter.ViewHolder.get(ViewHolder.java:40)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at com.fullfrontalgames.numberfighter.Admin$PlayerAdapter.getView(Admin.java:75)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.AbsListView.obtainView(AbsListView.java:2402)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.ListView.makeAndAddView(ListView.java:1769)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.ListView.fillDown(ListView.java:672)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.ListView.fillFromTop(ListView.java:733)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.ListView.layoutChildren(ListView.java:1622)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.AbsListView.onLayout(AbsListView.java:2237)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.View.layout(View.java:13840)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewGroup.layout(ViewGroup.java:4372)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.View.layout(View.java:13840)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewGroup.layout(ViewGroup.java:4372)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.View.layout(View.java:13840)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewGroup.layout(ViewGroup.java:4372)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.View.layout(View.java:13840)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewGroup.layout(ViewGroup.java:4372)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.View.layout(View.java:13840)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewGroup.layout(ViewGroup.java:4372)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1948)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1758)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1042)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4329)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.Choreographer.doFrame(Choreographer.java:525)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.os.Handler.handleCallback(Handler.java:615)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.os.Looper.loop(Looper.java:137)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at android.app.ActivityThread.main(ActivityThread.java:5059)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at java.lang.reflect.Method.invokeNative(Native Method)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at java.lang.reflect.Method.invoke(Method.java:511)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-21 02:23:21.563: E/AndroidRuntime(23401):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

类 ViewHolder

import android.util.SparseArray;
import android.view.View;

@SuppressWarnings("unchecked")
public class ViewHolder
{

private static final String TAG = "View Holder";

public static <T extends View> T get(View view, int id) 
{
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
    if (viewHolder == null) 
    {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);
    }
    View childView = viewHolder.get(id);
    if (childView == null) 
    {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }
    return (T) childView;
}

public static <T extends View> T get(View view, int id, int position) 
{
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
    if (viewHolder == null) 
    {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);
    }
    View childView = viewHolder.get(id);

    if (childView == null) 
    {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }
    childView.setTag(position);
    return (T) childView;
}
}

班级管理员

public class Admin extends ListActivity
{
    private DBAdapter mDbAdapter;

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

        mDbAdapter =  = new DBAdapter(this);
        mDbAdapter.open();

        getListView().setAdapter(new PlayerAdapter(this, mDbAdapter..getAllPlayers()));
    }

    private class PlayerAdapter extends BaseAdapter implements OnClickListener
    {
        private Cursor mPlayers;
        private Context mContext;

        public PlayerAdapter(Context context, Cursor players)
        {
            mContext = context; 
            mPlayers = players;
        }

        @Override
        public int getCount()
        {
            return mPlayers.getCount();
        }

        @Override
        public PlayerInfo getItem(int position)
        {
            if (mPlayers.moveToPosition(position))
            {
                String name = mPlayers.getString(mPlayers.getColumnIndex("USERNAME"));
                String password = mPlayers.getString(mPlayers.getColumnIndex("PASSWORD"));
                String email = mPlayers.getString(mPlayers.getColumnIndex("EMAIL");

                return new PlayerInfo(name, password, email);
            }

            return null;
        }

        @Override
        public long getItemId(int position)
        {
            mPlayers.moveToPosition(position);
            return mPlayers.getLong(mPlayers.getColumnIndex("_id");
        }

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

            PlayerInfo playerInfo = getItem(position);
            TextView nameTextView = ViewHolder.get(convertView, R.id.AdminUsername);
            TextView passwordTextView = ViewHolder.get(convertView, R.id.AdminPassword);
            TextView emailTextView = ViewHolder.get(convertView, R.id.AdminEmail);
            Button deleteButton = ViewHolder.get(convertView, R.id.PlayerDelete, position);
            deleteButton.setOnClickListener(this);

            nameTextView.setText(playerInfo.getName());
            passwordTextView.setText(playerInfo.getPassword());
            emailTextView.setText(playerInfo.getEmail());
            return convertView;
        }

        @Override
        public void onClick(View v)
        {
             int position = (Integer) v.getTag();
                mPlayers.moveToPosition(position);
                long id = mPlayers.getLong(mPlayers.getColumnIndex("_id"));
                mDbAdapter.deletePlayer(id);
                mPlayers = mDbAdapter.getAllPlayers();
                notifyDataSetChanged();
                break;
        }
    }

}

    public class PlayerInfo
    {
        private String mName;
        private String mPassword;
        private String mEmail;

        public PlayerInfo(String name, String password, String email)
        {
            mName = name;
            mPassword = password;
            mEmail = email;
        }

        public String getName()
        {
            return mName;
        }

        public String getPassword()
        {
            return mPassword;
        }

        public String getEmail()
        {
            return mEmail;
        }

    }
}  

list_items xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:padding="5dp" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/AdminUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />

    <TextView 
        android:id="@+id/AdminPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />

    <TextView 
        android:id="@+id/AdminEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />


</LinearLayout>

<Button
        android:id="@+id/PlayerDelete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Delete" />

</LinearLayout>
于 2013-04-21T05:34:26.043 回答