我刚刚在我的片段中使用asynctask创建了一个自定义列表视图。我不知道问题出在哪里,因为在logcat上从不显示错误。请有人可以帮助我
这是我的片段代码
public static class ListAnak extends Fragment{
private ProgressDialog pDialog;
private MenuItem newDataAnak;
private ListView listView;
private FroListAnakAdapter adapter;
private Streamer streamer;
private JSONArray jsonArray;
private JSONObject jsonObject;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fro_listanak, container, false);
adapter = new FroListAnakAdapter(new ArrayList<Anak>(), getActivity());
listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
(new Asyncronous()).execute();
return rootView;
}
private class Asyncronous extends AsyncTask<String, Void, List<Anak>> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<Anak> doInBackground(String... params) {
List<Anak> result = new ArrayList<Anak>();
try {
List<NameValuePair>nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("aksi", "dataanak"));
jsonArray = streamer.postData(VariabelGlobal.url+"/anak.php", nameValuePairs);
for (int i=0; i < jsonArray.length(); i++) {
result.add(convertContact(jsonArray.getJSONObject(i)));
}
return result;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
@Override
protected void onPostExecute(List<Anak> result) {
super.onPostExecute(result);
adapter.setItemList(result);
adapter.notifyDataSetChanged();
}
private Anak convertContact(JSONObject obj) throws JSONException {
String FullName = obj.getString("nama");
String Username = obj.getString("username");
String AnakKe = obj.getString("anakke");
return new Anak(FullName, Username, AnakKe);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.act_data_anak, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_addanak:
newDataAnak = item;
NewData();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void NewData() {
Intent i = new Intent(getActivity(), AddAnak.class);
startActivity(i);
}
}
这是我的自定义适配器
package com.app.amusa.library;
import com.app.amusa.R;
import com.app.amusa.library.*;
//import info.androidhive.customlistviewvolley.model.Movie;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ArrayAdapter;
public class FroListAnakAdapter extends ArrayAdapter<Anak> {
private List<Anak> itemList;
private Context context;
public FroListAnakAdapter(List<Anak> itemList, Context ctx) {
super(ctx, android.R.layout.simple_list_item_1, itemList);
this.itemList = itemList;
this.context = ctx;
}
public int getCount() {
if (itemList != null)
return itemList.size();
return 0;
}
public Anak getItem(int position) {
if (itemList != null)
return itemList.get(position);
return null;
}
public long getItemId(int position) {
if (itemList != null)
return itemList.get(position).hashCode();
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row_anak, null);
}
Anak c = itemList.get(position);
TextView text = (TextView) v.findViewById(R.id.FullName);
text.setText(c.getFullName());
TextView text1 = (TextView) v.findViewById(R.id.Username);
text1.setText(c.getUsername());
TextView text2 = (TextView) v.findViewById(R.id.AnakKe);
text2.setText(c.getAnakKe());
return v;
}
public List<Anak> getItemList() {
return itemList;
}
public void setItemList(List<Anak> itemList) {
this.itemList = itemList;
}
}
和这个可序列化的代码
package com.app.amusa.library;
import java.io.Serializable;
public class Anak implements Serializable {
private String FullName, thumbnailUrl,Username,AnakKe;
public Anak(String FullName,String Username, String AnakKe) {
this.FullName = FullName;
this.Username = Username;
this.AnakKe = AnakKe;
}
public String getFullName() {
return FullName;
}
public void setFullName(String FullName) {
this.FullName = FullName;
}
public String getthumbnailUrl() {
return thumbnailUrl;
}
public void setthumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getUsername() {
return Username;
}
public void setUsername(String Username) {
this.Username = Username;
}
public String getAnakKe() {
return AnakKe;
}
public void setAnakKe(String AnakKe) {
this.AnakKe= AnakKe;
}
}
和流光是我定制的json。 有人可以帮帮我吗? 感谢
答案 0 :(得分:0)
也许您没有正确地向适配器添加项目。尝试使用此方法添加
public class FroListAnakAdapter extends ArrayAdapter<Anak> {
...
public void setItemList(List<Anak> itemList) {
this.itemList = itemList;
for(Anak anak: itemList) {
this.add(anak);
}
/** OR **/
this.addAll(itemList);
}
}
答案 1 :(得分:0)
您的ListAnak必须扩展ListFragment&amp;您的布局必须包含带有android默认id属性的默认片段列表视图。我认为这一定对你有用。我需要进一步的帮助然后回复我。