You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中无法将ArrayList填充到CustomAdapter内ListView的问题

问题排查:Android游戏列表API搜索应用数据填充失败

我正在开发一款通过API搜索游戏列表的Android应用,目前其他功能运行正常,但遇到最后一个问题:主界面包含EditText、Button和名为lv_listofgames的空ListView;点击搜索按钮后,lv_listofgames会被填充为包含ImageView、名为list_item_text的ListView和Button的行结构,这一步表现正常。但无法将包含game_title、release_date、platform信息的ArrayList内容填充到list_item_text中,尝试多种方法甚至使用双适配器,要么应用崩溃,要么list_item_text仍为空。以下是相关代码,恳请帮忙排查问题:

MainActovity 代码

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

内容的提问来源于stack exchange,提问作者Lightball

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 10:54:14