ListFragment 一直为空
ListFragment keeps being empty
我想用从 API 端点获得的一些数据填充 ListFragment。我首先使用 ListView 对其进行了测试 - 它有效但现在将其更改为 ListFragment。
现在不再填充列表。我测试了 hashmap 是否可能为空,但事实并非如此。
现在我有点懵了-也许你能看出我犯的错误:
public class AuftrageFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.auftrag_view, container, false);
Context context = getContext();
final SharedPreferences tokenStore = context.getSharedPreferences("TokenStore", Context.MODE_PRIVATE);
String token = tokenStore.getString("token", null);
if (token == null) {
StartFragment start = new StartFragment();
getFragmentManager().beginTransaction()
.replace(R.id.frame_container, start).commit();
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
String[] from = {"auftragsnummer", "schadensbild", "termin"};
int[] to = new int[]{R.id.auftragsnummer, R.id.schadensbild, R.id.termin};
final ArrayList<HashMap<String,String>> auftragliste = new ArrayList<HashMap<String, String>>();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray ResponseArray = new JSONArray(response);
for(int i=0; i < ResponseArray.length(); i++){
JSONObject responseObject = ResponseArray.getJSONObject(i);
String termin = responseObject.getString("termin");
String schadensbild = responseObject.getString("schadensbild");
String auftragsnummer = responseObject.getString("auftragsnummer");
HashMap<String, String> map = new HashMap<String, String>();
map.put("auftragsnummer", auftragsnummer);
map.put("schadensbild", schadensbild);
map.put("termin", termin);
auftragliste.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Context context = getContext();
final SharedPreferences tokenStore = context.getSharedPreferences("TokenStore", Context.MODE_PRIVATE);
String token = tokenStore.getString("token", null);
MainListRequest mainListRequest = new MainListRequest(token, responseListener, null);
RequestQueue queue = Volley.newRequestQueue(this.getActivity());
queue.add(mainListRequest);
SimpleAdapter adapter = new SimpleAdapter(getActivity(), auftragliste,R.layout.auftrag_view, from, to);
setListAdapter(adapter);
}
}
auftrag_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Aufträge"
android:id="@+id/uberschrift"
android:layout_gravity="center_horizontal"
android:gravity="center" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/android:list"
android:choiceMode="singleChoice" />
</LinearLayout>
项目xml:
<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:baselineAligned="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Auftragsnummer"
android:id="@+id/auftragsnummer"
android:elegantTextHeight="true"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Schadensbild"
android:id="@+id/schadensbild"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:gravity="left" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Termin"
android:id="@+id/termin" />
</LinearLayout>
</LinearLayout>
您必须调用适配器#notifyDatasetChanged()
你能试试这个吗
(id for your listView).setListAdapter(adapter);
对于 'token',您期望 AuftrageFragment.onCreateView 的价值是多少?
onActivityCreated 在 onCreateView 之后被调用,你能检查一下容器中有哪个片段吗?我怀疑是 StartFragment
我想用从 API 端点获得的一些数据填充 ListFragment。我首先使用 ListView 对其进行了测试 - 它有效但现在将其更改为 ListFragment。
现在不再填充列表。我测试了 hashmap 是否可能为空,但事实并非如此。
现在我有点懵了-也许你能看出我犯的错误:
public class AuftrageFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.auftrag_view, container, false);
Context context = getContext();
final SharedPreferences tokenStore = context.getSharedPreferences("TokenStore", Context.MODE_PRIVATE);
String token = tokenStore.getString("token", null);
if (token == null) {
StartFragment start = new StartFragment();
getFragmentManager().beginTransaction()
.replace(R.id.frame_container, start).commit();
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
String[] from = {"auftragsnummer", "schadensbild", "termin"};
int[] to = new int[]{R.id.auftragsnummer, R.id.schadensbild, R.id.termin};
final ArrayList<HashMap<String,String>> auftragliste = new ArrayList<HashMap<String, String>>();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray ResponseArray = new JSONArray(response);
for(int i=0; i < ResponseArray.length(); i++){
JSONObject responseObject = ResponseArray.getJSONObject(i);
String termin = responseObject.getString("termin");
String schadensbild = responseObject.getString("schadensbild");
String auftragsnummer = responseObject.getString("auftragsnummer");
HashMap<String, String> map = new HashMap<String, String>();
map.put("auftragsnummer", auftragsnummer);
map.put("schadensbild", schadensbild);
map.put("termin", termin);
auftragliste.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Context context = getContext();
final SharedPreferences tokenStore = context.getSharedPreferences("TokenStore", Context.MODE_PRIVATE);
String token = tokenStore.getString("token", null);
MainListRequest mainListRequest = new MainListRequest(token, responseListener, null);
RequestQueue queue = Volley.newRequestQueue(this.getActivity());
queue.add(mainListRequest);
SimpleAdapter adapter = new SimpleAdapter(getActivity(), auftragliste,R.layout.auftrag_view, from, to);
setListAdapter(adapter);
}
}
auftrag_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Aufträge"
android:id="@+id/uberschrift"
android:layout_gravity="center_horizontal"
android:gravity="center" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/android:list"
android:choiceMode="singleChoice" />
</LinearLayout>
项目xml:
<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:baselineAligned="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Auftragsnummer"
android:id="@+id/auftragsnummer"
android:elegantTextHeight="true"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Schadensbild"
android:id="@+id/schadensbild"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:gravity="left" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Termin"
android:id="@+id/termin" />
</LinearLayout>
</LinearLayout>
您必须调用适配器#notifyDatasetChanged()
你能试试这个吗
(id for your listView).setListAdapter(adapter);
对于 'token',您期望 AuftrageFragment.onCreateView 的价值是多少?
onActivityCreated 在 onCreateView 之后被调用,你能检查一下容器中有哪个片段吗?我怀疑是 StartFragment