2015-10-27 26 views
2

所以大家好,在我的項目中我使用自定義listView,起初我只傳遞給適配器String的,但現在我想傳遞類的,因爲我想傳遞更多的信息。但我不是爲什麼它不會讓我! 這裏是我的適配器字符串尚未:ArrayAdapter與類

class costum_adapter extends ArrayAdapter<String>{ 

    public costum_adapter(Context context, ArrayList<String> horarios) { 
     super(context, R.layout.costum_listview ,horarios); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater horarioInflater = LayoutInflater.from(getContext()); 
     View costumView = horarioInflater.inflate(R.layout.costum_listview, parent, false); 

     String singleHorario = getItem(position); 
     TextView hora = (TextView) costumView.findViewById(R.id.Hora); 
     TextView nota = (TextView) costumView.findViewById(R.id.Nota); 

     hora.setText(singleHorario); 
     nota.setText(" "); 

     return costumView; 
    } 
} 

現在,我想,以獲得類的,我只需要改變的說法,這裏是ArrayList的horarios我會改變的ArrayList horarios。 「horario」是我的課程,在這裏:

public class horario{ 
    public String hora; 
    public int destino; 
    public int note; 

    public horario(String horaIn, int Destino, int Nota){ 
     hora = horaIn; 
     destino = Destino; 
     note = Nota; 
    } 
} 

我也在使用ArrayList。但是我用類中的對象填充了ArrayList。 當我嘗試更改適配器上的參數以接收我的類時,它無法識別它,我開始鍵入「horario」但它不被識別,我不知道爲什麼,因爲我在其他文件中有該類。

+1

您是否注意到您的構造函數中存在拼寫錯誤? costum_adapter而不是custom_adapter。 –

+1

是的,我知道,那是因爲當我創建java文件時,我發現它錯了,然後我不得不聲明所有與costum_adapter,否則會給錯誤。 –

+0

請確保你的擴展正確做公共類擴展ArrayAdapter

回答

0

所以我設法解決我自己的問題。我的問題是我無法將「ArrayList」更改爲「ArrayList」,horario是我想要的類,但這是因爲「horario」是在更大的類中聲明的。所以我不得不改變爲「ArrayList」,「mostraHorario」是更大的類。

0

試試這樣說:

class costum_adapter extends ArrayAdapter<horario> { 

public costum_adapter(Context context, ArrayList<horario> horarios) { 
super(context, R.layout.costum_listview ,horarios); 
} 
+0

正如我在說明中解釋,這是有道理的,我試了一下。但問題是它不會識別我的班級。如果我更改爲「ArrayList 」,給我錯誤的「horario」,因爲它不承認它 –

+0

如果問題是它不識別horario,那麼只需將它導入到您的班級。 –

+0

導入它在哪裏?怎麼樣 ? –

0

嘗試這個

public costum_adapter(Context context, ArrayList<horario> horarios) { 
     super(context, R.layout.costum_listview ,horarios); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater horarioInflater = LayoutInflater.from(getContext()); 
    View costumView = horarioInflater.inflate(R.layout.costum_listview, parent, false); 

    String singleHorario = getItem(position).getHora(); 
    String singlenote = getItem(position).getNote(); 
    TextView hora = (TextView) costumView.findViewById(R.id.Hora); 
    TextView nota = (TextView) costumView.findViewById(R.id.Nota); 

    hora.setText(singleHorario); 
    nota.setText(singlenote); 

    return costumView; 
} 

模型類

public class horario{ 
public String hora; 
public int destino; 
public int note; 

public horario(String horaIn, int Destino, int Nota){ 
    hora = horaIn; 
    destino = Destino; 
    note = Nota; 
} 

public String getHora() { 
    return hora; 
} 

public void setHora(String hora) { 
    this.hora = hora; 
} 

public int getDestino() { 
    return destino; 
} 

public void setDestino(int destino) { 
    this.destino = destino; 
} 

public int getNote() { 
    return note; 
} 

public void setNote(int note) { 
    this.note = note; 
} 
} 
+0

正如我之前和在說明中所說的答案。我已經嘗試將「ArrayList 」更改爲「ArrayList 」,顯然這就是我需要的。但問題是,當我在那裏寫horario時,它不能識別它,給出錯誤「無法解析符號horario」。它不承認我的班級,我不知道爲什麼 –