【问题标题】:ListView from ArrayAdapter来自 ArrayAdapter 的 ListView
【发布时间】:2015-01-10 11:49:08
【问题描述】:

我用 ArrayAdapter 创建了一个列表视图,当我运行它时,它在运行时向我显示了一个空活动,我不知道错误:

这里是 MainActivity 和 ArrayAdapter 类

public class MainActivity extends Activity {
    String [] title ;
    ListView list;

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

        list = (ListView) findViewById(R.id.list);
        title = getResources().getStringArray(R.array.titles);
        list.setAdapter(new MyAdapter(MainActivity.this, title));

    }
}

class MyAdapter extends ArrayAdapter{
    String [] title;
    Context context;

    public MyAdapter(Context context, String[] title) {
        super(context,R.layout.single_row,R.id.img);
        this.title = title;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TextView tv;
        ImageView iv;
        View row = convertView;
        if(row==null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.single_row, parent, false);
        }
            tv = (TextView) row.findViewById(R.id.title);
            tv.setText(title[position]);
            iv = (ImageView) row.findViewById(R.id.img);
            iv.setImageResource(R.drawable.arrow);

        return row;
    }
}

包含 ListView 的activity_main.xml

  <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/list"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>

single_row.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">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/img"
        android:src="@drawable/arrow"
        android:layout_margin="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textSize="@dimen/abc_action_bar_stacked_max_height"
        android:id="@+id/title"
        android:layout_gravity="center_vertical" />
</LinearLayout>

</LinearLayout>

【问题讨论】:

  • 对不起,如果我没有添加任何照片,我没有足够的声誉
  • 实现 BaseAdapter 类对于自定义列表视图来说效率更高,尝试使用它作为 Moubeen 的回答显示
  • 我猜你的 MainActivity 的 xml 有问题。你能把它放在这里吗?
  • 我已经添加了 xml 文件

标签: android android-arrayadapter


【解决方案1】:

在您的适配器中更改这行代码。

LayoutInflater inflater = LayoutInflater.from(context);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多