【发布时间】:2020-10-02 08:10:47
【问题描述】:
现在我想列出不同类别的项目,例如:其中一个是医院,一个项目必须包含姓名、地址和电话号码,如果索引有一个不可见的文本,我在适配器中创建了这个 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,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;
}
}
【问题讨论】:
-
请以 sn-p 或 pastebin 链接的形式提供您的代码,但不要以图片的形式(两次)。谢谢
-
欢迎来到 Stack Overflow。您能否更详细地描述“它对我不起作用”?会发生什么,您有什么期待?
-
当我运行时,一些文本没有出现,它必须出现。如:医院类别中的列表项之一“医院名称及其地址和电话号码”另一个类别示例最吸引人的类别之一它的列表项必须有“一个文本”我预计会这样但没有发生,活动工作好吧,我使用的构造函数有2个参数,其他没有@Milgo
标签: java android arraylist android-arrayadapter custom-arrayadapter