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;
}
}
谁能给我解决方案来解决我的错误
从您不需要的asynctask中移除runOnUIthread,并在您的json中使用ex.printStackTrace()。无效的json可能是您应该避免在onPostExecute中使用notifyDataSetChanged的原因 – 2014-09-05 06:29:00
。倾向于创建专用于更新适配器的回调方法。在某些情况下,getActivity()在使用它时可以为null。 – 2014-09-05 07:53:56