2013-08-29 50 views
0

我嘗試了幾乎所有從網上找到的解決方案,仍然無法解決我的問題。任何人都可以請幫助下面的代碼?我真的無法讓它工作。即使我試圖把的setResult代碼onBackPressed事件,父活動仍然得到結果代碼= 0Android上的活動結果總是返回0和零意圖

  1. 的結果開始活動父[CreatePostActivity]在旋轉器上的觸摸事件:

@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; 
} 
  1. 兒童[CreatePostActivity_Category]包含一個列表視圖,並用適配器初始化象下面這樣:

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接收:

@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"); 
} 
+0

完成兒童活動的代碼在哪裏?共享代碼CreatePostActivity_Category –

+0

它在適配器中的getview方法內被註釋。 –

+0

爲什麼這是評論?您應該將整個列表器從適配器替換爲活動 –

回答

0

最後我想通了!

我寫的原始觸摸監聽器已被觸發三次,這就是爲什麼它無法檢索到實際返回結果。

這個解決它 - >Setting a spinner onClickListener() in Android

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

一切正常。 謝謝大家的建議和正確的答案。

1

通話結束()方法,在你的孩子的活動之前,添加以下代碼:

setResult(RESULT_OK); 

Fllowing是新的: 1,添加新的變量:

Context mContext; 

2,更改適配器的構造:在適配器

public CategoryAdapter(Context context,CategoryModel[] list) { 
    mContext = context; 
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

3,變更完成()到:

((Activity)mContext).finish(); 

4,將您的適配器定義替換爲您的父級活動在步驟2中提到的建設

+0

我試過了,但它不起作用。一切都以CANCEL形式返回。 –

+0

嘗試步驟1,2,3,4 @WinnieL – happyhls

+0

嘗試過,仍然返回0作爲結果代碼。 –