我有一个main activity
首先加载了listview
,并且从这个主要活动开始我正在启动一个新的活动,我试图获取edittext
值通过intent
我的主要活动。一切都运行正常但是当我将editext值添加到listview时它会添加但是当我转到second activity
并再次添加并返回到主活动时,之前添加的数据不再被看到并且新添加的值正在更新列表视图。
任何人都可以帮我解决这个问题吗?
这是我的适配器:
public class AdapterListViewData extends BaseAdapter{
private LayoutInflater mInflater;
private Context context;
private ArrayList<DataShow> listData = new ArrayList<DataShow>();
public AdapterListViewData(Context context,ArrayList<DataShow> listData) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
this.listData = listData;
}
public int getCount() {
// TODO Auto-generated method stub
return listData.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
HolderListAdapter holderListAdapter;
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.adapter_listview, null);
holderListAdapter = new HolderListAdapter();
holderListAdapter.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
holderListAdapter.txtDetail = (TextView) convertView.findViewById(R.id.txtDetail);
holderListAdapter.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holderListAdapter);
}else{
holderListAdapter = (HolderListAdapter) convertView.getTag();
}
holderListAdapter.txtTitle.setText(listData.get(position).getTitle());
holderListAdapter.txtDetail.setText(listData.get(position).getDetail());
holderListAdapter.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context,"CheckBox "+ position +" check!!",Toast.LENGTH_SHORT).show();
}
});
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context,"List "+ position +" click!!",Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
MainActivity:
public class CustomListViewActivity extends Activity {
private AdapterListViewData adapterListViewData;
private ArrayList<DataShow> listData = new ArrayList<DataShow>();
private ListView listViewData;
String AddedTask ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
listViewData = (ListView)findViewById(R.id.listViewData);
if (intent.hasExtra("NewTask")) {
AddedTask = this.getIntent().getExtras().getString("NewTask");
listData.add(new DataShow(AddedTask,""));
}
adapterListViewData = new AdapterListViewData(getBaseContext(),listData);
listViewData.setAdapter(adapterListViewData);
adapterListViewData.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_add_task:
Intent i = new Intent(CustomListViewActivity.this, AddTask.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
DataShow:
public class DataShow {
private String title;
private String detail;
public DataShow(String title,String detail) {
// TODO Auto-generated constructor stub
this.title = title;
this.detail = detail;
}
public String getTitle(){
return this.title;
}
public String getDetail(){
return this.detail;
}
}
持有人:
public class HolderListAdapter {
TextView txtTitle;
TextView txtDetail;
CheckBox checkBox;
}
第二项活动:
public class AddTask extends Activity {
Button addtask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
// get action bar
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
addtask = (Button) findViewById(R.id.btnaddlist);
findViewById(R.id.btnaddlist).setOnClickListener(
new View.OnClickListener() {
public void onClick(View arg0) {
EditText edit = (EditText) findViewById(R.id.tskname);
Intent i = new Intent(AddTask.this,
CustomListViewActivity.class);
//Bundle bundle = new Bundle();
String TaskName = edit.getText().toString();
//bundle.putString("NewTask", TaskName);
i.putExtra("NewTask", TaskName);
//i.putExtras(bundle);
startActivity(i);
}
});
}
}
答案 0 :(得分:0)
我相信你应该在
中打电话给adapterListViewData.notifyDataSetChanged();
if (intent.hasExtra("NewTask"))
{
AddedTask = this.getIntent().getExtras().getString("NewTask");
listData.add(new DataShow(AddedTask,""));
}
即,
if (intent.hasExtra("NewTask"))
{
AddedTask = this.getIntent().getExtras().getString("NewTask");
listData.add(new DataShow(AddedTask,"")); // you are adding new data here
adapterListViewData.notifyDataSetChanged(); // just refresh the adapter here
}
出于调试目的,请检查NewTask
中MainActivity.class
获得的值。
Log.d("track",AddedTask);
DataShow类构造函数需要标题和详细信息。你有没有检查它是否增加了价值&amp;你错过了空的细节吗?
listData.add(new DataShow(AddedTask,""));
尝试提供静态数据。
还有一件事,在添加从secondActivity发回的新数据时,检查您的adapter
是空还是不,
if (adapterListViewData != null) {
AddedTask = this.getIntent().getExtras().getString("NewTask");
listData.add(new DataShow(AddedTask,""));
adapterListViewData.notifyDataSetChanged();
}
else
{
adapterListViewData = new AdapterListViewData(getBaseContext(),listData);
listViewData.setAdapter(adapterListViewData);
}
答案 1 :(得分:0)
您每次都在创建adapterListViewData
的新实例
调用onCreate,这就是之前数据被清除的原因。
在onCreate中添加一个检查以防止这种情况发生。
if ( adapterListViewData == null )
{
adapterListViewData = new AdapterListViewData(getBaseContext(),listData);
listViewData.setAdapter(adapterListViewData);
}
答案 2 :(得分:0)
您的旧数据已被删除,因为它未传输到您在创建新项目后开始的新活动。
您应该使用startActivityForResult
创建新任务,并在adapterListViewData
中调用将新任务添加到列表onActivityResult
并在其中调用notifyDataSetChanged
。