Java 片段的编辑文本不';单击按钮时无法刷新
我已经成功地访问了数据库和其他片段的单选按钮或buttonclick,但是当我尝试设置EditText的文本时,当前片段没有得到刷新。我尝试过无效、后验证和refreshDrawableState,但没有成功。有什么建议吗Java 片段的编辑文本不';单击按钮时无法刷新,java,android,button,android-fragments,Java,Android,Button,Android Fragments,我已经成功地访问了数据库和其他片段的单选按钮或buttonclick,但是当我尝试设置EditText的文本时,当前片段没有得到刷新。我尝试过无效、后验证和refreshDrawableState,但没有成功。有什么建议吗 public static class Tsiatisto extends Fragment { public Tsiatisto() {} @Override public View onCreateView(LayoutInflater inflater,
public static class Tsiatisto extends Fragment
{
public Tsiatisto() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.tsiatisto, container, false);
mViews.add(inflater.inflate(R.layout.tsiatisto, container));
DataBaseHelper myDbHelper = new DataBaseHelper(this.getActivity().getApplicationContext());
try
{
myDbHelper.createDataBase();
myDbHelper.openDataBase();
}catch(IOException ioe)
{
throw new Error("Unable to create database");
}
final SQLiteDatabase db = myDbHelper.getDB();
//Cursor cursor = db.query("Tsiatista ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
final View button = rootView.findViewById(R.id.buttonGo);
button.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v)
{
View fs;
RadioGroup rg;
int selectedId;
RadioButton rb;
fs = (View) mViews.get(0);
rg = (RadioGroup) fs.findViewById(R.id.radioGroupFS);
selectedId = rg.getCheckedRadioButtonId();
rb = (RadioButton) fs.findViewById(selectedId);
funnySerious = (String) rb.getText();
fs = (View) mViews.get(1);
rg = (RadioGroup) fs.findViewById(R.id.radioGroupMF);
selectedId = rg.getCheckedRadioButtonId();
rb = (RadioButton) fs.findViewById(selectedId);
malefemale = (String) rb.getText();
fs = (View) mViews.get(2);
rg = (RadioGroup) fs.findViewById(R.id.radioGroupDC);
selectedId = rg.getCheckedRadioButtonId();
rb = (RadioButton) fs.findViewById(selectedId);
dirtyclean = (String) rb.getText();
Cursor cursor = db.rawQuery("SELECT Verse FROM Tsiatista WHERE FunnySerious = '" + funnySerious + "' AND (MaleFemale = '" + malefemale + "' OR MaleFemale = 'Both')", null);
if(cursor.moveToFirst())
{
String verse = (String) cursor.getString(cursor.getColumnIndex("Verse"));
EditText et = (EditText) mViews.get(3).findViewById(R.id.tsiatistoTxt);
et.setText(verse);
cursor.close();
View vv = mViews.get(3);
//vv.();
//vv.postInvalidate();
//vv.refreshDrawableState();
//View ve = (View) mViews.get(3).findViewById(R.id.tsiatistoTxt);
//ve.removeAllViews();
//ve.refreshDrawableState();
}
}
}
);
return rootView;
}
}
我建议使用
getView()
方法调用,而不是get(index)代码>我不确定您是否获得了对UI元素的正确引用。示例EditText myEditText=(EditText)getView().findViewById(R.id.myEditText)
我还建议将所有这些代码移出onCreateView()
方法,并将其移到onActivityCreated()或onViewCreated()中。我相信onCreateView方法是用来进行最简单的UI膨胀的,在这种方法中引用元素可能会非常费力,而且不完全可靠。