我有从服务器收到的数据,接收部分也很顺利。但是当没有数据时会出现此错误int java.util.List.size()
,我知道这意味着没有数据。
我尝试使用get Count
处理,但它仍然给我一个错误,所以我决定从这里寻求帮助。我能做错什么?
提前感谢您的帮助!
这是我的代码:
private void DataFound(){
final ArrayAdapter<DataObject> adapter = new ArrayAdapter<DataObject>(getApplicationContext(), R.layout.data_item, searchResults){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.video_item, parent, false);
}
TextView title = convertView.findViewById(R.id.video_title);
DataObject searchResult = searchResults.get(position);
title.setText(searchResult.getTitle());
return convertView;
}
};
if(adapter.getCount() == 0){
emptyView.setVisibility(View.VISIBLE);
}else {
ListView.setAdapter(adapter);
}
}
答案 0 :(得分:0)
如果您使用搜索结果检查似乎是java.util.List
的内容,则应以防御性和防弹方式执行此操作。应该有两个检查:
以下是一个关于如何做到这一点的虚拟示例:
private void dataFound(){
List<SearchResult> searchResults = ...
final ArrayAdapter<DataObject> adapter = new ArrayAdapter<DataObject>(getApplicationContext(), R.layout.data_item, searchResults){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.video_item, parent, false);
}
TextView title = convertView.findViewById(R.id.video_title);
if(searchResults != null && !searchResults.isEmpty()) {
DataObject searchResult = searchResults.get(position);
title.setText(searchResult.getTitle());
}
else {
title.setText("No search results available. Please try again with another input, like e.g: 'foo' or 'bar'");
}
return convertView;
}
};
if(adapter.getCount() == 0){
emptyView.setVisibility(View.VISIBLE);
}else {
ListView.setAdapter(adapter);
}
}
private static class SearchResult {
private String title;
public SearchResult(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
答案 1 :(得分:0)
如下所示定义适配器:
public class Adapter extends ArrayAdapter<DataObject> {
private List<DataObject> dataObjectList;
public Adapter(@NonNull Context context, int resourceId) {
super(context, resourceId);
this.dataObjectList= new ArrayList<>();
}
public void setDataObjectList(List<DataObject> dataObjectList) {
this.dataObjectList= dataObjectList;
notifyDataSetChanged();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.video_item, parent, false);
}
TextView title = convertView.findViewById(R.id.video_title);
DataObject searchResult = dataObjectList.get(position);
title.setText(searchResult.getTitle());
return convertView;
}
@Override
public int getCount() {
return dataObjectList.size();
}
}
初始化并将数据设置为适配器,如下所示:
private void dataFound() {
List<SearchResult> searchResults = ...
if (searchResults != null && !searchResults.isEmpty()) {
Adapter adapter = new Adapter(getApplicationContext(), R.layout.your_layout);
adapter.setDataObjectList(searchResults);
} else {
// give error message
}
}
这样你就不必担心零项目的列表了。
答案 2 :(得分:0)
请设置这样的条件 if(searchResults!= null&amp;&amp; searchResults.size()&gt; 0){ 设置适配器 } 其他{ 显示吐司 }