2015-01-20 132 views
0

我剛剛在片段中用asynctask做了一個自定義列表視圖。我不知道問題在哪裏,因爲logcat永遠不會顯示錯誤。請有人能幫助我片段上的Listview沒有顯示

這是我的片段代碼

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

您是否設置了斷點以查看您是否實際加載了數據? – bryan 2015-01-20 20:40:24

回答

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); 
} 

}

0

你ListAnak必須擴展ListFragment &佈局必須包含與Android默認id屬性默認片段列表視圖中添加。我認爲這一定適合你。我需要進一步的幫助然後回覆我。