我有带碎片的导航抽屉。 我从URL片段中设置自定义ListView。 但是当我运行它时,ListView中的Items没有显示。 数据也显示在log cat中。我没有在logcat上得到任何错误
片段:
public class HomeFragment extends Fragment {
JSONArray post = null;
private ArrayList<HashMap<String, String>> PostList = new ArrayList<HashMap<String, String>>();
private View rootView;
private ListView list;
private StringRequest sr;
private RequestQueue queue;
private JSONArray laporanListObj;
private int Jumlah_list_Data;
private Adapter_Post adapter;
private Animation animstatuskoneksi;
private TextView statusKoneksi;
private ImageView nodata;
private ProgressBar load;
private Handler handler;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
// adapter list
adapter = new Adapter_Post(getActivity(), PostList);
// animasi
animstatuskoneksi = AnimationUtils.loadAnimation(getActivity(),
R.anim.anim_status_koneksi);
// View Declare
list = (ListView) rootView.findViewById(android.R.id.list);
statusKoneksi = (TextView) rootView.findViewById(R.id.status_koneksi);
nodata = (ImageView) rootView.findViewById(R.id.NoData);
load = (ProgressBar) rootView.findViewById(R.id.load);
// set adapter
list.setAdapter(adapter);
// Listview on item click listener
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
TextView judul = (TextView) view.findViewById(R.id.judul);
TextView waktu_post = (TextView) view
.findViewById(R.id.waktu_post);
TextView id_post = (TextView) view.findViewById(R.id.id_post);
TextView link_detail_post = (TextView) view
.findViewById(R.id.link_detail_post);
String var_judul = judul.getText().toString();
String var_waktu_post = waktu_post.getText().toString();
String var_id_post = id_post.getText().toString();
String var_link_detail_post = link_detail_post.getText()
.toString();
// Starting single contact activity
// Intent in = new Intent(getActivity(), DetailActivity.class);
// in.putExtra(Variabel.KEY_JUDUL, var_judul);
// in.putExtra(Variabel.KEY_WAKTU_POST, var_waktu_post);
// in.putExtra(Variabel.KEY_ID_POST, var_id_post);
// in.putExtra(Variabel.KEY_LINK_DETAIL_POST,
// var_link_detail_post);
// startActivity(in);
Toast.makeText(getActivity(), "oke", Toast.LENGTH_LONG).show();
}
});
// Calling async task to get json
GetDataPost(Variabel.url_post);
return rootView;
}
public void GetDataPost(String url) {
if (queue != null) {
queue.cancelAll(sr);
}
// adapter.delete_all();
load.setVisibility(View.VISIBLE);
Log.v("Get Data", url);
queue = Volley.newRequestQueue(getActivity());
sr = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.v("hasil", response);
load.setVisibility(View.GONE);
DrawData(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof NetworkError) {
statusKoneksi("Koneksi bermasalah");
} else if (error instanceof ServerError) {
statusKoneksi("Server Error");
} else if (error instanceof AuthFailureError) {
statusKoneksi("Koneksi Timeout");
} else if (error instanceof ParseError) {
statusKoneksi("Ada masalah dalam mengambil data");
} else if (error instanceof NoConnectionError) {
statusKoneksi("Tidak ada koneksi");
} else if (error instanceof TimeoutError) {
statusKoneksi("Koneksi Timeout");
}
load.setVisibility(View.GONE);
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("tag", Variabel.tag_default);
return jsonParams;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
}
protected void DrawData(String response) {
JSONObject json = null;
try {
json = new JSONObject(response);
String res = json.getString(Variabel.KEY_SUCCESS);
String msg_err = json.getString(Variabel.KEY_ERROR_MSG);
if (Integer.parseInt(res) == 1) {
laporanListObj = json.getJSONArray(Variabel.KEY_POST);
Jumlah_list_Data = laporanListObj.length();
if (Jumlah_list_Data > 0) {
// count_data.setText(Jumlah_list_Data+" Data");
list.setVisibility(View.VISIBLE);
nodata.setVisibility(View.GONE);
for (int i = 0; i < Jumlah_list_Data; i++) {
JSONObject c = laporanListObj.getJSONObject(i);
// Storing each json item in variable
Variabel.judul_post = c.getString(Variabel.KEY_JUDUL);
Variabel.waktu_post = c
.getString(Variabel.KEY_WAKTU_POST);
Variabel.id_post = c.getString(Variabel.KEY_ID_POST);
Variabel.link_detail_post = c
.getString(Variabel.KEY_LINK_DETAIL_POST);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(Variabel.KEY_JUDUL, Variabel.judul_post);
map.put(Variabel.KEY_WAKTU_POST, Variabel.waktu_post);
map.put(Variabel.KEY_ID_POST, Variabel.id_post);
map.put(Variabel.KEY_LINK_DETAIL_POST,
Variabel.link_detail_post);
// adding HashList to ArrayList
PostList.add(map);
}
adapter.notifyDataSetChanged();
}
} else {
list.setVisibility(View.GONE);
nodata.setVisibility(View.VISIBLE);
statusKoneksi(msg_err);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Listview适配器:
public class Adapter_Post extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private ImageLoaderDetailImage ImageLoader;
private int loader;
private static LayoutInflater inflater = null;
public Adapter_Post(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageLoader = new ImageLoaderDetailImage(activity.getBaseContext());
loader = R.drawable.ic_drawer;
}
public int getCount() {
return data.size();
}
public void delete_all() {
int count = getCount();
if (count > 0) {
data.clear();
notifyDataSetChanged();
}
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/* private view holder class */
private class ViewHolder {
public TextView judul;
public TextView waktu_post;
public TextView id_post;
public TextView link_detail_post;
// public RoundedImageView foto_bus;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.from(activity)
.inflate(R.layout.list_item, null, true);
holder = new ViewHolder();
// holder.foto_bus =
// (RoundedImageView)vi.findViewById(R.id.foto_bus);
holder.judul = (TextView) vi.findViewById(R.id.judul);
holder.waktu_post = (TextView) vi.findViewById(R.id.waktu_post);
holder.id_post = (TextView) vi.findViewById(R.id.id_post);
holder.link_detail_post = (TextView) vi
.findViewById(R.id.link_detail_post);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
HashMap<String, String> kendaraan = new HashMap<String, String>();
kendaraan = data.get(position);
// set data
holder.judul.setText(kendaraan.get(Variabel.KEY_JUDUL));
holder.waktu_post.setText(kendaraan.get(Variabel.KEY_WAKTU_POST));
holder.id_post.setText(kendaraan.get(Variabel.KEY_ID_POST));
holder.link_detail_post.setText(kendaraan
.get(Variabel.KEY_LINK_DETAIL_POST));
// ImageLoader.DisplayImage(kendaraan.get(Variabel.KEY_FOTO_BUS),loader,
// holder.foto_bus);
return vi;
}
}
已更新
片段XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:cacheColorHint="@android:color/transparent"
android:divider="@android:color/transparent"
android:dividerHeight="1px"
android:listSelector="@null"
android:visibility="visible" >
</ListView>
<ProgressBar
android:id="@+id/load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/NoData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@null"
android:src="@drawable/nodata"
android:visibility="visible" />
<TextView
android:id="@+id/status_koneksi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#99000000"
android:gravity="center"
android:padding="8dp"
android:text="msg."
android:textColor="#ffffff"
android:visibility="visible" />
</RelativeLayout>
我的代码中有任何错误? 怎么解决? 对不起我的英文..谢谢
答案 0 :(得分:1)
你应该改变这个
list=(ListView)rootView.findViewById(android.R.id.list);
到
list=(ListView)rootView.findViewById(R.id.list);
并确保ListView
布局中包含<{>} list
的{{1}}。
答案 1 :(得分:1)
在适配器的 getview 方法中更改此参数:
vi = inflater.from(activity).inflate(R.layout.list_item,null,true);
为:
vi = inflater.from(activity).inflate(R.layout.list_item, parent, false);