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

huangapple go评论75阅读模式
英文:

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 theReason){
        reason = theReason;
    }
    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 placeTextView = (TextView) listItem.findViewById(R.id.placetxt);
        placeTextView.setText(currentWord.getPlaceWord());
        TextView addressTextView = (TextView) listItem.findViewById(R.id.adddrestxt);
        addressTextView.setText(currentWord.getAddressWord());
        if (currentWord.oneText()) {
            TextView reasonTextView = (TextView) listItem.findViewById(R.id.placetxt);
            reasonTextView.setText(currentWord.getReason());
        } else {
            placeTextView.setVisibility(View.GONE);
            addressTextView.setVisibility(View.GONE);
        }
        View textContainer = listItem.findViewById(R.id.list_item);
        int color = ContextCompat.getColor(getContext(), mColorResourceId);
        textContainer.setBackgroundColor(color);
        return listItem;
    }
}

您可以在链接 1 中查看相关截图。

英文:

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

public class Data {
private String placeWord;
private String addressWord;
private String reason = ONE_TEXT;
private static final String ONE_TEXT = &quot;ah&quot;;
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&lt;Data&gt; 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;
    }
}

答案1

得分: 0

使用一个"recycler view",并将这个逻辑添加到它的适配器中。

英文:

Use a recycler view and add this logic in its adapter.

huangapple
  • 本文由 发表于 2020年10月2日 16:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64168213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定