java - Android Studio itemsAdapter 和自定义 ListView 不起作用

标签 java android xml listview android-listview

我在 Android Studio 中尝试实现自定义 ListView。我创建了一个名为“custom_layout_rachel.xml”的 xml 文件,并将其放入我的“layout”文件夹中。该文件包含我希望 ListView 的外观的代码。

我试图使页面中名为“activity_urgent__important.xml”的 ListView 看起来像“custom_layout_rachel.xml”中的 ListView 。在这个文件中,我有以下代码:

<ListView
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:id="@+id/lvItems"
      tools:listitem="@layout/custom_layout_rachel"
 />

在 Android Studio 中,会显示自定义布局,但当我在模拟器上运行该应用程序时,它不存在。

此 Activity 的 Java 代码如下所示:

lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.custom_layout_rachel, items);
lvItems.setAdapter(itemsAdapter);

第三行是我的错误所在。

有谁知道为什么我不能这样做或者为什么我收到错误?

谢谢!!!

新:

       lvItems = (ListView) findViewById(R.id.lvItems);
    items = new ArrayList<String>();
    readItems();
    itemsAdapter = new CustomListAdapter(this, items);
    lvItems.setAdapter(itemsAdapter);

在“自定义列表适配器(this, items)”上出现错误

我没有适配器代码,但我确实启动了以下内容,如果它能工作,我可以实现它:

public class CustomListAdapter extends ArrayAdapter {

    private Context mContext;
    private int id;
    private List<String> items ;

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
    {
        super(context, textViewResourceId, list);
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

public CustomListAdapter(Context context , List<String> list) {
    super(context, items);
}

@Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.textView);

        if(items.get(position) != null )
        {
            text.setTextColor(Color.WHITE);
            text.setText(items.get(position));
            text.setBackgroundColor(Color.RED);
            int color = Color.argb( 200, 255, 64, 64 );
            text.setBackgroundColor( color );

        }

        return mView;
    }

最佳答案

在第二个 CustomListAdapter 构造函数中,初始化 mContext 和项目,因为 mContext 将用于在 getView() 中膨胀 View 方法。

public CustomListAdapter(Context context , List<String> list) {
    super(context, items);
     mContext = context;
     items = list ;
}

关于java - Android Studio itemsAdapter 和自定义 ListView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33986141/

相关文章:

Java Gas Station 项目编码遇到的麻烦

java - 使用简单 Facebook API 记录时出现异常(没有打开 session 时请求 session )

android - 从android中的onKeyDown事件获取Unicode字符

iphone - 如何在 Objective-C 中解析包含 XML 的 NSString?

python - 如果特定 XML 结构通过,则过滤值

java - Play 2.0 Framework模板引擎中的计算

java - Android Realm - 从服务访问 Realm 对象

android - Paypal 整合 (MECL)

android - 如何从 Glass 流式传输视频?

Android 应用主题 textViewStyle 在手机上不起作用