簡體   English   中英

在不同的類別活動中顯示不同的列表項

[英]show different list items on different catagories activities

現在我想列出不同類別的項目,例如:其中一個是醫院,一個項目必須包含名稱、地址和電話號碼,如果索引有一個文本不可見,我在適配器中創建了這個 if 語句,但是它對我不起作用 任何幫助代碼

public class Data {
private String placeWord;
private String addressWord;
private String reason = ONE_TEXT;
private static final String ONE_TEXT = "ah";


public Data(String mPlaceWord , String mAddressWord){
    placeWord = mPlaceWord;
    addressWord = mAddressWord;
}
public Data(String theReson){
   reason = theReson;
}
public String getPlaceWord(){
    return placeWord;
}
public String getAddressWord(){
    return addressWord;
}
public String getReason(){return reason;}

public boolean oneText(){
    return  reason != ONE_TEXT;
}
public class DataAdapter extends ArrayAdapter {
    private int mColorResourceId;

    public DataAdapter(@NonNull Context context, ArrayList<Data> resource ,int ColorResourceId) {
        super(context,0, resource);
    mColorResourceId =  ColorResourceId ;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listitem = convertView;
        if( listitem == null){
            listitem = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }
        Data currentword = (Data) getItem(position);
        TextView pd = (TextView) listitem.findViewById(R.id.placetxt);
        pd.setText(currentword.getPlaceWord());
        TextView ad = (TextView) listitem.findViewById(R.id.adddrestxt);
        ad.setText(currentword.getAddressWord());
        if (currentword.oneText()){
            TextView ps = (TextView) listitem.findViewById(R.id.placetxt);
            ps.setText(currentword.getReason());
        } else{
            pd.setVisibility(View.GONE);
            ad.setVisibility(View.GONE);
       }
        View textContainer = listitem.findViewById(R.id.list_item);
        // Find the color that the resource ID maps to
        int color = ContextCompat.getColor(getContext(), mColorResourceId);
        // Set the background color of the text container View
        textContainer.setBackgroundColor(color);
        return listitem;
    }
}

使用回收器視圖並將此邏輯添加到其適配器中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM