java - Adapter.getView 从未被称为 Android Studio

标签 java android multithreading adapter

遗憾的是,我的 getView 方法从未使用自定义适配器调用,我认为它必须对线程执行某些操作。 我需要这个线程不出现异常,因为我显然无法在主线程中执行网络 Activity 。也许对此有更好的选择。 这是我的第一个 mit API 项目,所以我还需要学习很多东西!感谢您的回答!

    public class MainActivity extends AppCompatActivity {
    List<Schedule> races;
    startpage_lv_adapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startPage(2020);
        ListView lv = findViewById(R.id.raceList);
        lv.setAdapter(adapter);
        if(races == null){
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        adapter = new startpage_lv_adapter(races, MainActivity.this);
        adapter.notifyDataSetChanged();
    }

    public void startPage(final int year) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Ergast ergast = new Ergast(year, 100, Ergast.DEFAULT_OFFSET);
                try {
                    races = ergast.getSchedules();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < races.size(); i++) {
                    Schedule race = races.get(i);

                    if (Date.valueOf(race.getDate()).before(Calendar.getInstance().getTime())) {
                        races.set(i, null);
                    }
                }
                races.removeAll(Collections.singletonList(null));
                int i = races.size();
                if (races.size() == 0) {
                    startPage(year+1);
                }

            }
        });
        thread.start();



    }

}


public class startpage_lv_adapter extends BaseAdapter {
    private List<Schedule> races;
    private Context context;

    public startpage_lv_adapter(List<Schedule> races, Context context) {
        this.races = races;
        this.context = context;
    }

    @Override
    public int getCount() {
        return races.size();
    }

    @Override
    public Object getItem(int position) {
        return races.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.listview_events_detail, parent, false);
        String basicURL = "https://restcountries.eu/rest/v2/name/";
        String country = races.get(position).getCircuit().getLocation().getCountry();
        String extendURL = "?fields=alpha2Code";
        try {
            HttpResponse<String> httpResponse = Unirest.get(basicURL+country+extendURL)
                    .asString();
             country = httpResponse.getBody().split(":")[1].replace('}', ' ').replace('"', ' ').trim();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        ImageView imageView = convertView.findViewById(R.id.img_countryFlag);
        TextView countryText = convertView.findViewById(R.id.text_countryName);
        TextView roundText = convertView.findViewById(R.id.text_roundNumber);
        basicURL = "https://www.countryflags.io/";
        extendURL = "/flat/64.png";
        Picasso.get().load(basicURL+country+extendURL);
        countryText.setText(races.get(position).getCircuit().getLocation().getCountry());
        roundText.setText("Round "+ position);
        return convertView;
    }
}

最佳答案

您必须在初始化适配器之后(为其设置初始值)而不是之前设置适配器,因此将此行 lv.setAdapter(adapter); 移到 adapter = new startpage_lv_adapter(races, MainActivity.this); ,我想知道你的应用程序如何不崩溃:D

adapter = new startpage_lv_adapter(races, MainActivity.this);
lv.setAdapter(adapter);

关于java - Adapter.getView 从未被称为 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60688155/

相关文章:

java - 编写数据库支持的 Web 服务的最快方法

android - 添加google-api-client时,Gradle构建失败并重复

android - 使用 Google Nearby Connections 2.0 时是否可以监控发现过程?

android - 从状态栏启动 Activity 会创建新 Activity ,即使 Activity 已经存在

java - JVM 语言是受 Java 内存模型约束还是仅受 Java 编程语言约束?

java - Spring MVC - 请求映射

java - 如何使用 JPA 将数据库表放入 DTO 的属性中

java - Java 类上的小别针图标 - Android Studio

c# - 无法使用多线程访问已处置的对象

java - Spring websocket线程模型