0
這可能看起來很奇怪,但我試圖顯示我在列表視圖項目上創建的自定義視圖。 我的自定義視圖平鍵盤和絃,當我告訴他們在這個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擴展視圖實現OnClickListener {
.....
public ChordDiagram(Context context, String aChordName) {
super(context);
.....
}
....
}
請出示'KeyboardChordDiagram'類,或至少它的構造。您應該在'getView'函數內創建新的'KeyboardChordDiagram'。你的問題是你沒有建立和絃與列表的父/子關係。 – frozenkoi
我編輯了這個問題來添加construtor。我正在努力理解我應該如何將和絃與列表綁定在一起。我試圖在getView()方法中實例化一個KeyboardChordDiagram,傳遞getContext()作爲參數,但它沒有工作:( –