2017-02-19 109 views
0

我正在創建一個Sudoku應用程序,但有一個問題 - 我試圖隱藏網格上的某些元素,而且我可以使用getView(),但是隨時使用EditText改變一個值並將鍵盤向下移動,getView()被再次調用並且擦除輸入,所以我試圖找到一種解決方法。我相信這樣做的方法是簡單地遍歷元素並隱藏onCreate()中的某些元素,儘管我在獲取視圖的任何子元素時遇到了問題。我可以使用getChildAt(i),儘管這隻返回數據,而不是視圖本身,所以我無法更改可見性。我也嘗試使用getChildAt()解決方案從以前的Stackoverflow答案,但它似乎無法正常工作。Android循環通過GridView子元素

繼承人我的代碼:

MainActivity:

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.EditText; 
import android.widget.GridView; 
import android.widget.TextView; 

import static java.lang.Boolean.FALSE; 
import static java.lang.Boolean.TRUE; 

public class MainActivity extends AppCompatActivity { 

    public GridView gridView; 
    public String[] items = new String[]{ 
      "5","3","4","6","7","8","9","1","2", 
      "6","7","2","1","9","5","3","4","8", 
      "1","9","8","3","4","2","5", "6","7", 
      "8","5","9","7","6","1","4","2","3", 
      "4","2","6","8","5","3","7","9","1", 
      "7","1","3","9","2", "4","8","5","6", 
      "9","6","1","5","3","7","2","8","4", 
      "2","8","7","4","1","9","6","3","5", 
      "3","4","5", "2","8","6","1","7","9" 
    }; 

    public Boolean[] masker = new Boolean[]{ 
      TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, 
      TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, 
      FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, 
      TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, 
      TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, 
      TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, 
      FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, 
      FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, 
      FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     gridView = (GridView) this.findViewById(R.id.myGridView); 
     CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items, masker); 
     gridView.setAdapter(gridAdapter); 

     final int size = gridAdapter.getCount(); 
     for(int i = 0; i < size; i++) { 
      ViewGroup gridChild = (ViewGroup) gridView.getChildAt(i); 
      int childSize = gridChild.getChildCount(); 
      for(int k = 0; k < childSize; k++) { 
       gridChild.getChildAt(k); 
      } 
     } 

    } 


} 

CustomGridAdapter:

import android.content.Context; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.view.KeyEvent; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.view.inputmethod.EditorInfo; 
    import android.view.inputmethod.InputMethodManager; 
    import android.widget.BaseAdapter; 
    import android.widget.EditText; 
    import android.widget.TextView; 

    import static java.lang.Boolean.FALSE; 

    public class CustomGridAdapter extends BaseAdapter { 


      private Context context; 
      private String[] items; 
      private Boolean[] masker; 
      private String input; 
      private EditText t; 
      LayoutInflater inflater; 

      public CustomGridAdapter(Context context, String[] items, Boolean[] masker){ 
       this.context = context; 
       this.items = items; 
       this.masker = masker; 

       inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 

      public View getView(int position, View convertView, ViewGroup parent){ 
       if(convertView == null){ 
        convertView = inflater.inflate(R.layout.cell, null); 
       } 

       t = (EditText) convertView.findViewById(R.id.editCell); 

       if(masker[position]){ 
        t.setText(items[position]); 
        t.setEnabled(FALSE); 
       }else{ 
        t.setText(""); 
       } 

       if((position % 9 != 0) && (position % 3 == 0)){ 
        t.setBackgroundResource(R.drawable.columnborder); 
       } 
       if((position >= 18 && position <= 26) || (position >= 45 && position <= 53)){ 
        t.setBackgroundResource(R.drawable.rowborder); 
       } 
       if((position == 21 || position == 24 || position == 48 || position == 51)){ 
        t.setBackgroundResource(R.drawable.cornerborder); 
       } 

       return convertView; 
      } 

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

      @Override 
      public Object getItem(int position) { return items[position]; } 

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


    } 

從本質上講,我只希望要隱藏的網格數獨的某些元素,但我不」我認爲我可以在getView()中做到這一點,因爲它會因爲t.setText(「」);而繼續重置值。

有什麼建議嗎?

回答

1

你的方法是完全錯誤的。除了getView期間,您絕對不應該改變GridView中的視圖。您可以通過它的適配器和它所保存的數據來操作GridView。例如,假設您有一個由一系列字符串支持的適配器,並且您希望隱藏其他所有字符串。 YOu會有兩個選擇 - 然後更改列表以僅保存每一個其他字符串,或者更改它保存的數據以同時保存字符串和可見性,並在getView中同時遵守這兩個選項。

GridView(或ListView或RecyclerView)的整個點是它的數據支持視圖,它將回收以匹配數據。如果你開始試圖自己操縱視圖,那就很糟糕。不要這樣做 - 操縱數據,然後告訴視圖自行更新。

+0

非常有用的評論,謝謝!我可以簡單地刪除setText(「」);並從一開始就將每個單元格清空,從而解決了問題。 –