开发者

Inflating Custom View Using XML Layout Inside ListView

I'm trying to create an element within a ListView which displays a few TextViews and a custom view in which I plan to draw to the canvas by overriding onDraw(). The method I'm attempting works fine with just the TextViews, but fails when I try and use the custom element (Graph), so the problem definitely lies here.

I have the following XML file which I use as the layout for each ListView item:

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 class is as follows:

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);
    }
}

My main class which sets up the elements of the ListView then contains the following code:

    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();
    }

As I said, removing all references to the Graph class, the application works fine. I'm just having trouble getting Graph to display.

Logcat gives the error:

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

Thanks for any help in advance.


Okay, the main problem seemed to be that SimpleAdaptor does not allow you to use custom views. I had to create a custom adaptor which extends BaseAdapter in order to display the custom view, with the following code for the getView() method:

@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;
}


You need a constructor in your Graph class.


Start By calling super

Change the onDraw function of the Graph class

@Override
protected void onDraw(Canvas canvas) {
    //start with super
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜