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

问题描述

现在,我想列出不同类别的项目,例如:其中一家医院,其中一项必须包含名称,地址和电话号码,如果索引中的一种文本不可见,则我在适配器中创建了此if语句其他人,但对我没有任何帮助code

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,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;
    }
}

解决方法

使用回收站视图,并将此逻辑添加到其适配器中。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...