Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在ListView元素内绘制到画布_Java_Android_Listview_Canvas_Adapter - Fatal编程技术网

Java 在ListView元素内绘制到画布

Java 在ListView元素内绘制到画布,java,android,listview,canvas,adapter,Java,Android,Listview,Canvas,Adapter,我试图在ListView的每个元素内绘制画布。目前,我正在使用以下代码,在我启动活动时,该代码将立即强制关闭: Main.java 我意识到我的方法可能有很多问题,所以如果有人能给我指出正确的方向,我将不胜感激 提前谢谢你的帮助 编辑:ListView现在加载但不显示任何元素。我使用的是如下所示的main.xml文件 main.xml 您需要listRows=newarraylist()在向列表添加元素之前初始化列表 通常,尝试在Eclipse调试透视图或ddms中检查logcat的输出。在这

我试图在ListView的每个元素内绘制画布。目前,我正在使用以下代码,在我启动活动时,该代码将立即强制关闭:

Main.java

我意识到我的方法可能有很多问题,所以如果有人能给我指出正确的方向,我将不胜感激

提前谢谢你的帮助


编辑:ListView现在加载但不显示任何元素。我使用的是如下所示的main.xml文件

main.xml


您需要
listRows=newarraylist()
在向列表添加元素之前初始化列表

通常,尝试在Eclipse调试透视图或ddms中检查logcat的输出。在这种情况下,它会说一些类似于“onCreate中的空指针”的话,这样您就节省了一些时间

编辑我怀疑这是因为您的行视图不知道它们应该有多大。您可以尝试使用一个简单的方法覆盖测量上的
,作为健全性检查:

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   setMeasuredDimension(100, 100);
}
这样做的需要可以通过更好地分解代码来减轻。适配器的底层数据并不意味着直接接触视图;通常,视图是在
getView
中创建的

例如,您可能在
res/layout/row.xml
中有:

<?xml version="1.0" encoding="utf-8"?>
<com.example.HelloCanvas
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ... other attributes like background color ...
    />
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    HelloCanvas hello = (HelloCanvas) convertView;
    if (hello == null) {
        // inflate a new view
        hello = (HelloCanvas) 
          LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
    }
    hello.setHelloString(getItem(position));
    return hello;
}
最后,您的
getView
方法将膨胀
row.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.HelloCanvas
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ... other attributes like background color ...
    />
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    HelloCanvas hello = (HelloCanvas) convertView;
    if (hello == null) {
        // inflate a new view
        hello = (HelloCanvas) 
          LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
    }
    hello.setHelloString(getItem(position));
    return hello;
}

这样,如果不必生成新视图,您就不会生成新视图,而ArrayAdapter的底层数据类型(消除了一些多余的代码)可以是字符串。

请放慢速度,这样我也可以得到一些:pWell。。。你的答案更完整。。。如果我是正确的人,我会选择你的。@Cristian&Matthew:为快速回答干杯:)你是对的,我肯定应该检查logcat,我只是认为我的代码有很多问题。这肯定会修复force close,但列表元素似乎根本不显示。我是否需要向XML文件中添加一些内容。我在原来的问题中加了这个。再次感谢。覆盖测量有效。感谢您对如何改进代码的建议,我一定会尝试使用此结构重写它。再次感谢。
<?xml version="1.0" encoding="utf-8"?>
<com.example.HelloCanvas
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ... other attributes like background color ...
    />
public HelloCanvas(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    HelloCanvas hello = (HelloCanvas) convertView;
    if (hello == null) {
        // inflate a new view
        hello = (HelloCanvas) 
          LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
    }
    hello.setHelloString(getItem(position));
    return hello;
}