仅当我单击/激活SearchView后,我的ListView才会显示,我想它会以某种方式刷新视图并显示ListView。
我尝试刷新(重建)视图和列表视图。
这是我的列表视图:
sviEventiListView = this.findViewById(R.id.ListListaEvenata);
sviEventiListView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return eventiList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) inflater.inflate(R.layout.eventistavka, null);
}
final Eventi row = eventiList.get(position);
TextView eventiName = (TextView) convertView.findViewById(R.id.EventNaziv);
eventiName.setText(row.getNaziv());
TextView eventiDesc = (TextView) convertView.findViewById(R.id.OpisEventa);
eventiDesc.setText(row.getOpis());
return convertView;
}
});
它在这部分代码之后运行(从数据库中获取数据,这应该可以正常工作):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
AndroidNetworking.enableLogging();
AndroidNetworking.get(Config.url+"api/Eventi").build().getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
Gson gson = new Gson();
try {
Log.d("Response:", response.getString(0));
JSONArray array = response;
for (int i = 0; i < array.length(); i++) {
JSONObject myData = array.getJSONObject(i);
JsonObject element = gson.fromJson(myData.toString(), JsonObject.class);
Eventi eventi = gson.fromJson(element, Eventi.class);
eventiList.add(eventi);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < eventiList.size(); i++) {
Log.wtf("TEST2", "onResponse: " + eventiList.get(i).getNaziv());
Log.wtf("TEST2", "onResponse: " + eventiList.get(i).getOpis());
}
}
@Override
public void onError(ANError anError) {
}
});
这也是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPageActivity"
android:padding="10dp"
>
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/PretragaBarId"
android:inputType="text"
android:queryHint="Pretraga"
/>
<TextView
android:id="@+id/noItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/noResult"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="30dp"
android:visibility="gone"/>
<ListView
android:id="@+id/ListListaEvenata"
android:layout_below="@id/PretragaBarId"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</RelativeLayout>
预期结果是在顶部显示“空”搜索栏,并在运行该应用程序后立即显示具有3个成员的列表,但只有在我单击SearchView后才会显示该列表。
Before clicking on the searchview
After clicking on the searchview
是的,它在关闭搜索视图后仍然存在,我尚未为其分配列表视图。
这是我的声明:
public class MainPageActivity extends AppCompatActivity {
private ListView sviEventiListView;
private BaseAdapter adapter;
//private int myLastVisiblePos;
TextView noItemsTextView;
private ProgressBar mainProgressBar;
private List<Eventi> eventiList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
答案 0 :(得分:1)
将数据加载到列表后,我看不到执行哪个方法,但是我认为您需要使用:
BaseAdapter myAdapter =new BaseAdapter;
myAdapter...//configure it
sviEventiListView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();//call this after loading data into list view
由于这个原因,您应该首先声明一个适配器对象。
答案 1 :(得分:0)
我认为问题在于数组没有保存您的实际数据,而是保存了对它们的引用。因此,一旦onResponse结束,原始JSON对象将被销毁,指向它们的数组将变为0。您可以在添加到列表之前尝试复制或clone()
数据吗?
答案 2 :(得分:0)
解决了!
唯一需要添加的内容:
sviEventiListView.requestLayout();