简体   繁体   English

在不同的类别活动中显示不同的列表项

[英]show different list items on different catagories activities

Now i want to list items in different categories ex: one of them hospitals and the one item must contain the name, the address and the phone number, I created this if statement in the adapter if the index of has one text invisible others but it didn't work with me well any help code现在我想列出不同类别的项目,例如:其中一个是医院,一个项目必须包含名称、地址和电话号码,如果索引有一个文本不可见,我在适配器中创建了这个 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