我正在創建一個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(「」);而繼續重置值。
有什麼建議嗎?
非常有用的評論,謝謝!我可以簡單地刪除setText(「」);並從一開始就將每個單元格清空,從而解決了問題。 –