2017-02-19 3 views
0

스도쿠 응용 프로그램을 만들고 있지만 문제가 있습니다. 그리드의 특정 요소 만 숨기려고하는데 getView()를 사용하여 그렇게 할 수 있지만 언제든지 EditText를 사용하여 값을 변경하고 키보드를 아래로 이동하면 getView()가 다시 호출되어 입력을 지 웁니다. 그래서 주위를 둘러 보려고합니다. 이 작업을 수행하는 방법은 단순히 요소를 반복하고 onCreate()에서 특정 요소를 숨기는 것입니다. 뷰의 자식을 가져 오는 데는 문제가 있습니다. getChildAt (i)를 사용할 수 있습니다. 이는 뷰 자체가 아닌 데이터 만 반환하기 때문에 가시성을 변경할 수 없습니다. 나는 또한 이전 Stackoverflow 대답에서 getChildAt() 솔루션을 사용하여 시도했지만 작동하지 않는 것 같습니다. Heres는GridView 자식 요소를 통한 안드로이드 루프

내 코드 :

MainActivity :

이 CustomGridAdapter
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); 
      } 
     } 

    } 


} 

: 기본적으로

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; } 


    } 

, 나는 단지 원하는 스도쿠에 대한 그리드의 특정 요소를 숨기려면,하지만 난 돈 ' t.setText (""); 때문에 값을 다시 계속 유지하므로 getView()에서이 작업을 수행 할 수 있다고 생각하지 않습니다.

제안 사항?

답변

1

귀하의 접근 방식은 완전히 잘못되었습니다. getView 동안을 제외하고는 GridView에서 뷰를 절대 변경해서는 안됩니다. 해당 어댑터 및 보유한 데이터를 통해 GridView를 조작합니다. 예를 들어, 문자열 목록에 의해 지원되는 어댑터가 있고 다른 모든 문자열을 숨기고 싶다고 가정 해 보겠습니다. 그런 다음 두 개의 선택 항목을 사용하여 다른 모든 문자열 만 보유하도록 목록을 변경하거나 문자열 및 가시성을 유지하기 위해 보유한 데이터를 변경하고 getView에서 두 가지를 모두 수행하십시오.

GridView (또는 ListView 또는 RecyclerView)의 전체 점은 데이터와 일치하도록 재활용되는 데이터 백업보기입니다. 자신이 직접 뷰를 조작하려고하면 심하게 나빠집니다. 그렇게하지 마십시오. 데이터를 조작 한 다음 뷰를 업데이트하도록 지시하십시오.

+0

매우 유용한 메모입니다. 감사합니다. setText (""); 문제를 해결 한 시작부터 각 셀을 비 웁니다. –