上的Android活动结果总是返回0和空意图 [英] Android on activity result always return 0 and null intent
问题描述
我试图从网上找到几乎所有的解决方案,仍然解决不了我的问题。会有人请通过以下方式codeS帮助吗?我真的不能使它发挥作用。即使我试图把的setResult code在onBack pressed事件,父活动仍然得到结果,code = 0。
- 在一个微调的触摸事件父[CreatePostActivity]开始为活动的结果:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
mContext =这一点;
APPSTATE =((ApplicationProvider)getApplicationContext()); LayoutInflater膨胀= LayoutInflater.from(本);
mContent = inflate.inflate(R.layout.activity_create_post,NULL);
的setContentView(mContent); spinnerCategory =(微调)mContent.findViewById(R.id.spinnerCategory);
spinnerCategory.setOnTouchListener(本); arrayCategory =新的String [] {getResources()的getString(R.string.create_post_category)};
adapterCategory =新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_item,arrayCategory);
spinnerCategory.setAdapter(adapterCategory);}
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){ 如果(v.getId()== R.id.spinnerCategory){
束束=新包();
bundle.putInt(类别ID的categoryId);
意向意图=新意图(这一点,CreatePostActivity_Category.class);
intent.putExtras(包);
// intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(意向,Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_ code);
}
返回true;
}
- 儿童[CreatePostActivity_Category]包含一个ListView和中包含适配器初始化:
listitem_select_checkbox.xml
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@色/白
机器人:填充=5DP> <的TextView
机器人:ID =@ + ID / textViewItem
风格=@风格/ Heading.h2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerVertical =真
机器人:文字=TextView的/> <复选框
机器人:ID =@ + ID / checkBoxItem
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_centerVertical =真/>< / RelativeLayout的>
CreatePostActivity接收:
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code == Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_ code){
如果(结果code == RESULT_OK){
的System.out.println(OK);
}其他
的System.out.println(CANCEL);
}
}
CreatePostActivity_Category:
公共类CategoryAdapter扩展BaseAdapter实现OnCheckedChangeListener {
私人LayoutInflater吹气= NULL; 公共CategoryAdapter(CategoryModel []列表){
吹气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @覆盖
公众诠释的getCount(){
返回mListItems.length;
} @覆盖
公共对象的getItem(INT位置){
返回的位置;
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看VI = convertView;
如果(convertView == NULL)
VI = inflater.inflate(R.layout.listitem_select_checkbox,NULL); TextView的textViewItem =(TextView中)vi.findViewById(R.id.textViewItem);
textViewItem.setText(mListItems [位置] .getDescription((字符串)appState.get preference(变量preF_LANG))); 复选框checkBoxItem =(复选框)vi.findViewById(R.id.checkBoxItem);
如果(==的categoryId mListItems [位置] .CategoryId)
checkBoxItem.setChecked(真);
checkBoxItem.setTag(R.id.tag_id,mListItems [位置] .CategoryId);
checkBoxItem.setTag(R.id.tag_desc,mListItems [位置] .getDescription((字符串)appState.get preference(变量preF_LANG)));
checkBoxItem.setOnCheckedChangeListener(本); 返回VI;
} @覆盖
公共无效onCheckedChanged(CompoundButton V,布尔器isChecked){
如果(器isChecked){
INT ID =的Integer.parseInt(((CompoundButton)ⅴ).getTag(R.id.tag_id)的ToString());
desc字符串=((CompoundButton)V).getTag(R.id.tag_desc)的ToString(); 束束=新包();
bundle.putInt(类别ID,身份证);
bundle.putString(类别,DESC);
意图mIntent =新的Intent();
mIntent.putExtras(包); 如果(的getParent()== NULL){
的setResult(Activity.RESULT_OK,mIntent);
}其他{
的getParent()的setResult(Activity.RESULT_OK,mIntent)。
}
完();
}
}
}@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); mContext =这一点;
APPSTATE =((ApplicationProvider)getApplicationContext());
数据库= DatabaseHelper.instance(); LayoutInflater膨胀= LayoutInflater.from(本);
mContent = inflate.inflate(R.layout.activity_create_post_category,NULL);
的setContentView(mContent); 的ImageButton ibtnClose =(的ImageButton)mContent.findViewById(R.id.ibtnClose);
ibtnClose.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
完();
}
}); listViewCategory =(ListView控件)mContent.findViewById(R.id.listViewCategory);
光标CR = database.select(SELECT CATEGORY_ID,edesc,cdesc FROM类别);
mListItems =新CategoryModel [cr.getCount()]; 如果(CR!= NULL){
如果(cr.moveToFirst()){
INT I = 0;
做{
INT的categoryId = cr.getInt(cr.getColumnIndex(CATEGORY_ID));
串cdesc = cr.getString(cr.getColumnIndex(cdesc));
串edesc = cr.getString(cr.getColumnIndex(edesc));
mListItems [I] =新CategoryModel(的categoryId,cdesc,edesc);
我++;
}而(cr.moveToNext());
}
}
cr.close(); CategoryAdapter mCategoryAdapter =新CategoryAdapter(mContext,mListItems);
listViewCategory.setAdapter(mCategoryAdapter); MDATA = this.getIntent()getExtras()。
的categoryId = mData.getInt(类别ID);
}
最后我想通了!
我写了一篇关于触摸监听器原来已经被解雇三次,这就是为什么它无法检索实际的返回结果。
这个解决它 - > 安卓设置微调onClickListener()
私人View.OnTouchListener Spinner_OnTouch =新View.OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
如果(event.getAction()== MotionEvent.ACTION_UP){ 如果(v.getId()== R.id.spinnerCategory){
束束=新包();
bundle.putInt(类别ID的categoryId);
意向意图=新意图(mContext,CreatePostActivity_Category.class);
intent.putExtras(包);
startActivityForResult(意向,Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_ code);
}
}
返回true;
}
};
一切正常。
感谢所有建议和正确的答案。
I tried almost all solutions found from the net and still can't solve my problem. Would anyone please help with the following codes? I really can't make it work. Even if I tried to put the setresult code in onBackPressed event, the parent activity still get result code = 0.
- Start activity for result in the parent [CreatePostActivity] on touch event of a spinner:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
appState = ((ApplicationProvider) getApplicationContext());
LayoutInflater inflate = LayoutInflater.from(this);
mContent = inflate.inflate(R.layout.activity_create_post, null);
setContentView(mContent);
spinnerCategory = (Spinner) mContent.findViewById(R.id.spinnerCategory);
spinnerCategory.setOnTouchListener(this);
arrayCategory = new String[] { getResources().getString(R.string.create_post_category) };
adapterCategory = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayCategory);
spinnerCategory.setAdapter(adapterCategory);}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.spinnerCategory) {
Bundle bundle = new Bundle();
bundle.putInt("CategoryId", categoryId);
Intent intent = new Intent(this, CreatePostActivity_Category.class);
intent.putExtras(bundle);
// intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_CODE);
}
return true;
}
- Child [CreatePostActivity_Category] contains a listview and initialize with adapter like below:
listitem_select_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:padding="5dp" >
<TextView
android:id="@+id/textViewItem"
style="@style/Heading.h2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="TextView" />
<CheckBox
android:id="@+id/checkBoxItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
CreatePostActivity to receive :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
System.out.println("OK");
} else
System.out.println("CANCEL");
}
}
CreatePostActivity_Category:
public class CategoryAdapter extends BaseAdapter implements OnCheckedChangeListener {
private LayoutInflater inflater = null;
public CategoryAdapter(CategoryModel[] list) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mListItems.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.listitem_select_checkbox, null);
TextView textViewItem = (TextView) vi.findViewById(R.id.textViewItem);
textViewItem.setText(mListItems[position].getDescription((String) appState.getPreference(Variables.PREF_LANG)));
CheckBox checkBoxItem = (CheckBox) vi.findViewById(R.id.checkBoxItem);
if (categoryId == mListItems[position].CategoryId)
checkBoxItem.setChecked(true);
checkBoxItem.setTag(R.id.tag_id, mListItems[position].CategoryId);
checkBoxItem.setTag(R.id.tag_desc, mListItems[position].getDescription((String) appState.getPreference(Variables.PREF_LANG)));
checkBoxItem.setOnCheckedChangeListener(this);
return vi;
}
@Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
if (isChecked) {
int id = Integer.parseInt(((CompoundButton) v).getTag(R.id.tag_id).toString());
String desc = ((CompoundButton) v).getTag(R.id.tag_desc).toString();
Bundle bundle = new Bundle();
bundle.putInt("CategoryId", id);
bundle.putString("Category", desc);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
if (getParent() == null) {
setResult(Activity.RESULT_OK, mIntent);
} else {
getParent().setResult(Activity.RESULT_OK, mIntent);
}
finish();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
appState = ((ApplicationProvider) getApplicationContext());
database = DatabaseHelper.instance();
LayoutInflater inflate = LayoutInflater.from(this);
mContent = inflate.inflate(R.layout.activity_create_post_category, null);
setContentView(mContent);
ImageButton ibtnClose = (ImageButton) mContent.findViewById(R.id.ibtnClose);
ibtnClose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
listViewCategory = (ListView) mContent.findViewById(R.id.listViewCategory);
Cursor cr = database.select("SELECT category_id, edesc, cdesc FROM category");
mListItems = new CategoryModel[cr.getCount()];
if (cr != null) {
if (cr.moveToFirst()) {
int i = 0;
do {
int categoryId = cr.getInt(cr.getColumnIndex("category_id"));
String cdesc = cr.getString(cr.getColumnIndex("cdesc"));
String edesc = cr.getString(cr.getColumnIndex("edesc"));
mListItems[i] = new CategoryModel(categoryId, cdesc, edesc);
i++;
} while (cr.moveToNext());
}
}
cr.close();
CategoryAdapter mCategoryAdapter = new CategoryAdapter(mContext, mListItems);
listViewCategory.setAdapter(mCategoryAdapter);
mData = this.getIntent().getExtras();
categoryId = mData.getInt("CategoryId");
}
Finally I figured it out!!!
The original on touch listener I wrote has been fired three times, that's why it was unable to retrieve the actual return result.
Solved it by this --> Android: setting a spinner onClickListener()
private View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (v.getId() == R.id.spinnerCategory) {
Bundle bundle = new Bundle();
bundle.putInt("CategoryId", categoryId);
Intent intent = new Intent(mContext, CreatePostActivity_Category.class);
intent.putExtras(bundle);
startActivityForResult(intent, Variables.CREATEPOST_CATEGORY_ACTIVITY_REQUEST_CODE);
}
}
return true;
}
};
Everything works. Thanks all for the suggested and correct answers.
这篇关于上的Android活动结果总是返回0和空意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!