这个问题纠结我很久了,那位大虾要是看到了,帮我解决一下,谢谢了问题出在public void onTabActivityResult(int requestCode, int resultCode, Intent data)
这个方法里,在这个方法里调用
adapter = new ContactMainAdapter(AddNewActivity.this, list);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
这样死活不更新,而不在这个方法里调用addDataSource()这个方法,是可以正常显示数据刷新的,我在里面这样调用这个方法也没用
下面这个是TabHost的那个Activity一段代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 获取当前活动的Activity实例
@SuppressWarnings("deprecation")
Activity subActivity = manager.getCurrentActivity();
//判断是否实现返回值接口
if (subActivity instanceof OnTabActivityResultListener) {
//获取返回值接口实例
OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;
//转发请求到子Activity
listener.onTabActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
public interface OnTabActivityResultListener {
public void onTabActivityResult(int requestCode, int resultCode, Intent data);
}
package com.vzo.eightpartycall.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.vzo.eightpartycall.R;
import com.vzo.eightpartycall.activity.MainActivity.OnTabActivityResultListener;
import com.vzo.eightpartycall.adapter.ContactMainAdapter;
import com.vzo.eightpartycall.bean.ContactBean;
public class AddNewActivity extends Activity implements OnClickListener,OnTabActivityResultListener {
private static final String TAG = AddNewActivity.class.getSimpleName();
private TextView tv_minute;
private Button bt_cacel, bt_add, bt_softKey, add_contact;
private ListView listView;
private ImageButton ib_sms, ib_call, ib_collet;
private RelativeLayout relayout;
private LinearLayout llayoutButton;
private LinearLayout topLayout;
private Dialog smsDialog;
private View smsView;
private EditText ed_title, ed_month, ed_day, ed_hour;
private EditText ed_emcee, ed_bafanghao, ed_passWord;
private Button bt_ensure, bt_cancel;
private ViewStub stub;
private List<ContactBean> list = new ArrayList<ContactBean>();
private ContactMainAdapter adapter;
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
addDataSource();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.i(TAG,"onCreate");
setContentView(R.layout.addnew);
initView();
// addDataSource();
}
private void initView() {
tv_minute = (TextView) findViewById(R.id.avariable_minute);
bt_cacel = (Button) findViewById(R.id.topCancelButton);
bt_cacel.setOnClickListener(this);
bt_add = (Button) findViewById(R.id.topAddButton);
bt_add.setOnClickListener(this);
bt_softKey = (Button) findViewById(R.id.topSoftButton);
bt_softKey.setOnClickListener(this);
ib_sms = (ImageButton) findViewById(R.id.smsImageButton);
ib_sms.setOnClickListener(this);
ib_call = (ImageButton) findViewById(R.id.callImageButton);
ib_call.setOnClickListener(this);
ib_collet = (ImageButton) findViewById(R.id.collectImageButton);
ib_collet.setOnClickListener(this);
listView = (ListView) findViewById(R.id.listView);
relayout = (RelativeLayout) findViewById(R.id.relayout);
add_contact = (Button) findViewById(R.id.addContact);
add_contact.setOnClickListener(this);
llayoutButton = (LinearLayout) findViewById(R.id.llayoutButton);
topLayout = (LinearLayout) findViewById(R.id.toplayout);
}
private void addDataSource(){
Log.i("lanjianlong","addDataSource");
for (int i = 0; i < 10; i++) {
ContactBean cb = new ContactBean();
cb.setDisplayName("name"+i);
cb.setPhoneNum("123456"+i);
list.add(cb);
}
adapter = new ContactMainAdapter(AddNewActivity.this, list);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if(adapter.isButtonGone)
adapter.isButtonGone = false;
else
adapter.isButtonGone = true;
adapter.notifyDataSetChanged();
return false;
}
});
relayout.setVisibility(View.GONE);
// llayoutButton.setVisibility(View.GONE);
// add_contact.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = null;
switch (v.getId()) {
case R.id.topCancelButton:
if(list != null && adapter != null){
list.clear();
adapter.setDateList(list);
adapter.notifyDataSetChanged();
}
break;
case R.id.topAddButton:
intent = new Intent();
intent.putExtra("where", "AddNewActivity");
intent.setClass(AddNewActivity.this, InviteFriendActivity.class);
getParent().startActivityForResult(intent, 1);
break;
case R.id.topSoftButton:
getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
break;
case R.id.smsImageButton:
smsView = LayoutInflater.from(AddNewActivity.this).inflate(R.layout.sms_model, null);
// smsView.setVisibility(View.VISIBLE);
// listView.setVisibility(View.GONE);
// relayout.setVisibility(View.GONE);
// topLayout.setVisibility(View.GONE);
// stub = (ViewStub) findViewById(R.id.smsview);
// stub.inflate();
ed_title = (EditText) smsView.findViewById(R.id.ed_title);
ed_month = (EditText) smsView.findViewById(R.id.ed_month);
ed_day = (EditText) smsView.findViewById(R.id.ed_day);
ed_hour = (EditText) smsView.findViewById(R.id.ed_hour);
ed_emcee = (EditText) smsView.findViewById(R.id.ed_emcee);
ed_bafanghao = (EditText) smsView.findViewById(R.id.ed_bafanghao);
ed_passWord = (EditText) smsView.findViewById(R.id.ed_password);
bt_ensure = (Button) smsView.findViewById(R.id.bt_ensure);
bt_ensure.setOnClickListener(this);
bt_cancel = (Button) smsView.findViewById(R.id.bt_cancel);
bt_cancel.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(AddNewActivity.this);
builder.setView(smsView);
smsDialog = builder.create();
smsDialog.show();
break;
case R.id.callImageButton:
intent = new Intent();
intent.putExtra("where", "AddNewActivity");
intent.setClass(AddNewActivity.this, CallPhoneActivity.class);
startActivity(intent);
break;
case R.id.collectImageButton:
break;
case R.id.addContact:
intent = new Intent();
intent.putExtra("where", "AddNewActivity");
intent.setClass(AddNewActivity.this, InviteFriendActivity.class);
getParent().startActivityForResult(intent, 1);
break;
case R.id.bt_ensure:
// smsView.setVisibility(View.GONE);
// topLayout.setVisibility(View.VISIBLE);
smsDialog.dismiss();
listView.setVisibility(View.VISIBLE);
Toast.makeText(AddNewActivity.this, "ensure is onClick", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_cancel:
// smsView.setVisibility(View.GONE);
// Log.i("lanjianlong","list, listView = "+listView);
// listView.setVisibility(View.VISIBLE);
// topLayout.setVisibility(View.VISIBLE);
smsDialog.dismiss();
Toast.makeText(AddNewActivity.this, "cnacel is onClick", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
public void onTabActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.i("lanjianlong","AddNewActivity, onTabActivityResult(), data = "+data);
Log.i("lanjianlong","AddNewActivity, onTabActivityResult, requestCode = "+requestCode+", resultCode = "+resultCode+", Activity.RESULT_OK="+Activity.RESULT_OK);
addDataSource();//这个方法不在这里调用是可以显示数据的,在此调用,怎么都没反应,这个方法是放在这里测试的
//下面的是另一个Activity返回的数据,数据可以返回,数据有打印出来
if(requestCode == 1){
if(data == null)
return;
Bundle bd = data.getExtras();
list.clear();
List<ContactBean> tempList = (List<ContactBean>) bd.getSerializable("lists");
ContactBean cb = null;
for (int i = 0; i < tempList.size(); i++) {
cb = new ContactBean();
cb.setDisplayName(tempList.get(i).getDisplayName());
cb.setPhoneNum(tempList.get(i).getPhoneNum());
list.add(cb);
Log.i("lanjianlong","AddNewActivity, onTabActivityResult, name, phone = "+tempList.get(i).getDisplayName()+", "
+tempList.get(i).getPhoneNum()+", list.size() = "+list.size());
}
Log.i("lanjianlong","onTabActivityResult, tempList = "+tempList);
if(list.size() > 0){
listView = null;
listView = (ListView) findViewById(R.id.listView);
relayout.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
adapter = new ContactMainAdapter(AddNewActivity.this, list);//在此重新new 或者不new 效果都是一样的,数据就是不更新
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Log.i("lanjianlong", "onTabActivityResult, list.size() = "+list.size());
}else{
relayout.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
Log.i("lanjianlong", "onTabActivityResult, else");
}
}
// super.onActivityResult(requestCode, resultCode, data);
}
}
adapter = new ContactMainAdapter(AddNewActivity.this, list);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
这样死活不更新,而不在这个方法里调用addDataSource()这个方法,是可以正常显示数据刷新的,我在里面这样调用这个方法也没用
下面这个是TabHost的那个Activity一段代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 获取当前活动的Activity实例
@SuppressWarnings("deprecation")
Activity subActivity = manager.getCurrentActivity();
//判断是否实现返回值接口
if (subActivity instanceof OnTabActivityResultListener) {
//获取返回值接口实例
OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;
//转发请求到子Activity
listener.onTabActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
public interface OnTabActivityResultListener {
public void onTabActivityResult(int requestCode, int resultCode, Intent data);
}
package com.vzo.eightpartycall.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.vzo.eightpartycall.R;
import com.vzo.eightpartycall.activity.MainActivity.OnTabActivityResultListener;
import com.vzo.eightpartycall.adapter.ContactMainAdapter;
import com.vzo.eightpartycall.bean.ContactBean;
public class AddNewActivity extends Activity implements OnClickListener,OnTabActivityResultListener {
private static final String TAG = AddNewActivity.class.getSimpleName();
private TextView tv_minute;
private Button bt_cacel, bt_add, bt_softKey, add_contact;
private ListView listView;
private ImageButton ib_sms, ib_call, ib_collet;
private RelativeLayout relayout;
private LinearLayout llayoutButton;
private LinearLayout topLayout;
private Dialog smsDialog;
private View smsView;
private EditText ed_title, ed_month, ed_day, ed_hour;
private EditText ed_emcee, ed_bafanghao, ed_passWord;
private Button bt_ensure, bt_cancel;
private ViewStub stub;
private List<ContactBean> list = new ArrayList<ContactBean>();
private ContactMainAdapter adapter;
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
addDataSource();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.i(TAG,"onCreate");
setContentView(R.layout.addnew);
initView();
// addDataSource();
}
private void initView() {
tv_minute = (TextView) findViewById(R.id.avariable_minute);
bt_cacel = (Button) findViewById(R.id.topCancelButton);
bt_cacel.setOnClickListener(this);
bt_add = (Button) findViewById(R.id.topAddButton);
bt_add.setOnClickListener(this);
bt_softKey = (Button) findViewById(R.id.topSoftButton);
bt_softKey.setOnClickListener(this);
ib_sms = (ImageButton) findViewById(R.id.smsImageButton);
ib_sms.setOnClickListener(this);
ib_call = (ImageButton) findViewById(R.id.callImageButton);
ib_call.setOnClickListener(this);
ib_collet = (ImageButton) findViewById(R.id.collectImageButton);
ib_collet.setOnClickListener(this);
listView = (ListView) findViewById(R.id.listView);
relayout = (RelativeLayout) findViewById(R.id.relayout);
add_contact = (Button) findViewById(R.id.addContact);
add_contact.setOnClickListener(this);
llayoutButton = (LinearLayout) findViewById(R.id.llayoutButton);
topLayout = (LinearLayout) findViewById(R.id.toplayout);
}
private void addDataSource(){
Log.i("lanjianlong","addDataSource");
for (int i = 0; i < 10; i++) {
ContactBean cb = new ContactBean();
cb.setDisplayName("name"+i);
cb.setPhoneNum("123456"+i);
list.add(cb);
}
adapter = new ContactMainAdapter(AddNewActivity.this, list);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if(adapter.isButtonGone)
adapter.isButtonGone = false;
else
adapter.isButtonGone = true;
adapter.notifyDataSetChanged();
return false;
}
});
relayout.setVisibility(View.GONE);
// llayoutButton.setVisibility(View.GONE);
// add_contact.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = null;
switch (v.getId()) {
case R.id.topCancelButton:
if(list != null && adapter != null){
list.clear();
adapter.setDateList(list);
adapter.notifyDataSetChanged();
}
break;
case R.id.topAddButton:
intent = new Intent();
intent.putExtra("where", "AddNewActivity");
intent.setClass(AddNewActivity.this, InviteFriendActivity.class);
getParent().startActivityForResult(intent, 1);
break;
case R.id.topSoftButton:
getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
break;
case R.id.smsImageButton:
smsView = LayoutInflater.from(AddNewActivity.this).inflate(R.layout.sms_model, null);
// smsView.setVisibility(View.VISIBLE);
// listView.setVisibility(View.GONE);
// relayout.setVisibility(View.GONE);
// topLayout.setVisibility(View.GONE);
// stub = (ViewStub) findViewById(R.id.smsview);
// stub.inflate();
ed_title = (EditText) smsView.findViewById(R.id.ed_title);
ed_month = (EditText) smsView.findViewById(R.id.ed_month);
ed_day = (EditText) smsView.findViewById(R.id.ed_day);
ed_hour = (EditText) smsView.findViewById(R.id.ed_hour);
ed_emcee = (EditText) smsView.findViewById(R.id.ed_emcee);
ed_bafanghao = (EditText) smsView.findViewById(R.id.ed_bafanghao);
ed_passWord = (EditText) smsView.findViewById(R.id.ed_password);
bt_ensure = (Button) smsView.findViewById(R.id.bt_ensure);
bt_ensure.setOnClickListener(this);
bt_cancel = (Button) smsView.findViewById(R.id.bt_cancel);
bt_cancel.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(AddNewActivity.this);
builder.setView(smsView);
smsDialog = builder.create();
smsDialog.show();
break;
case R.id.callImageButton:
intent = new Intent();
intent.putExtra("where", "AddNewActivity");
intent.setClass(AddNewActivity.this, CallPhoneActivity.class);
startActivity(intent);
break;
case R.id.collectImageButton:
break;
case R.id.addContact:
intent = new Intent();
intent.putExtra("where", "AddNewActivity");
intent.setClass(AddNewActivity.this, InviteFriendActivity.class);
getParent().startActivityForResult(intent, 1);
break;
case R.id.bt_ensure:
// smsView.setVisibility(View.GONE);
// topLayout.setVisibility(View.VISIBLE);
smsDialog.dismiss();
listView.setVisibility(View.VISIBLE);
Toast.makeText(AddNewActivity.this, "ensure is onClick", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_cancel:
// smsView.setVisibility(View.GONE);
// Log.i("lanjianlong","list, listView = "+listView);
// listView.setVisibility(View.VISIBLE);
// topLayout.setVisibility(View.VISIBLE);
smsDialog.dismiss();
Toast.makeText(AddNewActivity.this, "cnacel is onClick", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
public void onTabActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.i("lanjianlong","AddNewActivity, onTabActivityResult(), data = "+data);
Log.i("lanjianlong","AddNewActivity, onTabActivityResult, requestCode = "+requestCode+", resultCode = "+resultCode+", Activity.RESULT_OK="+Activity.RESULT_OK);
addDataSource();//这个方法不在这里调用是可以显示数据的,在此调用,怎么都没反应,这个方法是放在这里测试的
//下面的是另一个Activity返回的数据,数据可以返回,数据有打印出来
if(requestCode == 1){
if(data == null)
return;
Bundle bd = data.getExtras();
list.clear();
List<ContactBean> tempList = (List<ContactBean>) bd.getSerializable("lists");
ContactBean cb = null;
for (int i = 0; i < tempList.size(); i++) {
cb = new ContactBean();
cb.setDisplayName(tempList.get(i).getDisplayName());
cb.setPhoneNum(tempList.get(i).getPhoneNum());
list.add(cb);
Log.i("lanjianlong","AddNewActivity, onTabActivityResult, name, phone = "+tempList.get(i).getDisplayName()+", "
+tempList.get(i).getPhoneNum()+", list.size() = "+list.size());
}
Log.i("lanjianlong","onTabActivityResult, tempList = "+tempList);
if(list.size() > 0){
listView = null;
listView = (ListView) findViewById(R.id.listView);
relayout.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
adapter = new ContactMainAdapter(AddNewActivity.this, list);//在此重新new 或者不new 效果都是一样的,数据就是不更新
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Log.i("lanjianlong", "onTabActivityResult, list.size() = "+list.size());
}else{
relayout.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
Log.i("lanjianlong", "onTabActivityResult, else");
}
}
// super.onActivityResult(requestCode, resultCode, data);
}
}