这是我的代码的缩写版本。
public class MainActivity extends ActionBarActivity {
private ArrayList<String> entry = new ArrayList<String>();
private String[] entryString = new String[11];
private ArrayAdapter<String> aa;
private ListView entryListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
entryListView = (ListView) findViewById(R.id.listView1);
int listID = R.layout.entry_layout;
aa = new EntryAdapter(this, listID, entry);
entryListView.setAdapter(aa);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_add: {
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivityForResult(intent, 1);
break;
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int rc, int resc, Intent i) {
super.onActivityResult(rc, resc, i);
entryString = i.getStringArrayExtra("Entry");
this.onNewItemAdded(entryString);
}
public void onNewItemAdded(String[] _entry){
String key = new String();
key = _entry[0];
entry.add(key);
aa.notifyDataSetChanged(); //Runs without crashing when this is removed.
}
适配器类是,
@SuppressLint("Instantiatable")
public class EntryAdapter extends ArrayAdapter<String>{
private ArrayList<String> entries;
int count;
int resource;
public EntryAdapter(Context _context, int _resource, List<String> _entries){
//TODO Auto-generated constructor stub
super(_context, _resource, R.id.key, _entries);
this.resource = _resource;
this.entries = new ArrayList<String>(_entries);
this.count = 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (convertView == null) {
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(inflater);
row = li.inflate(resource, parent, false);
}
TextView keyView = (TextView) row.findViewById(R.id.key);
TextView siteView = (TextView) row.findViewById(R.id.site);
for(int c=0; c<11; c++){
while(!entries.get(c).toString().equals("")){
count++;
}
}
keyView.setText(entries.get(position));
siteView.setText(count + "sources");
return row;
}
}
通过在MainActivity中注释aa.notifyDataSetChanged()调用,应用程序运行时不会崩溃但会抛出
IndexOutOfBoundsException:索引0无效,大小为0
没有评论时。
PS:XML非常好。
编辑:
这是日志
03-18 18:38:03.840:E / AndroidRuntime(6052):致命异常:主要 03-18 18:38:03.840:E / AndroidRuntime(6052):进程:com.FirstClass.alert,PID:6052 03-18 18:38:03.840:E / AndroidRuntime(6052):java.lang.IndexOutOfBoundsException:索引0无效,大小为0 03-18 18:38:03.840:E / AndroidRuntime(6052):at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 03-18 18:38:03.840:E / AndroidRuntime(6052):at java.util.ArrayList.get(ArrayList.java:308) 03-18 18:38:03.840:E / AndroidRuntime(6052):at com.FirstClass.netalert.EntryAdapter.getView(EntryAdapter.java:66) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.AbsListView.obtainView(AbsListView.java:2263) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.ListView.makeAndAddView(ListView.java:1790) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.ListView.fillDown(ListView.java:691) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.ListView.fillFromTop(ListView.java:752) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.ListView.layoutChildren(ListView.java:1630) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.AbsListView.onLayout(AbsListView.java:2091) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.View.layout(View.java:14822) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.View.layout(View.java:14822) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.View.layout(View.java:14822) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.View.layout(View.java:14822) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.View.layout(View.java:14822) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.View.layout(View.java:14822) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.widget.FrameLayout.onLayout(FrameLayout.java:388) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.View.layout(View.java:14822) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewGroup.layout(ViewGroup.java:4631) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5670) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.Choreographer.doCallbacks(Choreographer.java:574) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.Choreographer.doFrame(Choreographer.java:544) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.os.Handler.handleCallback(Handler.java:733) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.os.Handler.dispatchMessage(Handler.java:95) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.os.Looper.loop(Looper.java:136) 03-18 18:38:03.840:E / AndroidRuntime(6052):在android.app.ActivityThread.main(ActivityThread.java:5021) 03-18 18:38:03.840:E / AndroidRuntime(6052):at java.lang.reflect.Method.invokeNative(Native Method) 03-18 18:38:03.840:E / AndroidRuntime(6052):at java.lang.reflect.Method.invoke(Method.java:515) 03-18 18:38:03.840:E / AndroidRuntime(6052):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:827) 03-18 18:38:03.840:E / AndroidRuntime(6052):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 03-18 18:38:03.840:E / AndroidRuntime(6052):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我的问题是通过实施ArrayAdapter
的{{1}}方法解决的,该方法帮助ArrayAdapter
知道有多少对象要调用getView()
,一旦数据有改变。
@Override
public int getCount()
{
return data.size();
}
答案 1 :(得分:0)
如果您通过调用new ArrayList<String>(_entries);
创建新的ArrayList,则您的活动中的ArrayList将不会被适配器中的ArrayList引用。因此,当您在活动中更改条目Arraylist时,它不会影响Adapters ArrayList。将构造函数更改为:
public EntryAdapter(Context _context, int _resource, List<String> _entries){
super(_context, _resource, R.id.key, _entries);
this.resource = _resource;
this.entries = _entries;
this.count = 0;
}
或将onNewItemAdded
更改为:
public void onNewItemAdded(String[] _entry){
String key = new
key = _entry[0];
aa.add(key);
aa.notifyDataSetChanged();
}