简体   繁体   English

无法将 ArrayList 放入 Android Studio 中 CustomAdapter 内的 ListView

[英]Can't put an ArrayList in a ListView inside a CustomAdapter in Android Studio

I'm having a problem understanding how to finish this part of my code.我在理解如何完成这部分代码时遇到问题。 It's an app that searches a list of games with the help of an API.这是一个借助 API 搜索游戏列表的应用程序。 Everything is working so far so good right now, but one final thing.到目前为止,一切都很好,但最后一件事。

In the code, first of all I have a simple activity with an edit_text, a button and an empty list view that it is called "lv_listofgames".在代码中,首先我有一个简单的活动,其中包含一个edit_text、一个按钮和一个名为“lv_listofgames”的空列表视图。

Then, when I press the "search" button, I fill the "lv_listofgames" with a series of rows formed by an imageview, a listView called "list_item_text" and a button.然后,当我按下“搜索”按钮时,我用由 imageview、一个名为“list_item_text”的列表视图和一个按钮组成的一系列行填充“lv_listofgames”。 To this point everything is okay it seems.到目前为止,一切似乎都很好。

Then I should just fill the "list_item_text" inside the "lv_listofgames" with the contents of an arraylist but I just can't make it happen.然后我应该用 arraylist 的内容填充“lv_listofgames”中的“list_item_text”,但我无法实现。 I tried in many ways but I'm stuck.我尝试了很多方法,但我被卡住了。 I even tried using 2 adapters but the app crashed everytime or the "list_item_text" remained empty.我什至尝试使用 2 个适配器,但应用程序每次都崩溃或“list_item_text”仍然为空。

The arrayList is something like: [game_title='Name', release_date='date', platform=platform] arrayList 类似于:[game_title='Name', release_date='date', platform=platform]

I seem so close to the solution but I just can't figure it out how to accomplish that.我似乎非常接近解决方案,但我无法弄清楚如何实现这一目标。 Im going crazy:(我要疯了:(

tl;dr: problem: when I press the "search" button the arrayList content doesn't appear in the ListView "list_item_text". tl; dr:问题:当我按下“搜索”按钮时,arrayList 内容不会出现在 ListView“list_item_text”中。

Here is the code, tell me if something is wrong, thanks:这是代码,如果有问题请告诉我,谢谢:

public class MainActovity extends AppCompatActivity {

    EditText et_searchName;
    Button btn_search;
    ListView lv_listofgames;
    ListView lv;

    final GamesDataService gameDataService = new GamesDataService(MainActovity.this);

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        et_searchName = findViewById(R.id.et_searchName);
        btn_search = findViewById(R.id.btn_search);
        lv_listofgames= findViewById(R.id.lv_listofgames);

        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameDataService.getGameName(et_searchName.getText().toString(), new GamesDataService.searchGamesResponse() {
                    @Override
                    public void onError(String message) {
                        Toast.makeText(MainActovity.this, "Error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(List<GamesReportModel> gamesReportModels) {

                        List<GamesReportModel> newName = gamesReportModels;

                        List<String> stringsList = new ArrayList<>(newName.size());
                        for (Object object : newName) {
                            stringsList.add(Objects.toString(object, null));
                        }

                        System.out.println("stringsList:" + stringsList);

                        lv = (ListView) findViewById(R.id.lv_listofnames);
                        MyListAdapter adapter = new MyListAdapter(MainActovity.this, R.layout.details, stringsList);
                        lv.setAdapter(adapter);

                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    class MyListAdapter extends ArrayAdapter<String> {
        private int layout;
        public MyListAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
            super(context, resource, objects);
            layout = resource;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            MainActovity.ViewHolder mainViewHolder = null;
            if(convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(layout, parent, false);
                MainActovity.ViewHolder viewHolder = new MainActovity.ViewHolder();
                viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
                viewHolder.title = (ListView) convertView.findViewById(R.id.list_item_text);
                viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
                convertView.setTag(viewHolder);

                viewHolder.button.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
            else {
                mainViewHolder = (MainActovity.ViewHolder) convertView.getTag();
            }
            return convertView;
        }
    }
    public class ViewHolder {
        ImageView thumbnail;
        ListView title;
        Button button;
    }
}

GamesReportModel:游戏报告模型:

public class GamesReportModel {

    private String game_title;
    private String release_date;
    private String platform;
 
    public GamesReportModel(String game_title, String release_date, String platform) {
        this.game_title = game_title;
        this.release_date = release_date;
        this.platform = platform;
    }

    public GamesReportModel() {
    }

    @Override
    public String toString() {
        return  "game_title='" + game_title + '\'' +
                ", release_date='" + release_date + '\'' +
                ", platform=" + platform;
    }

    public String getGame_title() {
        return game_title;
    }

    public void setGame_title(String game_title) {
        this.game_title = game_title;
    }

    public String getRelease_date() {
        return release_date;
    }

    public void setRelease_date(String release_date) {
        this.release_date = release_date;
    }

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }
}

There are two things you need to change in your code to get the desired effect.您需要在代码中更改两件事才能获得所需的效果。

  1. In your row view layout ( R.layout.details ), replace the ListView with a TextView since you are just trying to show text for a given row (not a nested list inside each row).在您的视图布局( R.layout.details )中,将ListView替换为TextView因为您只是试图显示给定行的文本(而不是每行内的嵌套列表)。 Then update the view holder to hold the correct view type as well然后更新视图持有者以保持正确的视图类型
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text);
 
 //...
 
public class ViewHolder {
  ImageView thumbnail;
  TextView title;
  Button button;
}
  1. In the adapter's getView method you have to actually set the text to show for that row.在适配器的getView方法中,您必须实际设置要为该行显示的文本。 You never set the text to show anywhere, which is why your rows are blank.您从未将文本设置为在任何地方显示,这就是您的行为空白的原因。 That should look like this:这应该是这样的:
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    MainActovity.ViewHolder mainViewHolder = null;
    if(convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(layout, parent, false);
        MainActovity.ViewHolder viewHolder = new MainActovity.ViewHolder();
        viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
        viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text);
        viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
        convertView.setTag(viewHolder);
    }
    else {
        mainViewHolder = (MainActovity.ViewHolder) convertView.getTag();
    }
    
    // Here, you need to set what values to show for this row - this
    // is why your list is empty/blank
    mainViewHolder.title.setText((String)getItem(position));
    
    return convertView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM