2016-03-23 20 views
0

'daki bir satırı güncelleştirmek için tıklatınSorunların listesini görüntülemek için bir ListView kullanan aşağıdaki Etkinliğim'im. Her satırın, o konuyla ilgili soruya henüz cevap verilmediğini belirtmek için kırmızıya ayarlanmış bir metin görünümü vardır. Kullanıcı bir satırı tıkladığında, o konuyla ilgili bir soru soran bir Etkinlik başlatmalıdır.StartActivityForResult ListView

StartActivityForResult öğesini çağırarak Etkinliği başlatmak istiyorum, bu, soruyu sorulan gerçekle güncelleştirecek ve metin görünümünü yeşile dönüştürecektir.

Sorunuz, liste görünümündeki belirli bir satırı onActivityResult öğesinden nasıl güncelleyeceğinizdir?

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

Özel sınıf oluşturabilir ve iki değer Dize ve boole yapabilir ve sonucu aldığınızda boole yanlış ayarını yapabilirsiniz. Ardından adaptörü bilgilendirin. – Keshav

cevap

2

Bunun gibi bazı özel sarıcı sınıf oluşturun:

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

}

sonra Adaptöre Giden bu sarmalayıcıların Listesini ayarlamanız gerekir:

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); 
    .... 
Böyle

ve Güncelleyin Adaptör: -: Sana Adaptör tıklama konumunu kullanabilirsiniz startActivityForResult içinde RequestCode için düşünüyorum

@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(); 
    } 
    ... 
} 

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 Need sarıcı sınıf ve adpater güncellemek için.

+0

hey, bunu yapmak için zaman ayırdığınız için teşekkür ederiz! Ancak 1 hata alıyorum, "Bir kurucu açık bir şekilde çağırırken bir örnek alanı mWrappers başvuruda bulunamaz" bu, bağdaştırıcı yapılandırıcısının süper bölümündeki mWrappers değişkeni içindir. – turtleboy

+0

http://pastebin.com/2CxR1XnU sizin için daha iyi bir örnek al –

+0

Ben sadece mWapper outter sınıfında yeniden adlandırıldı. Yardımın için çok teşekkürler :) – turtleboy