P
P
paleniy2017-07-04 01:08:44
Java
paleniy, 2017-07-04 01:08:44

Why is GridView not populated with new data after notifyDataSetChanged in thread?

Good afternoon!
Calling notifyDataSetChanged() on a Thread does not update data in cells. There are no errors.
If you call directly in Oncreate, then the data is updated.
Tell me, what's the problem?

code
public class MainActivity extends AppCompatActivity {
    public GridView Grid;
   public String[] data = {"0","1","2","3","4","5","6","7","8"};
    public Button ButtonGo;
  public  TextViewAdapter myTextViewAdapter;
    public SquareTextView textView;
    public View gridView;

...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       ButtonGo = (Button) findViewById(R.id.ButtonGo);
        Grid = (GridView) findViewById(R.id.GridView);
       myTextViewAdapter = new TextViewAdapter(this, data);
        Grid.setAdapter(myTextViewAdapter);
        Grid.setOnItemClickListener(gridViewOnItemClickListener);
....

public void TimerStart(final int cellposition,final long celltime,final long celltimer) {
    final Thread ThreadTimer = new Thread() {
        public void run() {
            try {
                while (System.currentTimeMillis() < celltime + celltimer) {
                    Thread.sleep(100);
                }
          if ((System.currentTimeMillis() >= celltime + celltimer)){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                data[cellposition]="123";
                                myTextViewAdapter.notifyDataSetChanged();
                            }
                        });      }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    final Handler handlerTimer = new Handler();
    handlerTimer.post(new Runnable() {
        @Override
        public void run() {
            ThreadTimer.start();
        }
    });
    }
public void ClickGo(View view) {
        ButtonGo.setVisibility(View.INVISIBLE);
TimerStart(1,System.currentTimeMillis(),10000);
    }

...

    public class TextViewAdapter extends BaseAdapter {
        private Context context;
        private final String[] textViewValues;

        public TextViewAdapter(Context context, String[] textViewValues) {
            this.context = context;
            this.textViewValues = textViewValues;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                gridView = inflater.inflate(R.layout.item, null);
                textView = (SquareTextView) gridView.findViewById(R.id.cellTextView);
                textView.setText(textViewValues[position]);
            } else {
                gridView = (View) convertView;
            }
            return gridView;
        }

        @Override
        public int getCount() {
            return textViewValues.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question