ListView与自定义适配器[Android]

时间:2014-12-25 17:03:27

标签: android android-listview android-arrayadapter

我正在尝试使用自定义布局构建ListView,但到目前为止还无法成功完成。我是初学者,并且一直在按照教程来完成它。 这就是我正在做的事情:

    //this is where I want the custom list items to be displayed.
    //GroupAdapter is the constructor of the class in which I'm trying to make a custom adapter
    stringArray = new String[10];
    groupItemArrayAdapter = new GroupAdapter(this,stringArray);
    groupListView = (ListView) findViewById(R.id.mainList);
    groupListView.setAdapter(groupItemArrayAdapter);

    //this is the code in GroupAdapter.java class
    public class GroupAdapter extends ArrayAdapter{
    private LayoutInflater inflater;

    public GroupAdapter(Activity activity, String[] items){
        super(activity,R.layout.group_view);
        inflater=activity.getWindow().getLayoutInflater();
     }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        return inflater.inflate(R.layout.group_view,parent,false);
     }
    }

当我启动应用程序时,会出现一个空白屏幕。最初,当我使用Android默认布局列表时,显示列表项但不再显示...我不明白我做错了什么。有人可以帮忙吗?

这是group_view.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="horizontal"
   android:padding="30dp" >

<TextView
    android:id="@+id/gName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Group Name"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_marginRight="100dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/activity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/signIn"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:text="5"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textStyle="bold" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以在下一步

替换所有代码
stringArray = new String[10];
groupItemArrayAdapter = new GroupAdapter(this,stringArray);
groupListView = (ListView) findViewById(R.id.mainList);
groupListView.setAdapter(new ArrayAdapter<String>(this, R.layout.group_view, R.id.gName, stringArray));

如果你想在gName字段显示字符串,如果你想在activity拍摄,那么你需要使用

groupListView.setAdapter(new ArrayAdapter<String>(this, R.layout.group_view, R.id.activity, stringArray));

回答“为什么它是空白的?”

@Override
public View getView(int position, View convertView, ViewGroup parent){
     return inflater.inflate(R.layout.group_view,parent,false);
}

您只会夸大空视图,并且不会将字符串分配给字段。这将是(最简单的版本)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.group_view, parent, false);
    TextView textView = (TextView)convertView.findViewById(R.id.gName);
    String item = getItem(position)
    textView.setText(item);
    return convertView;
}

阅读文章Making ListView Scrolling Smooth

后,您将能够实施更好的版本