嘿,我有一个使用此适配器的listfragment:
public class F1_fr extends ListFragment {
View rootview;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootview=inflater.inflate(R.layout.f1_lay,container,false);
rootview.findViewById(R.id.semi_transparent).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(),MainActivity2.class);
startActivityForResult(intent, 2);
}
});
return rootview;
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
aa.add(message);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final String[] items = getResources().getStringArray(R.array.heroes);
ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, items);
setListAdapter(aa);
}
}
以及我从编辑文本中获得结果的活动:
public class ajout extends ActionBarActivity {
EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ajout);
editText1=(EditText)findViewById(R.id.editText1);
}
public void btn (View view)
{
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();
}
它解决但现在我有这个错误:
Failure delivering result ResultInfo{who=null, request=131074, result=2, data=Intent { (has extras) }}
答案 0 :(得分:0)
变量aa
在当前范围内不可用。这应该是您的问题的解决方案:
public class F1_fr extends ListFragment {
View rootview;
ArrayAdapter<String> aa;
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 2)
{
String message=data.getStringExtra("MESSAGE");
aa.add(message);
}
else
{
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final String[] items = getResources().getStringArray(R.array.heroes);
aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, items);
setListAdapter(aa);
}
}