简体   繁体   中英

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. 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".

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. 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. 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.

The arrayList is something like: [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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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