我正在尝试在ListView中创建一个元素,该元素显示一些TextView和一个自定义视图,我计划通过覆盖onDraw()来绘制到画布。我正在尝试的方法只使用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>
My Graph类如下:
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类的所有引用,应用程序工作正常。我只是在显示图表时遇到了麻烦。
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
感谢您提前提供任何帮助。
答案 0 :(得分:2)
好的,主要问题似乎是SimpleAdaptor不允许您使用自定义视图。我必须创建一个自定义适配器,它扩展BaseAdapter以显示自定义视图,以及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;
}
答案 1 :(得分:0)
首先致电 super
更改onDraw
Graph class
功能
@Override
protected void onDraw(Canvas canvas) {
//start with super
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
}
答案 2 :(得分:0)
您需要Graph
课程中的构造函数。