我使用自定义适配器将图像和文本显示到列表视图,当我第一次运行活动时它显示正常,但是当我导航到另一个活动并更改值时,我回到我的列表视图活动它崩溃了。它在Log中给出了以下错误 -
(07-03 08:53:15.456: E/AndroidRuntime(574): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification.
Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class stevey.sadc.system101.MobileArrayAdapter)])
这是我的代码:
public class System101Activity extends ListActivity implements OnClickListener {
public static final String S101 = null;
private Button btnLogout,btnSignin,btnRegister;
private TextView txtSharedName;
public static final ArrayList<String> BookImages = new ArrayList<String>();
public static final ArrayList<String> BookNames = new ArrayList<String>();
public static boolean LogedIn = true;
final Context context = this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.storemain);
this.btnLogout = (Button) findViewById(R.id.btnSignout);
this.btnRegister = (Button) findViewById(R.id.btnSignUp);
this.btnSignin = (Button) findViewById(R.id.btnSignIn);
this.btnRegister.setOnClickListener(this);
this.btnSignin.setOnClickListener(this);
this.btnLogout.setOnClickListener(this);
//txtSharedName = (TextView) findViewById(R.id.textviewLogName);
///////////////////////////New List//////////////////////////////////////////
/*if(LogedIn){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String sharedUsername = settings .getString("Sharedusername","null");
txtSharedName.setText(sharedUsername);
}else{
this.btnLogout.setClickable(false);
}*/
Log.d("Logged IN", " "+LogedIn);
JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/prrrrrrrrr.php");
// http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
BookNames.clear();
BookImages.clear();
try{
JSONArray earthquakes = json.getJSONArray("books");
for(int i=0;i<earthquakes.length();i++){
JSONObject e = earthquakes.getJSONObject(i);
String BookName = e.getString("bookname");
BookNames.add(BookName);
String BookImg = e.getString("imageurl");
BookImages.add(BookImg);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
setListAdapter(new MobileArrayAdapter(this, BookNames,BookImages));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
Intent moreDetailsIntent = new Intent(System101Activity.this,MoreBookDetails.class);
Bundle dataBundle = new Bundle();
dataBundle.putString("SelectedBook", selectedValue);
moreDetailsIntent.putExtras(dataBundle);
startActivity(moreDetailsIntent);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnSignUp:
Intent signupIntent = new Intent(this,RegisterActivity.class);
startActivity(signupIntent);
break;
case R.id.btnSignIn:
Intent signinIntent = new Intent(this,SignInActivity.class);
startActivity(signinIntent);
break;
case R.id.btnSignout:
///////////////////////////// Creating Alert Dialog/////////////////////////
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
//set title
alertDialogBuilder.setTitle("LogOut");
//set dialog message
alertDialogBuilder
.setMessage("Are You Sure!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
//LogedIn = false;
LogedIn = false;
Intent tabIntent = new Intent(System101Activity.this,MyTabs.class);
startActivity(tabIntent);
//Sign.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
////////////////////////////////////////////////////////////////////////////
}
}
}
这是我的适配器。
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] myBookNamelist = null;
private ArrayList<String> MyBooks = new ArrayList<String>();
private ArrayList<String> myBookImageurl = new ArrayList<String>();
//ArrayList<HashMap<String, String>> myBookNamelist = new ArrayList<HashMap<String, String>>();
//ArrayList<HashMap<String, String>> myBookImageurl = new ArrayList<HashMap<String, String>>();
public MobileArrayAdapter(Context context,ArrayList<String> Bname,ArrayList<String> BUrl) {
super(context, R.layout.mylist, Bname);
this.context = context;
this.MyBooks = Bname;
this.myBookImageurl = BUrl;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.mylist, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(MyBooks.get(position));
Bitmap bitmap = null;
// Change icon based on name
JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/prrrrrrrrr.php");
// http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
try{
JSONArray earthquakes = json.getJSONArray("books");
//for(position=0;position<earthquakes.length();position++){
JSONObject e = earthquakes.getJSONObject(position);
String BB = e.getString("bookname");
MyBooks.add(BB);
String UU = e.getString("imageurl");
myBookImageurl.add(UU);
//}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
String s = MyBooks.get(position);
String i = myBookImageurl.get(position);
System.out.println(s);
System.out.println(i);
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(i).getContent());
imageView.setImageBitmap(bitmap);
} catch (MalformedURLException err) {
// TODO Auto-generated catch block
err.printStackTrace();
} catch (IOException err) {
// TODO Auto-generated catch block
err.printStackTrace();
}
System.out.println("Bitmap image: "+position+"="+bitmap);
return rowView;
}
}
答案 0 :(得分:0)
从您的第二项活动中,当您的数据集发生变化时,发送广播通知。
在您的主要活动中,注册广播接收器以侦听这些更改,并调用mListViewAdapter.notifyDataSetChanged()以重新加载列表视图。