2016-03-23 43 views
0

我有以下使用ListView顯示主題列表的Activity。每一行都有一個文本視圖,該視圖被設置爲紅色以指示該主題尚未回答的問題。當用戶點擊一行時,它應該啓動一個活動,詢問有關該主題的問題。StartActivityForResult更新ListView中的一行

我想通過調用startActivityForResult來啓動Activity,這將更新與問題已被問到的事實並將文本視圖變爲綠色的行。

我的問題是如何從onActivityResult更新列表視圖中的特定行?

public class QuestionListForInTransaction extends ActionBarActivity { 

    ListView listView; 


     public void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 

      setContentView(R.layout.questionlistforintxlayout); 
      getSupportActionBar().setDisplayShowHomeEnabled(true); 
      getSupportActionBar().setIcon(R.drawable.ic_launcher); 
      setTitle("Call Question(s)"); 

      listView = (ListView)findViewById(R.id.txinquestionlist); 

      String[] values = new String[] { "Are you the driver?", "Question 2", "Question 3", }; 


      MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values); 
      listView.setAdapter(adapter); 
      listView.setOnItemClickListener(listener); 




      }//end of onCreate 




      public class MySimpleArrayAdapter extends ArrayAdapter<String> { 
       private final Context context; 
       private final String[] values; 

       public MySimpleArrayAdapter(Context context, String[] values) { 
       super(context, R.layout.questionlisttxinrowlayout, values); 
       this.context = context; 
       this.values = values; 
       } 

       @Override 
       public View getView(int position, View convertView, ViewGroup parent) { 

       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View rowView = inflater.inflate(R.layout.questionlisttxinrowlayout, parent, false); 

       TextView question = (TextView) rowView.findViewById(R.id.tvquestionforintx); 
       question.setText(values[position]); 

       TextView answered = (TextView) rowView.findViewById(R.id.rowstatusbox); 
       answered.setBackgroundColor(Color.RED); 

       String questionStr = values[position]; 
       rowView.setTag(questionStr); 


       return rowView; 

       } 




      } //end of adapter class 



      OnItemClickListener listener = new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       //Toast.makeText(QuestionListForInTransaction.this, "pos = " + position, Toast.LENGTH_LONG).show(); 

       String question = (String)view.getTag(); 

       if(question.equalsIgnoreCase("Are you the driver?")){ 

        //Toast.makeText(QuestionListForInTransaction.this, "Are you the driver?" + position, Toast.LENGTH_LONG).show(); 

        final int QUESTION_REQUEST = 1; 
        Intent questionIntent = new Intent(QuestionListForInTransaction.this, Question.class); 
        startActivityForResult(questionIntent, QUESTION_REQUEST); 

        //TextView answered = (TextView) view.findViewById(R.id.rowstatusbox); 
        //answered.setBackgroundColor(Color.GREEN); 

       } 


      } 
     }; 


     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent intent){ 
      super.onActivityResult(requestCode, resultCode, intent); 
      Bundle extras = intent.getExtras(); 
      if(extras != null) 
      int result = extras.getInt("result"); 

      //*********************update a row green********************** 
     } 

    } //end of class 
+0

您可以創建自定義類,並有兩個值String和boolean,並在獲得結果時設置布爾值false。然後通知適配器。 – Keshav

回答

2

只是創建了一些自定義包裝類,像這樣:

public class QuestionWrapper { 
private String mQuestionText; 
private boolean mIsAsked; 

public QuestionWrapper(String defaultQuestionText, boolean isAsked) { 
    mQuestionText = defaultQuestionText; 
    mIsAsked = isAsked; 
} 

public boolean isAsked() { 
    return mIsAsked; 
} 

public void setIsAswked(boolean isAsked) { 
    mIsAsked = isAsked; 
} 

public String getQuestionText() { 
    return mQuestionText; 
} 

}

,那麼你需要設置這個包裝的清單適配器:

public class QuestionListForInTransaction extends ActionBarActivity { 
    List<QuestionWrapper> mWrappers = new ArrayList<>(); 
    MySimpleArrayAdapter mAdapter; 

... 。

public void onCreate(Bundle icicle) { 
    .... 
    wrappers.add(new QuestionWrapper("Are you the driver?",false)); 
    wrappers.add(new QuestionWrapper("Question 2",false)); 
    wrappers.add(new QuestionWrapper("Question 3",false)); 

    mAdapter = new MySimpleArrayAdapter(this, wrappers); 
    .... 

和更新您的適配器是這樣的:

public class MySimpleArrayAdapter extends ArrayAdapter<QuestionWrapper> { 
    private final Context context; 
    private final List<QuestionWrapper> mWrappers; 

    public MySimpleArrayAdapter(Context context, List<QuestionWrapper> wrappers) { 
     super(context, R.layout.questionlisttxinrowlayout, wrappers); 
     this.context = context; 
     this.mWrappers = wrappers; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.questionlisttxinrowlayout, parent, false); 
     QuestionWrapper wrapper = mWrappers.get(position); 
     TextView question = (TextView) rowView.findViewById(R.id.tvquestionforintx); 
     question.setText(wrapper.getQuestionText()); 

     TextView answered = (TextView) rowView.findViewById(R.id.rowstatusbox); 
     if (wrapper.isAsked()) { 
      answered.setBackgroundColor(Color.GREEN); 
     } else { 
      answered.setBackgroundColor(Color.RED); 
     } 

     rowView.setTag(wrapper.getQuestionText()); 

     return rowView; 

    } 

onActivityResult - 需要更新您的包裝類和adpater:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent){ 
    super.onActivityResult(requestCode, resultCode, intent); 
    .... 
    mWrappersList.get(yourPosition).setAsked(true/false); 
    if (mAdapter!=null){ 
    mAdapter.notifyDataSetChanged(); 
    } 
    ... 
} 

我認爲RequestCode在startActivityForResult您可以使用適配器點擊位置。

+0

嘿感謝您花時間做到這一點,感激!然而,我得到1錯誤,「不能在明確調用構造函數時引用實例字段mWrappers」,這是適配器構造器超級部分中的mWrappers變量。 – turtleboy

+0

http://pastebin.com/2CxR1XnU爲你帶來更好的樣品 –

+0

我剛剛重新命名了outter類中的mWapper。非常感謝你的幫助 :) – turtleboy