Adapter.getView在Android Studio中从不被调用。

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

Adapter.getView is never called Android Studio

问题

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;
    }
}
英文:

Sadly my getView method is never called using a custom adapter, I think it has to do something with the Thread.
I need this Thread to not get an Exception because I cant do Network Activity in the Main Thread obviously. Maybe there is a better option for this.
This is my first Project mit API's so I still need to learn a lot! Thanks for your answers!

    public class MainActivity extends AppCompatActivity {
List&lt;Schedule&gt; 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 &lt; 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&lt;Schedule&gt; races;
private Context context;
public startpage_lv_adapter(List&lt;Schedule&gt; 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 = &quot;https://restcountries.eu/rest/v2/name/&quot;;
String country = races.get(position).getCircuit().getLocation().getCountry();
String extendURL = &quot;?fields=alpha2Code&quot;;
try {
HttpResponse&lt;String&gt; httpResponse = Unirest.get(basicURL+country+extendURL)
.asString();
country = httpResponse.getBody().split(&quot;:&quot;)[1].replace(&#39;}&#39;, &#39; &#39;).replace(&#39;&quot;&#39;, &#39; &#39;).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 = &quot;https://www.countryflags.io/&quot;;
extendURL = &quot;/flat/64.png&quot;;
Picasso.get().load(basicURL+country+extendURL);
countryText.setText(races.get(position).getCircuit().getLocation().getCountry());
roundText.setText(&quot;Round &quot;+ position);
return convertView;
}
}

答案1

得分: 0

你必须在初始化适配器后(将初始值设置给它),而不是之前,所以将这行 lv.setAdapter(adapter); 移动到 adapter = new startpage_lv_adapter(races, MainActivity.this); 之后,我想知道你的应用怎么没有崩溃 Adapter.getView在Android Studio中从不被调用。

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

You have to set adapter after initializing it (set initial value to it) not before, so move this line lv.setAdapter(adapter); after adapter = new startpage_lv_adapter(races, MainActivity.this); , I wonder how your app doesn't crash Adapter.getView在Android Studio中从不被调用。

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

huangapple
  • 本文由 发表于 2020年3月15日 07:07:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/60688155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定