在列表项视图上显示自定义视图

时间:2014-03-27 02:14:39

标签: android android-listview android-custom-view

这可能看起来很奇怪,但我正在尝试显示我在列表视图项上创建的自定义视图。 我的自定义视图绘制键盘和弦,当我在FragmentActivity上显示它们时,这个工作 ViewPager等我以前做过,创建了一个XML文件作为列表项并实现了一个类 从ArrayAdapter扩展。当我给XML文件充气时,它运行得很好,但它不起作用 当我使用我的自定义视图类时,它没有xml文件。你们有什么线索吗?我的代码如下。非常感谢:)

public class ShowChordActivityNew extends Activity {

    private ListView chordList;

    private List<KeyboardChordDiagram> aList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_chord_activity_new);

        chordList = (ListView) findViewById(R.id.lvChords);
        // this array is a chord I'm using to test this before moving to my working code
        int[] chord = new int[] { 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        aList = new ArrayList<KeyboardChordDiagram>();      
        aList.add(new KeyboardChordDiagram(getApplicationContext(), "A", chord));
        aList.add(new KeyboardChordDiagram(getApplicationContext(), "A", chord));
        aList.add(new KeyboardChordDiagram(getApplicationContext(), "A", chord));


        chordList.setAdapter(new MyAdpater());  
    }

    private class MyAdpater extends ArrayAdapter<KeyboardChordDiagram> {

        public MyAdpater() {
            super(getApplicationContext(), R.layout.fragment_list_item, aList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null) {
                //since my class extends View, this should work, right?
                KeyboardChordDiagram a = aList.get(position);       
                itemView = a;                   
            }

            return itemView;
        }

    }
}

//这是我的KeyboardChordDiagram类构造函数 公共类KeyboardChordDiagram扩展ChordDiagram {     .....

public KeyboardChordDiagram(Context context, String aChordName, int[] aChord) {
    super(context, aChordName);
    chord = aChord;
    .....
}

....

}

//这是ChordDiagram类:

公共抽象类ChordDiagram扩展了View实现OnClickListener {

.....

public ChordDiagram(Context context, String aChordName) {
    super(context);
    .....
}
....

}

1 个答案:

答案 0 :(得分:0)

您尚未将值绑定到itemView中的视图。我认为您需要将“a”对象中的值绑定到listview行。 getView

中缺少这个