java - 在 ListView 中使用 XML 布局扩展自定义 View

标签 java android listview canvas

我正在尝试在 ListView 中创建一个元素,它显示几个 TextView 和一个自定义 View ,我计划在其中通过覆盖 onDraw() 来绘制到 Canvas 。我正在尝试的方法仅适用于 TextViews,但当我尝试使用自定义元素 (Graph) 时失败,所以问题肯定出在这里。

我有以下 XML 文件,我将其用作每个 ListView 项目的布局:

year_row.xml:

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/year_row_linear"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="6dp"
    >
    <TextView android:id="@+id/year_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:textColor="@android:color/white"
        />
    <com.android.gradetracker.Graph android:id="@+id/year_graph"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        />
    <TextView android:id="@+id/year_average"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        />
    <TextView android:id="@+id/year_progress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />    
</LinearLayout>

我的Graph类如下:

图形.java:

package com.android.gradetracker;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

public class Graph extends View {

    public Graph(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // to test if it works
        canvas.drawColor(Color.WHITE);
        super.onDraw(canvas);
    }
}

设置 ListView 元素的主类包含以下代码:

    private ArrayList <HashMap<String,Object>> list = new ArrayList <HashMap<String,Object>>();
    private SimpleAdapter adapter;
    private ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listView = (ListView) findViewById(R.id.listview);
        listView.setClickable(true);

        String[] from = {"name", "graph", "average", "progress"};
        int[] to = new int[] {R.id.year_name, R.id.year_graph, R.id.year_average, R.id.year_progress};

        adapter = new SimpleAdapter(this.getApplicationContext(), list, R.layout.year_row, from, to);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    public void addYear(String name) {
        Graph graph = new Graph(this);
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("name", name);
        map.put("graph", graph);
        map.put("average", "--%");
        map.put("progress", "--/--%");
        list.add(map);
        adapter.notifyDataSetChanged();
    }

正如我所说,删除所有对 Graph 类的引用,应用程序运行良好。我只是无法显示 Graph。

Logcat 给出错误:

    08-04 17:09:50.415: ERROR/AndroidRuntime(397): android.view.InflateException: Binary XML file line #16: Error inflating class com.android.gradetracker.Graph

提前感谢您的帮助。

最佳答案

好的,主要问题似乎是 SimpleAdaptor 不允许您使用自定义 View 。我必须创建一个扩展 BaseAdapter 的自定义适配器以显示自定义 View ,使用以下 getView() 方法代码:

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

TextView name = (TextView) convertView.findViewById(R.id.year_name);
Graph graph = (Graph) convertView.findViewById(R.id.year_graph);
TextView average = (TextView) convertView.findViewById(R.id.year_average);
TextView progress = (TextView) convertView.findViewById(R.id.year_progress);

    ArrayList<Object> row = list.get(position);

    name.setText(String.valueOf(row.get(0)));
    graph = (Graph)row.get(1);
    average.setText(String.valueOf(row.get(2)));
    progress.setText(String.valueOf(row.get(3)));

    return convertView;
}

关于java - 在 ListView 中使用 XML 布局扩展自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946107/

相关文章:

android - 更快的图像处理

java - 尝试从 flickr 获取数据时出现 UnknownHostException?

java - Android 需要强制转换 findViewById() 返回值吗?

java - DNA 文件转换的位操作

java - 关于 LinkedBlockingQueue 迭代器从不抛出 ConcurrentModificationException

java - 执行 put through java 客户端时出现 Aerospike 异常错误代码 4 参数错误

java - 如何根据用户输入重新启动我的java程序?

android - 在特定 X-Y​​ 坐标的 Android ImageView 上添加 OverlayView

java - 无法在listview java中显示来自php mysql的数据

android - 如何在 Android 中创建这种类型的 Listview?