2013-03-03 76 views
0

我想在我的项目中实现以下内容。 来自活动A我想开始活动B的结果。 活动A有一个列表视图(比如mylistview)和为它设置的适配器。 (比如说myadapter)。 和相应的数组列表myylylist。填充列表视图与另一个活动的结果

与活动B中的结果字符串我想这个字符串添加到活动A.

我做的现有列表视图以下几件事:

在活性的

ArrayAdapter<String> myadapter = new ArrayAdapter<String>(this, android.r.layout.simple_list_item_1,myarraylist); 
mylistview.setListAdapter(myadapter); 

Intent i = new Intent(this, activityB.class); 
startAcivityForResult(i,1); 

现在在我的onActivityResult方法中:

String new_str = data.getStringExtra(); 
myarraylist.add(new_str); 
myadapter.notifyDatasetChanged(); 

和在活动B:

Intent i =new Intent(); 
i.putExtra("card_name", Card_name); 
setResult(RESULT_OK,i); 

我的程序没有显示任何错误,工作正常。 但仍然我的listview没有得到正确更新。

我在哪里出错?

+0

什么意思是没有正确更新?你的意思是有时它确实有时并不是? – Kanth 2013-03-03 07:14:14

+0

没有对不起,我陷害了它,它从来没有得到更新..... – user2056245 2013-03-03 07:22:45

+0

请看我编辑的问题.....我已经添加了我在活动B做的 – user2056245 2013-03-03 07:24:20

回答

0

我认为你应该尝试调试出了什么问题。字符串不正确读取或字符串永远不会到达列表视图。

如果未读取该字符串,请尝试将其发送给意图。如果它添加到数组中,但不添加到列表视图,我会尝试更新适配器,以便它直接从数组中读取。

像这样:

public class CustomStringAdapter extends ArrayAdapter<String> { 

    public CustomStringAdapter(Context context, int textViewResourceId, 
      List<String> objects) { 
     super(context, textViewResourceId, objects); 
    } 
    @Override 
    public int getCount() { 
     return myarraylist.size(); 
    } 

    @Override 
    public String getItem(int position) { 
     // TODO Auto-generated method stub 
     return myarraylist.get(position); 
    } 

} 

然后设置一个适配器来代替:

CustomStringAdapter myadapter = new CustomStringAdapter(this,android.r.layout.simple_list_item_1,myarraylist); 
mylistview.setListAdapter(myadapter); 

这可能不是答案,但它是值得进行测试。

+0

肯定,谢谢,我这样做 – user2056245 2013-03-03 07:44:13