2014-09-05 34 views
1

我對此有很多研究,但沒有什麼能滿足我。每個人都給我一個解決方案,把adapter.notifyDataSetChanged()放在我已經完成的runOnUiThread位didi不給我解決方案。 我的代碼如下 公共類FragmentActivity3擴展片段{適配器的內容已經改變,但listview沒有收到通知

// Hashmap for ListView 
ArrayList<Item> imageArry = new ArrayList<Item>(); 
CustomQuoteAdapter adapter; 
String jsonstring; 
@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    View rootView=inflater.inflate(R.layout.activity_fragment_activity1, container,false); 
    jsonstring=JsonClass.readData(); 
    Log.d("JSon String ",jsonstring); 
    new GetQuotesInfo().execute(); 
    adapter = new CustomQuoteAdapter(getActivity(), R.layout.list, imageArry); 
    ListView dataList = (ListView) rootView. findViewById(R.id.list); 
    dataList.setAdapter(adapter); 
    return rootView; 
} 

//Async Task to load data 
    class GetQuotesInfo extends AsyncTask<Void, Void, Void> 
    { 
     int count; 
     @Override 
     protected Void doInBackground(Void... arg0) { 
      // TODO Auto-generated method stub 
      try { 
       JSONArray jsonarr=new JSONArray(Globals.jsonText); 
       count=jsonarr.length(); 
       for(int i=0;i<count;i++) 
       { 
        HashMap<String, String> quoteinfo = new HashMap<String, String>(); 
        String author=""; 
        Log.d("Json","Reading"); 
        JSONObject jsonobj=jsonarr.getJSONObject(i); 
        String name=jsonobj.getString("name"); 
        quoteinfo.put("name", name); 
        JSONArray jarr=jsonobj.getJSONArray("quotes"); 
        String[] myarr=new String[jarr.length()]; 
        String[] myarr1=new String[jarr.length()]; 
        for(int j=0;j<jarr.length();j++) 
        { 


         JSONObject jobj=jarr.getJSONObject(j); 
         author=jobj.getString("author"); 
         String text=jobj.getString("text"); 
         Log.d("Author ",author); 
         Log.d("Text ",text); 
         myarr[j]=text; 
         myarr1[j]=author; 
        } 

        imageArry.add(new Item(name,myarr1, myarr)); 

       } 

      } 
      catch(Exception ex) 
      { 

     } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      getActivity().runOnUiThread(new Runnable(){ 
       public void run() { 
        adapter.notifyDataSetChanged(); 
       } 
      }); 
     } 

    } 

}

我的適配器類是低於

public class CustomQuoteAdapter extends ArrayAdapter<Item> { 
    Context context; 
    int layoutResourceId; 
    LinearLayout linearMain; 
    ArrayList<Item> data = new ArrayList<Item>(); 

    public CustomQuoteAdapter(Context context, int layoutResourceId, 
      ArrayList<Item> data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 

     if (row == null) { 
      LayoutInflater inflater = ((FragmentActivity) context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      linearMain = (LinearLayout) row.findViewById(R.id.lineraMain); 
      Item myImage = data.get(position); 
      TextView txtview=new TextView(context); 
      String heading=myImage.heading; 
      txtview.setText(heading); 
      txtview.setTextColor(Color.BLUE); 
      txtview.setTextSize(20); 
      linearMain.addView(txtview); 

      for (int j = 0; j < myImage.getName().length; j++) { 
       TextView label1 = new TextView(context); 
       label1.setText(myImage.author[j]); 
       linearMain.addView(label1); 
       TextView label2 = new TextView(context); 
       label2.setText(myImage.name[j]); 
       linearMain.addView(label2); 
      } 
//   ImageView image = new ImageView(context); 
//   int outImage = myImage.image; 
//   image.setImageResource(outImage); 
//   linearMain.addView(image); 
     } 

     return row; 

    } 

} 

誰能給我解決方案來解決我的錯誤

+1

從您不需要的asynctask中移除runOnUIthread,並在您的json中使用ex.printStackTrace()。無效的json可能是您應該避免在onPostExecute中使用notifyDataSetChanged的原因 – 2014-09-05 06:29:00

+0

。傾向於創建專用於更新適配器的回調方法。在某些情況下,getActivity()在使用它時可以爲null。 – 2014-09-05 07:53:56

回答

0

onPostExecute()被自動調用UI線程,所以你不需要runOnUIThread()

你檢查,以確保您的列表中的數據實際上正在發生變化?此外,我沒有通過你的整個適配器的邏輯,但這條線看起來像有一個錯字。我猜你的IDE會抓住它,如果這個錯字也不在你的資源文件中,雖然 linearMain = (LinearLayout) row.findViewById(R.id.lineraMain); 不知道如果這個錯字R.id.lineraMain是造成你的問題。

相關問題