2015-10-26 214 views
0

我正在爲公交車站創建應用程序,以提供時間表。爲此,我使用自定義列表視圖。那就是:自定義ListView - 自定義參數

class custom_adapter extends ArrayAdapter<String>{ 

public custom_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; 
} 

}

現在你可以看到我剛剛2 texViews然而,「霍拉」是顯示總線的定時器,在「諾塔」是一些筆記,就像有一天公共汽車不去或者類似的東西一樣。而我的問題正是在於「nota」textview。我有幾十個arrayList傳遞給這個自定義的ListView,以及幾十個和幾十個定時器,並且有一些定時器需要放置一個便條,其他的我不需要。所以,我可以給這個自定義ListView另外一個參數,就像一個布爾值或者其他東西,所以我可以在該代碼中做一個if/else來爲每一個添加一個註釋。爲了做到這一點,我需要改變什麼?我一直在努力,但並沒有完全做到這一點。

回答

0

讀取數據而不是使用字符串爲您ArrayAdapter參數,創建一個自定義類並使用它。
通過這種方式,您可以將所有想要的信息傳遞到適配器,然後按照您的喜好顯示。

public class custom_adapter extends ArrayAdapter<MyClass>{ 

public custom_adapter(Context context, ArrayList<MyClass> 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); 

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

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

    return costumView; 
} 

而且新類

public class MyClass { 
    public String hora; 
    public String nota; 
} 
+0

但問題是,如果我必須傳遞一個類,那麼這個類將不得不擁有ArrayList,但是我想放入的「nota」不是對所有Array的單個註釋,而是一個註釋, (或不)在數組的每一個項目中。不知道我能否更好地解釋。如果我要放一個音符,我傳遞的數組中的每一項都將被檢查。 –

0

如何讓持有兩個值'hora'和'nota'的類以及可以說boolean isNotaAvailable()方法。然後在getView()你只是做這樣的事情:

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

YourClassName singleHorario = getItem(position); 
TextView hora = (TextView) costumView.findViewById(R.id.Hora); 
TextView nota = (TextView) costumView.findViewById(R.id.Nota); 
// set hora text 
hora.setText(singleHorario); 
// check if nota is available, if true - set nota text 
if(singleHorario.isNotaAvailable()) { 
nota.setText(singleHorario.getNota())} 
else nota.setVisibility(View.GONE); 

return costumView; 
} 

這只是一個想法,告訴我,如果它可以幫助:)

+0

但問題是,如果我必須通過一個類,這個類必須有它的ArrayList中,但「諾塔」我希望把不是所有數組的單個音符,這是一個筆記,我把(或不)放在數組的每一個項目中。不知道我能否更好地解釋。如果我要放一個音符,我傳遞的數組中的每一項都將被檢查。 –

0

只是延長BaseAdapter,這樣你就可以定義數據結構。

class custom_adapter extends BaseAdapter 

變化ArrayList<String> horariosArrayList<Data> horariosData可以

public class Data{ 
    private String hora; 
    private String nota; 
    private boolean shouldShowNota; 

    //write getter and setter here 
} 

最後,在getView

Data data = getItem(position); 
if (data.getShouldShowNota) { 
    nota.setText(data.getNote); 
} 
hora.setText(data.getHora); 
+0

但問題是,如果我必須傳遞一個類,那麼類將不得不具有ArrayList,但是我想放入的「nota」不是對所有Array的單個註釋,而是一個註釋, (或不)在數組的每一個項目中。不知道我能否更好地解釋。如果我要放一個音符,我傳遞的數組中的每一項都將被檢查。 –