notifyDataSetChange() 不通过 ArrayAdapter() 更新 DataGrid()

发布于 2024-12-01 06:43:23 字数 11987 浏览 0 评论 0原文

在下面的代码中有一个类型为 List '<'String'>' 的变量名为“stringList”,通过 ArrayAdapter 绑定到 GridView 视图。下面的代码运行一个 for 循环,该循环创建一个字符串并将其添加到“stringList”中。从 LogCat 输出中可以看出,当 OnCreate() 子例程结束并且填充 GridView 时,将触发 ArrayAdapter 的 getView() 函数。我想要的是 GridView 在运行 for 循环的每次迭代时一次填充一项。

我的研究表明,notifyDataSetObersver() 函数是完成此任务的正确工具。尽管 notifyDataSetObersver() 在 for 循环的每次迭代中都会触发,但尚不清楚它正在执行任何操作,并且在 onCreate() 子例程结束后我的 GridView 仍在填充。

如果有人有任何建议,我将非常感激。

*请注意,下面的代码(如注释中所示)依赖于粘贴在代码下方的两个标题为“main”和“data_grid”的布局资源。

代码:

package com.arrayadapterupdate2;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity {

    LinearLayout mainLinearLayout;
    LayoutInflater inflater;
    GridArrayAdapter gridAdapter;
    GridView gridView;
    List<String> stringList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
        stringList = new ArrayList<String>();
        inflater = LayoutInflater.from(this);
        //The Resource 'mainLinearLayout' is a Linear Layout in the set content view 'main'
        mainLinearLayout = (LinearLayout) this.findViewById(R.id.mainLinearLayout);
        //The Resource 'grid_view' is a layout containing an empty gridview
        gridView = (GridView) inflater.inflate(R.layout.grid_view,null);
        gridAdapter = new GridArrayAdapter(this, stringList, R.layout.grid_view);
        gridView.setAdapter(gridAdapter);
        System.out.println("main triggered");
        stringList.clear();
        int i;
        int j =10;
            for (i=0;i<=j-1;i=i+1){
            System.out.println("Loop: "+i);
            String string;
            String addedString = "String "+i;
            stringList.add(addedString); 
            gridAdapter.notifyDataSetChanged();
            }
         mainLinearLayout.addView(gridView);
    }


public class GridArrayAdapter extends ArrayAdapter implements OnClickListener {

    private Context context;
    private List<String> stringList;

    public GridArrayAdapter(Context context, List<String> stringList, int id) {
        super(context, id, stringList);
        this.context = context;
        this.stringList = stringList;

    }   
@Override

    public View getView(int position, View convertView, ViewGroup viewGroup) {
         System.out.println("Get View Triggered "+ position);
         String string = stringList.get(position);
         LinearLayout gridViewElement = new LinearLayout(context);
         TextView textView = new TextView(context);
         textView.setText(string);
         gridViewElement.addView(textView);
        if (convertView == null) { 
            convertView = (View)gridViewElement;
        }  
        return convertView;
    }

@Override 

public void notifyDataSetChanged(){

System.out.println("Notify Data Set Changed Triggered");
}

@Override 

public int getCount() {

    return stringList.size();
}

@Override 

public Object getItem(int position) {

    return stringList.get(position);
}

@Override 

public long getItemId(int position) {

    return position;
}

@Override

public void onClick(View arg0) {

}
}

}

LogCat 输出部分:

08-24 20:10:09.395: INFO/System.out(223): 主要触发

08-24 20:10:09.395:信息/System.out(223):循环:0

08-24 20:10:09.395: INFO/System.out(223):通知数据集更改已触发

08-24 20:10:09.395:INFO/System.out(223):循环:1

08-24 20:10:09.395:INFO/System.out( 223): 通知数据集更改已触发

08-24 20:10:09.395: INFO/System.out(223):循环:2

08-24 20:10:09.395:INFO/System.out(223):触发通知数据集更改

08-24 20:10:09.395:INFO/System.out( 223):循环:3

08-24 20:10:09.405: INFO/System.out(223):通知数据集更改已触发

08-24 20:10:09.405:INFO/System.out(223):循环:4

08-24 20:10:09.405:INFO/System.out( 223): 通知数据集更改已触发

08-24 20:10:09.405: INFO/System.out(223):循环:5

08-24 20:10:09.405:INFO/System.out(223):通知数据集更改已触发

08-24 20:10:09.405:INFO/System.out( 223):循环:6

08-24 20:10:09.405: INFO/System.out(223):通知数据集更改已触发

08-24 20:10:09.405:INFO/System.out(223):循环:7

08-24 20:10:09.405:INFO/System.out( 223): 通知数据集更改已触发

08-24 20:10:09.405: INFO/System.out(223):循环:8

08-24 20:10:09.405:INFO/System.out(223):通知数据集更改已触发

08-24 20:10:09.405:INFO/System.out( 223):循环:9

08-24 20:10:09.405: INFO/System.out(223):通知数据集更改已触发

08-24 20:10:09.415:INFO/System.out(223):创建时已结束

08-24 20:10:09.445:INFO/System.out( 223): 获取视图触发 0

08-24 20:10:09.495: INFO/System.out(223):获取视图已触发 0

08-24 20:10:09.515:INFO/System.out(223):获取视图已触发 1

08-24 20:10:09.515:INFO/System.out( 223): 获取视图触发 2

08-24 20:10:09.525: INFO/System.out(223):获取视图已触发 3

08-24 20:10:09.525:INFO/System.out(223):获取视图已触发 4

08-24 20:10:09.525:INFO/System.out( 223): 获取视图触发 5

08-24 20:10:09.537: INFO/System.out(223): 获取视图触发 6

08-24 20:10:09.537: INFO/System.out(223): 获取视图触发 7

08-24 20:10:09.545: INFO/System.out( 223): 获取视图触发 8

08-24 20:10:09.545: INFO/System.out(223):获取视图已触发 9

08-24 20:10:09.676:INFO/ActivityManager(51):显示的活动

com.arrayadapterupdate2/.Main:972 毫秒(总计 972 毫秒)


嘿 Kerubu 下面是我的尝试将您的建议与 LogCat 输出的一部分一起实施。正如您从 LogCat 中看到的,尽管调用了 notificationDataSetChanged(),但 getView() 既没有被 notifyDataSetChanged() 调用,也没有被 onCreate() 子例程的结束调用。

我非常感谢您的所有帮助,欢迎任何进一步的建议。

代码:

public class Main extends Activity {

    LinearLayout mainLinearLayout;
    LayoutInflater inflater;
    GridView gridView;
    List<String> stringList;
    Handler androidHandler;
    private GuiThreadMessageHandler guiThreadMsgHandler;
    private GridArrayAdapter gridAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            System.out.println("On Create Started");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        stringList = new ArrayList<String>();
        inflater = LayoutInflater.from(this);
        //The Resource 'mainLinearLayout' is a Linear Layout in the set content view 'main'
        mainLinearLayout = (LinearLayout) this.findViewById(R.id.mainLinearLayout);
        //The Resource 'grid_view' is a layout containing an empty gridview
        gridView = (GridView) inflater.inflate(R.layout.grid_view,null);
        gridAdapter = new GridArrayAdapter(this, stringList, R.layout.grid_view);
        gridView.setAdapter(gridAdapter);
        stringList.clear();
        guiThreadMsgHandler = new GuiThreadMessageHandler();
        new GridArrayPopulator().execute();
        mainLinearLayout.addView(gridView);
        System.out.println("On Create Concluded");
    }

    private class GridArrayPopulator extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params){
            /* Here do your loop and notify the Gui thread */
            int i;
            int j =10;
            Message msg;
            String addedString;
            for (i=0;i<=j-1;i=i+1){
                System.out.println("loop: "+i);
                addedString = "String "+i;
                stringList.add(addedString); 
                msg = Message.obtain(guiThreadMsgHandler);
                msg.sendToTarget();
        }
            return null;
  }
    }


public class GuiThreadMessageHandler extends Handler {
 public void handleMessage(Message m){
     System.out.println("Notify Data Set Changed Triggered ");
     gridAdapter.notifyDataSetChanged();
}
}

public class GridArrayAdapter extends ArrayAdapter implements OnClickListener {

    private Context context;
    private List<String> stringList;

    public GridArrayAdapter(Context context, List<String> stringList, int id) {
        super(context, id, stringList);
        this.context = context;
        this.stringList = stringList;

    }   
@Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
         System.out.println("Get View Triggered "+ position);
         String string = stringList.get(position);
         LinearLayout gridViewElement = new LinearLayout(context);
         TextView textView = new TextView(context);
         textView.setText(string);
         gridViewElement.addView(textView);
        if (convertView == null) { 
            convertView = (View)gridViewElement;
        }  
        return convertView;
    }

@Override 
public void notifyDataSetChanged(){
System.out.println("Notify Data Set Changed Triggered");
}

@Override 
public int getCount() {
    return stringList.size();
}

@Override 
public Object getItem(int position) {
    return stringList.get(position);
}

@Override 
public long getItemId(int position) {
    return position;
}

@Override
public void onClick(View arg0) {
}
}
}

LogCat:

08-25 17:41:12.872: INFO/System.out(705): On Create Started
08-25 17:41:12.972: INFO/System.out(705): On Create Concluded
08-25 17:41:13.111: WARN/IInputConnectionWrapper(31516): showStatusIcon on inactive InputConnection
08-25 17:41:13.191: INFO/ActivityManager(1357): Displayed activity com.arrayadapterupdate2/.Main: 934 ms (total 934 ms)
08-25 17:41:13.261: DEBUG/vending(1482): [2073] LocalAssetCache.updateOnePackage(): No local info for com.arrayadapterupdate2
08-25 17:41:13.261: INFO/System.out(705): loop: 0
08-25 17:41:13.261: INFO/ActivityManager(1357): Stopping service: com.android.vending/.util.WorkService
08-25 17:41:13.321: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.321: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.351: INFO/System.out(705): loop: 1
08-25 17:41:13.351: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.351: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.351: INFO/System.out(705): loop: 2
08-25 17:41:13.361: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.361: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.361: INFO/System.out(705): loop: 3
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.391: INFO/System.out(705): loop: 4
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.401: INFO/System.out(705): loop: 5
08-25 17:41:13.422: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.422: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.422: INFO/System.out(705): loop: 6
08-25 17:41:13.441: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.441: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.441: INFO/System.out(705): loop: 7
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.462: INFO/System.out(705): loop: 8
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.462: INFO/System.out(705): loop: 9
08-25 17:41:13.471: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.471: INFO/System.out(705): Notify Data Set Changed Triggered

In the code below have a variable of type List '<'String'>' named 'stringList' which is bound to a GridView view via and ArrayAdapter. The code below runs a for loop which creates a string and adds it to 'stringList'. As can be seen in the LogCat output the ArrayAdapter's getView() function is triggered when the OnCreate() sub routine concludes and the GridView is populated. What I want is for the GridView to populate one item at a time as each iteration of the for loop is run.

My research indicated that notifyDataSetObersver() function is the correct tool for accomplishing this. Although notifyDataSetObersver() is triggering in each iteration of the for loop it is not clear that it's doing anything, and my GridView is still getting populated after the onCreate() sub routine concludes.

If anyone has any suggestions I would be very appreciative.

*Please not the code below (as indicated in comments) relies on a the two layout resources titled "main" and "data_grid" pasted below the code.

Code:

package com.arrayadapterupdate2;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity {

    LinearLayout mainLinearLayout;
    LayoutInflater inflater;
    GridArrayAdapter gridAdapter;
    GridView gridView;
    List<String> stringList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
        stringList = new ArrayList<String>();
        inflater = LayoutInflater.from(this);
        //The Resource 'mainLinearLayout' is a Linear Layout in the set content view 'main'
        mainLinearLayout = (LinearLayout) this.findViewById(R.id.mainLinearLayout);
        //The Resource 'grid_view' is a layout containing an empty gridview
        gridView = (GridView) inflater.inflate(R.layout.grid_view,null);
        gridAdapter = new GridArrayAdapter(this, stringList, R.layout.grid_view);
        gridView.setAdapter(gridAdapter);
        System.out.println("main triggered");
        stringList.clear();
        int i;
        int j =10;
            for (i=0;i<=j-1;i=i+1){
            System.out.println("Loop: "+i);
            String string;
            String addedString = "String "+i;
            stringList.add(addedString); 
            gridAdapter.notifyDataSetChanged();
            }
         mainLinearLayout.addView(gridView);
    }


public class GridArrayAdapter extends ArrayAdapter implements OnClickListener {

    private Context context;
    private List<String> stringList;

    public GridArrayAdapter(Context context, List<String> stringList, int id) {
        super(context, id, stringList);
        this.context = context;
        this.stringList = stringList;

    }   
@Override

    public View getView(int position, View convertView, ViewGroup viewGroup) {
         System.out.println("Get View Triggered "+ position);
         String string = stringList.get(position);
         LinearLayout gridViewElement = new LinearLayout(context);
         TextView textView = new TextView(context);
         textView.setText(string);
         gridViewElement.addView(textView);
        if (convertView == null) { 
            convertView = (View)gridViewElement;
        }  
        return convertView;
    }

@Override 

public void notifyDataSetChanged(){

System.out.println("Notify Data Set Changed Triggered");
}

@Override 

public int getCount() {

    return stringList.size();
}

@Override 

public Object getItem(int position) {

    return stringList.get(position);
}

@Override 

public long getItemId(int position) {

    return position;
}

@Override

public void onClick(View arg0) {

}
}

}

Portion of LogCat Output:

08-24 20:10:09.395: INFO/System.out(223): main triggered

08-24 20:10:09.395: INFO/System.out(223): Loop: 0

08-24 20:10:09.395: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.395: INFO/System.out(223): Loop: 1

08-24 20:10:09.395: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.395: INFO/System.out(223): Loop: 2

08-24 20:10:09.395: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.395: INFO/System.out(223): Loop: 3

08-24 20:10:09.405: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.405: INFO/System.out(223): Loop: 4

08-24 20:10:09.405: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.405: INFO/System.out(223): Loop: 5

08-24 20:10:09.405: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.405: INFO/System.out(223): Loop: 6

08-24 20:10:09.405: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.405: INFO/System.out(223): Loop: 7

08-24 20:10:09.405: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.405: INFO/System.out(223): Loop: 8

08-24 20:10:09.405: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.405: INFO/System.out(223): Loop: 9

08-24 20:10:09.405: INFO/System.out(223): Notify Data Set Changed Triggered

08-24 20:10:09.415: INFO/System.out(223): On Create Concluded

08-24 20:10:09.445: INFO/System.out(223): Get View Triggered 0

08-24 20:10:09.495: INFO/System.out(223): Get View Triggered 0

08-24 20:10:09.515: INFO/System.out(223): Get View Triggered 1

08-24 20:10:09.515: INFO/System.out(223): Get View Triggered 2

08-24 20:10:09.525: INFO/System.out(223): Get View Triggered 3

08-24 20:10:09.525: INFO/System.out(223): Get View Triggered 4

08-24 20:10:09.525: INFO/System.out(223): Get View Triggered 5

08-24 20:10:09.537: INFO/System.out(223): Get View Triggered 6

08-24 20:10:09.537: INFO/System.out(223): Get View Triggered 7

08-24 20:10:09.545: INFO/System.out(223): Get View Triggered 8

08-24 20:10:09.545: INFO/System.out(223): Get View Triggered 9

08-24 20:10:09.676: INFO/ActivityManager(51): Displayed activity

com.arrayadapterupdate2/.Main: 972 ms (total 972 ms)


Hey Kerubu below is my attempt at implimenting your suggestion along with a portion of the LogCat output. As you can see from the LogCat although notifyDataSetChanged() is being called getView() is neither being called by notifyDataSetChanged() or by the conclusion of the onCreate() sub routine.

I greatly appreciate all your help, and any further advice would be welcome.

Code:

public class Main extends Activity {

    LinearLayout mainLinearLayout;
    LayoutInflater inflater;
    GridView gridView;
    List<String> stringList;
    Handler androidHandler;
    private GuiThreadMessageHandler guiThreadMsgHandler;
    private GridArrayAdapter gridAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            System.out.println("On Create Started");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        stringList = new ArrayList<String>();
        inflater = LayoutInflater.from(this);
        //The Resource 'mainLinearLayout' is a Linear Layout in the set content view 'main'
        mainLinearLayout = (LinearLayout) this.findViewById(R.id.mainLinearLayout);
        //The Resource 'grid_view' is a layout containing an empty gridview
        gridView = (GridView) inflater.inflate(R.layout.grid_view,null);
        gridAdapter = new GridArrayAdapter(this, stringList, R.layout.grid_view);
        gridView.setAdapter(gridAdapter);
        stringList.clear();
        guiThreadMsgHandler = new GuiThreadMessageHandler();
        new GridArrayPopulator().execute();
        mainLinearLayout.addView(gridView);
        System.out.println("On Create Concluded");
    }

    private class GridArrayPopulator extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params){
            /* Here do your loop and notify the Gui thread */
            int i;
            int j =10;
            Message msg;
            String addedString;
            for (i=0;i<=j-1;i=i+1){
                System.out.println("loop: "+i);
                addedString = "String "+i;
                stringList.add(addedString); 
                msg = Message.obtain(guiThreadMsgHandler);
                msg.sendToTarget();
        }
            return null;
  }
    }


public class GuiThreadMessageHandler extends Handler {
 public void handleMessage(Message m){
     System.out.println("Notify Data Set Changed Triggered ");
     gridAdapter.notifyDataSetChanged();
}
}

public class GridArrayAdapter extends ArrayAdapter implements OnClickListener {

    private Context context;
    private List<String> stringList;

    public GridArrayAdapter(Context context, List<String> stringList, int id) {
        super(context, id, stringList);
        this.context = context;
        this.stringList = stringList;

    }   
@Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
         System.out.println("Get View Triggered "+ position);
         String string = stringList.get(position);
         LinearLayout gridViewElement = new LinearLayout(context);
         TextView textView = new TextView(context);
         textView.setText(string);
         gridViewElement.addView(textView);
        if (convertView == null) { 
            convertView = (View)gridViewElement;
        }  
        return convertView;
    }

@Override 
public void notifyDataSetChanged(){
System.out.println("Notify Data Set Changed Triggered");
}

@Override 
public int getCount() {
    return stringList.size();
}

@Override 
public Object getItem(int position) {
    return stringList.get(position);
}

@Override 
public long getItemId(int position) {
    return position;
}

@Override
public void onClick(View arg0) {
}
}
}

LogCat:

08-25 17:41:12.872: INFO/System.out(705): On Create Started
08-25 17:41:12.972: INFO/System.out(705): On Create Concluded
08-25 17:41:13.111: WARN/IInputConnectionWrapper(31516): showStatusIcon on inactive InputConnection
08-25 17:41:13.191: INFO/ActivityManager(1357): Displayed activity com.arrayadapterupdate2/.Main: 934 ms (total 934 ms)
08-25 17:41:13.261: DEBUG/vending(1482): [2073] LocalAssetCache.updateOnePackage(): No local info for com.arrayadapterupdate2
08-25 17:41:13.261: INFO/System.out(705): loop: 0
08-25 17:41:13.261: INFO/ActivityManager(1357): Stopping service: com.android.vending/.util.WorkService
08-25 17:41:13.321: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.321: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.351: INFO/System.out(705): loop: 1
08-25 17:41:13.351: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.351: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.351: INFO/System.out(705): loop: 2
08-25 17:41:13.361: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.361: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.361: INFO/System.out(705): loop: 3
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.391: INFO/System.out(705): loop: 4
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.391: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.401: INFO/System.out(705): loop: 5
08-25 17:41:13.422: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.422: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.422: INFO/System.out(705): loop: 6
08-25 17:41:13.441: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.441: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.441: INFO/System.out(705): loop: 7
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.462: INFO/System.out(705): loop: 8
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.462: INFO/System.out(705): Notify Data Set Changed Triggered
08-25 17:41:13.462: INFO/System.out(705): loop: 9
08-25 17:41:13.471: INFO/System.out(705): Notify Data Set Changed Triggered 
08-25 17:41:13.471: INFO/System.out(705): Notify Data Set Changed Triggered

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-12-08 06:43:23

我认为以下内容可能会解释正在发生的事情:

onCreate() 在 GUI 线程中运行。您在 onCreate() 中循环,并在循环中将数据添加到 GridView。通过这样做,您实际上会占用 GUI 线程,直到完成循环。因此,Android 没有机会响应您对 notificationDataSetChanged() 的调用,即它无法“绘制”到屏幕,因为您的循环占用了 GUI 线程。

作为建议,我将创建一个新线程并将循环放入其中,并从那里调用 notificationDataSetchanged() (尽管请检查 Android 文档,该方法可以从 GUI 之外的线程安全地调用。
为了让事情变得简单,android 有 AsyncTask 类来帮助创建线程等等

编辑
我刚刚看了一些代码,我试图做与你类似的事情。看来只能从 GUI 线程调用 notificationDataSetchanged()。为了解决这个问题,我在 GUI 线程中创建了一个消息处理程序,它将调用 notificationDataSetchanged()。其他线程将通过向该消息处理程序发送消息来通知该消息处理程序。

I think the following might explain what is happening:

onCreate() runs in the GUI thread. You're looping in onCreate() and in the loop you are adding the data to the GridView. by doing this you are essentially hogging the GUI thread until you complete your loop. Android therefore does not get chance to respond to your calls to notifyDataSetChanged() i.e. it cannot 'paint' to the screen because you are hogging the GUI thread with your loop.

As a suggestion, I would create a new thread and put your loop in there and also call notifyDataSetchanged() from there also (though please check Android docs that this method can be safely called from a thread other than the GUI.
To make things easy, android has the AsyncTask class to help with thread creation etc.

Edit
I just had a look at some code where I was trying to do a similar thing as you. It appears that calling notifyDataSetchanged() might only be allowed from the GUI thread. To get round this, I created a Message Handler in the GUI thread which would call notifyDataSetchanged(). This Message handler would be notified from other threads by sending it a Message.

策马西风 2024-12-08 06:43:23

这大致就是我认为你应该做的。请注意,我没有包含用于设置 GridArray 的代码,但它与您在原始帖子中引用的内容相同。

    public class YourActivity extends Activity {
        private GuiThreadMessageHandler guiThreadMsgHandler;
        private GridArrayAdapter gridAdapter;

        onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                guiThreadMsgHandler = new GuiThreadMessageHandler();
                new GridArrayPopulator().execute();
        }

        private class GridArrayPopulator extends AsyncTask<Void,Void,Void>{

               @Override
               protected Void doInBackground(Void.. params){
                   /* Here do your loop and notify the Gui thread */
                    int i;
                    int j =10;
                    Message msg;
                    String addedString;
                    for (i=0;i<=j-1;i=i+1){
                        //System.out.println("Loop: "+i);
                        //String string;
                        addedString = "String "+i;
                        stringList.add(addedString); 
                        msg = Message.obtain(guiThreadMsgHandler);
                        msg.sendToTarget()
               }
         }

    }

public class GuiThreadMessageHandler extends Handler {
        public void handleMessage(Message m){
            gridAdapter.notifyDataSetChanged();

}

This is roughly what I think you should do. Please note I have not included code for setting up the GridArray but it's the same as what you quoted in your original post anyway.

    public class YourActivity extends Activity {
        private GuiThreadMessageHandler guiThreadMsgHandler;
        private GridArrayAdapter gridAdapter;

        onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                guiThreadMsgHandler = new GuiThreadMessageHandler();
                new GridArrayPopulator().execute();
        }

        private class GridArrayPopulator extends AsyncTask<Void,Void,Void>{

               @Override
               protected Void doInBackground(Void.. params){
                   /* Here do your loop and notify the Gui thread */
                    int i;
                    int j =10;
                    Message msg;
                    String addedString;
                    for (i=0;i<=j-1;i=i+1){
                        //System.out.println("Loop: "+i);
                        //String string;
                        addedString = "String "+i;
                        stringList.add(addedString); 
                        msg = Message.obtain(guiThreadMsgHandler);
                        msg.sendToTarget()
               }
         }

    }

public class GuiThreadMessageHandler extends Handler {
        public void handleMessage(Message m){
            gridAdapter.notifyDataSetChanged();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文