Android:adapter.notifydatasetchanged()
我想创建一个动态GridView,当我点击我的选项菜单时,我刷新我的asyncTask,然后我从postExecute获得所有结果,并通知我的适配器刷新!但我不工作请帮帮我Android:adapter.notifydatasetchanged(),android,adapter,notify,Android,Adapter,Notify,我想创建一个动态GridView,当我点击我的选项菜单时,我刷新我的asyncTask,然后我从postExecute获得所有结果,并通知我的适配器刷新!但我不工作请帮帮我 GridView grid; ImageAdapter adapter; ArrayList<String> arrayThumbs = new ArrayList<String>(); ArrayList<String> arrayBig_image = new ArrayList&l
GridView grid;
ImageAdapter adapter;
ArrayList<String> arrayThumbs = new ArrayList<String>();
ArrayList<String> arrayBig_image = new ArrayList<String>();
ArrayList<String> arrayAuthor = new ArrayList<String>();
ArrayList<String> arrayDescription = new ArrayList<String>();
ArrayList<String> arrayDate = new ArrayList<String>();
String[] arraymThumbs;
String[] arrayBimages;
String[] arrayauthor;
String[] arraydescription;
String[] arraydate;
final static String TAG_ITEM = "img_list";
final static String TAG_THUMBNAILS = "thumbnail";
final static String TAG_BIG_IMAGE = "big_image";
final static String TAG_AUTHOR = "author";
final static String TAG_DESCRIPTION = "description";
final static String TAG_DATE = "event_date";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
backTask();
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, ImageGallery.class);
intent.putExtra("position", position);
intent.putExtra("big_images", arrayBimages);
intent.putExtra("author", arrayauthor);
intent.putExtra("description", arraydescription);
intent.putExtra("date", arraydate);
startActivity(intent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.refresh:
backTask();
break;
}
return super.onOptionsItemSelected(item);
}
public void backTask(){
BackTask task = new BackTask();
task.execute();
try {
JSONObject result = task.get();
if(result != null){
JSONArray jarray = result.getJSONArray(TAG_ITEM);
for(int i = 0; i < jarray.length(); i++){
JSONObject jrss = jarray.getJSONObject(i);
arrayThumbs.add(jrss.getString(TAG_THUMBNAILS));
arrayBig_image.add(jrss.getString(TAG_BIG_IMAGE));
arrayAuthor.add(jrss.getString(TAG_AUTHOR));
arrayDescription.add(jrss.getString(TAG_DESCRIPTION));
arrayDate.add(jrss.getString(TAG_DATE));
Log.d("LLLLLOOOOGGGG", jrss.getString("author")+"\n");
}}
else{
AlertDialog.Builder alertNetworkError = new AlertDialog.Builder(this);
alertNetworkError.setMessage("нет подключения к интернету");
alertNetworkError.setNegativeButton("Выйти", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertNetworkError.create();
alertNetworkError.show();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
arraymThumbs = new String[arrayThumbs.size()];
arraymThumbs = arrayThumbs.toArray(arraymThumbs);
arrayBimages = new String[arrayBig_image.size()];
arrayBimages = arrayBig_image.toArray(arrayBimages);
arrayauthor = new String[arrayAuthor.size()];
arrayauthor = arrayAuthor.toArray(arrayauthor);
arraydescription = new String[arrayDescription.size()];
arraydescription = arrayDescription.toArray(arraydescription);
arraydate = new String[arrayDate.size()];
arraydate = arrayDate.toArray(arraydate);
adapter = new ImageAdapter(MainActivity.this, arraymThumbs);
adapter.notifyDataSetChanged();
}
这是我的任务
public class BackTask extends AsyncTask<String, Void, JSONObject>{
@Override
protected JSONObject doInBackground(String... params) {
InputStream ips = null;
JSONObject jsonObj = null;
String json = "";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(new HttpPost("http://192.168.1.179/lenta/image.js"));
ips = response.getEntity().getContent();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader bufff = new BufferedReader(new InputStreamReader(ips, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = bufff.readLine()) != null){
sb.append(line + "\n");
}
ips.close();
json = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObj;
}
@Override
protected void onPostExecute(JSONObject result){
super.onPostExecute(result);
}
public类BackTask扩展了AsyncTask{
@凌驾
受保护的JSONObject doInBackground(字符串…参数){
InputStream ips=null;
JSONObject jsonObj=null;
字符串json=“”;
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpResponse response=httpClient.execute(新建HttpPost(“http://192.168.1.179/lenta/image.js"));
ips=response.getEntity().getContent();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader bufff=新的BufferedReader(新的InputStreamReader(ips,“UTF-8”);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=buff.readLine())!=null){
sb.追加(第+行“\n”);
}
ips.close();
json=sb.toString();
}捕获(例外e){
e、 printStackTrace();
}
试一试{
jsonObj=新的JSONObject(json);
}捕获(JSONException e){
e、 printStackTrace();
}
返回jsonObj;
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
super.onPostExecute(结果);
}
}
在这里,我从url处理json,在MainActivity中,我在这一行后面得到这个表单task.get()Addgrid.setAdapter(adapter)
adapter = new ImageAdapter(MainActivity.this, arraymThumbs);
希望这能解决你的问题
编辑:另外,您似乎需要阅读一些内容,因为我认为您不完全了解什么是适配器以及如何使用它。请使用以下内容:
adapter = new ImageAdapter(YourActivity.this, arraymThumbs);
myGridView.setAdapter(adapter);
希望这对你有帮助
谢谢。请发布您的
适配器的代码。如果这是您的全部代码,那么它必须是null
。好的,还有一件事:您在主代码中创建适配器的位置?我在>公共类MainActivity extends Activity之后创建它{我只是猜测一下,但是您调用task.get()时可能没有从BackTask获得任何数据因为它还没有完成。但当然,我不确定,因为我不知道你的BackTask是什么。所以我要说检查一下。@03uk请发布你的活动的完整代码,包括适配器构造函数调用。我添加了这个,但它不希望我这样做(
adapter = new ImageAdapter(YourActivity.this, arraymThumbs);
myGridView.setAdapter(adapter);