我有以下使用ListView显示主题列表的Activity。每行都有一个文本视图,该视图设置为红色,表示尚未回答该主题的问题。当用户点击一行时,它应该启动一个活动,询问有关该主题的问题。
我想通过调用startActivityForResult启动Activity,这将更新行,询问问题并将textview变为绿色。
我的问题是如何从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 :(得分: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;
}
}
然后你需要将这个包装器的List设置为Adapter:
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();
}
...
}
我认为对于startActivityForResult中的RequestCode,您可以使用适配器点击位置。