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

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

show different list items on different catagories activities

问题

现在我想要在不同类别中列出项目,例如:其中一个类别是医院,其中一个项目必须包含名称、地址和电话号码。我在适配器中创建了这个if语句,如果索引具有一个文本不可见,其他的文本可见,但是它在我这里运行得不好,需要帮助。

以下是您提供的代码:

  1. public class Data {
  2. private String placeWord;
  3. private String addressWord;
  4. private String reason = ONE_TEXT;
  5. private static final String ONE_TEXT = "ah";
  6. public Data(String mPlaceWord, String mAddressWord){
  7. placeWord = mPlaceWord;
  8. addressWord = mAddressWord;
  9. }
  10. public Data(String theReason){
  11. reason = theReason;
  12. }
  13. public String getPlaceWord(){
  14. return placeWord;
  15. }
  16. public String getAddressWord(){
  17. return addressWord;
  18. }
  19. public String getReason(){
  20. return reason;
  21. }
  22. public boolean oneText(){
  23. return reason != ONE_TEXT;
  24. }
  25. }
  26. public class DataAdapter extends ArrayAdapter {
  27. private int mColorResourceId;
  28. public DataAdapter(@NonNull Context context, ArrayList<Data> resource, int ColorResourceId) {
  29. super(context, 0, resource);
  30. mColorResourceId = ColorResourceId;
  31. }
  32. @NonNull
  33. @Override
  34. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  35. View listItem = convertView;
  36. if (listItem == null) {
  37. listItem = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
  38. }
  39. Data currentWord = (Data) getItem(position);
  40. TextView placeTextView = (TextView) listItem.findViewById(R.id.placetxt);
  41. placeTextView.setText(currentWord.getPlaceWord());
  42. TextView addressTextView = (TextView) listItem.findViewById(R.id.adddrestxt);
  43. addressTextView.setText(currentWord.getAddressWord());
  44. if (currentWord.oneText()) {
  45. TextView reasonTextView = (TextView) listItem.findViewById(R.id.placetxt);
  46. reasonTextView.setText(currentWord.getReason());
  47. } else {
  48. placeTextView.setVisibility(View.GONE);
  49. addressTextView.setVisibility(View.GONE);
  50. }
  51. View textContainer = listItem.findViewById(R.id.list_item);
  52. int color = ContextCompat.getColor(getContext(), mColorResourceId);
  53. textContainer.setBackgroundColor(color);
  54. return listItem;
  55. }
  56. }

您可以在链接 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

  1. public class Data {
  2. private String placeWord;
  3. private String addressWord;
  4. private String reason = ONE_TEXT;
  5. private static final String ONE_TEXT = &quot;ah&quot;;
  6. public Data(String mPlaceWord , String mAddressWord){
  7. placeWord = mPlaceWord;
  8. addressWord = mAddressWord;
  9. }
  10. public Data(String theReson){
  11. reason = theReson;
  12. }
  13. public String getPlaceWord(){
  14. return placeWord;
  15. }
  16. public String getAddressWord(){
  17. return addressWord;
  18. }
  19. public String getReason(){return reason;}
  20. public boolean oneText(){
  21. return reason != ONE_TEXT;
  22. }
  1. public class DataAdapter extends ArrayAdapter {
  2. private int mColorResourceId;
  3. public DataAdapter(@NonNull Context context, ArrayList&lt;Data&gt; resource ,int ColorResourceId) {
  4. super(context,0, resource);
  5. mColorResourceId = ColorResourceId ;
  6. }
  7. @NonNull
  8. @Override
  9. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  10. View listitem = convertView;
  11. if( listitem == null){
  12. listitem = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
  13. }
  14. Data currentword = (Data) getItem(position);
  15. TextView pd = (TextView) listitem.findViewById(R.id.placetxt);
  16. pd.setText(currentword.getPlaceWord());
  17. TextView ad = (TextView) listitem.findViewById(R.id.adddrestxt);
  18. ad.setText(currentword.getAddressWord());
  19. if (currentword.oneText()){
  20. TextView ps = (TextView) listitem.findViewById(R.id.placetxt);
  21. ps.setText(currentword.getReason());
  22. } else{
  23. pd.setVisibility(View.GONE);
  24. ad.setVisibility(View.GONE);
  25. }
  26. View textContainer = listitem.findViewById(R.id.list_item);
  27. // Find the color that the resource ID maps to
  28. int color = ContextCompat.getColor(getContext(), mColorResourceId);
  29. // Set the background color of the text container View
  30. textContainer.setBackgroundColor(color);
  31. return listitem;
  32. }
  33. }

答案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:

确定