我试图从我的MainActivity
显示我的列表碎片。我跟着这个https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial#android-listview-custom-adapter-overview。这就是
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fm = getSupportFragmentManager();
ListFragment fragment = null;
String title = getString(R.string.app_name);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (id == R.id.nav_rijek) {
fragment = new Rijek();
title = "Rijek Item from DO";
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_logout) {
Logoutdialog dialogFragment = new Logoutdialog();
dialogFragment.setRetainInstance(true);
dialogFragment.show( this .getFragmentManager(), "Tag");
}
if (fragment != null) {
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
return true;
}
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"/>
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
这是我的ListFragment(Rijek.java)
public class Rijek extends ListFragment {
ArrayList<RijekModel> dataModels;
ListView listView;
private static Rijekadapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Toast toast = Toast.makeText(getActivity(), "Load listfragment",
Toast.LENGTH_SHORT);
toast.show();
startASycnc();
View v = inflater.inflate(R.layout.rijekmain, container, false);
listView = (ListView) v.findViewById (android.R.id.list);
return v;
}
public void startASycnc() {
SessionHandler ses = new SessionHandler();
String Nip = new SessionHandler().getNip(getContext());
new getListRijek().execute(Nip);
}
public class getListRijek extends AsyncTask<String,String,String>{
HttpURLConnection conn;
URL url = null;
String result = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
url = new URL("http://192.168.3.223:84/storelf/api/Getrijek");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(100);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("user_login", params[0]);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "exception";
}
try {
int response_code = conn.getResponseCode();
if (response_code == HttpURLConnection.HTTP_OK) {
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = null;
while ((line = reader.readLine()) != null) {
result += line;
}
try {
JSONArray jsonArray = new JSONArray(result);
dataModels= new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String DrDocumentNo = c.getString("DrDocumentNo");
String DoDocumentNo = c.getString("DoDocumentNo");
String RijekCode = c.getString("RijekCode");
dataModels.add(new RijekModel(RijekCode,DrDocumentNo, DoDocumentNo));
}
adapter= new Rijekadapter(dataModels,getContext());
}
catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ERROR", "ERR --> " +response_code);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
}
这是我的rijekmain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
没有错误。但是当我运行它时,我的列表视图并没有显示出来。我该如何解决?提前谢谢,对不起我的英文
答案 0 :(得分:1)
您没有为ListView
设置适配器。当数据可用时,您必须创建/更新适配器并将其设置为ListView
。在onPostExecute()
方法内将适配器设置为ListView
ListFragment
使用:
setListAdapter(adapter);
在ListFragment
访问ListView,您可以调用
getListView();
答案 1 :(得分:0)
试试这个
listView = (ListView) v.findViewById (android.R.id.list);
Insted of
listView = (ListView) v.findViewById (R.id.list);
答案 2 :(得分:0)
您正在后台线程中更新 UI元素(列表视图) ..执行setAdapter
中的onPostExecute(...)
部分。在后台线程中获取json 从doInBackgroud()
返回结果 ..解析JSON对象并更新onPostExecute()
中的适配器