Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在listview中添加项时发生IndexAutofBound异常_Android_Android Listview_Indexoutofboundsexception - Fatal编程技术网

Android 在listview中添加项时发生IndexAutofBound异常

Android 在listview中添加项时发生IndexAutofBound异常,android,android-listview,indexoutofboundsexception,Android,Android Listview,Indexoutofboundsexception,你好,我是android新手 我很难在daynamic listview中添加元素。 当我点击TextView(tv)时,它应该是ArrayList末尾的add元素,但当我向下滚动到listview时,它崩溃了,出现indexoutofbound异常 public class PosterList extends Activity { MyCustomAdapter dataAdapter = null; ArrayList<Country> countryList = new

你好,我是android新手 我很难在daynamic listview中添加元素。 当我点击TextView(tv)时,它应该是ArrayList末尾的add元素,但当我向下滚动到listview时,它崩溃了,出现indexoutofbound异常

public class PosterList extends Activity 
{

MyCustomAdapter dataAdapter = null;
ArrayList<Country> countryList = new ArrayList<Country>();

TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.posterlist);

            //Click on textview to add element in contrylist

    tv = (TextView) findViewById(R.id.myFilter);
    tv.setOnClickListener(new View.OnClickListener() 
    {
            public void onClick(View v) 
            {
                Country country = new Country("df","df","df","m");
                countryList.add(country);      
                dataAdapter.notifyDataSetChanged();
            }
    });

    displayListView();

}

private void displayListView() 
{

            //Parse my JSON and store it in to different arrays

    for(int k=0;k<len;k++)
    {
        Country country = new Country(subcategory[k],caseid[k],time[k],newpost[k]);
        countryList.add(country);
    }

    dataAdapter = new MyCustomAdapter(this,R.layout.country_info, countryList);
    ListView listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(dataAdapter);

    listView.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) 
        {

                Country country = (Country) parent.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(),
                country.getContinent(), Toast.LENGTH_SHORT).show();

        }
    });


}

private class MyCustomAdapter extends ArrayAdapter<Country> 
{

    private ArrayList<Country> originalList;
    private ArrayList<Country> countryList;

    public MyCustomAdapter(Context context, int textViewResourceId, 
        ArrayList<Country> countryList) {
    super(context, textViewResourceId, countryList);
    this.countryList = new ArrayList<Country>();
    this.countryList.addAll(countryList);
    this.originalList = new ArrayList<Country>();
    this.originalList.addAll(countryList);
}

private class ViewHolder 
{
    TextView code;
    TextView name;
    TextView continent;
    TextView region;
 }

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{

    ViewHolder holder = null;
    Log.v("ConvertView", String.valueOf(position));
    if (convertView == null) 
    {

        LayoutInflater vi = (LayoutInflater)getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.country_info, null);

        holder = new ViewHolder();
        holder.code = (TextView) convertView.findViewById(R.id.code);
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.continent = (TextView) convertView.findViewById(R.id.continent);
        holder.region = (TextView) convertView.findViewById(R.id.region);

        convertView.setTag(holder);

    } 
    else
     {
            holder = (ViewHolder) convertView.getTag();
     }

    Country country = countryList.get(position);
    holder.code.setText(country.getCode());
    holder.name.setText(country.getName());
    holder.continent.setText(country.getContinent());
    holder.region.setText(country.getRegion());

    return convertView;

  }
   }
}

去掉适配器中的这些字段

private ArrayList<Country> originalList;
private ArrayList<Country> countryList;
private ArrayList originalist;
私有ArrayList国家列表;
您正在修改正在适配器中传递给超级调用的countryList,但您正在从countryList中读取,该countryList存储为适配器中的一个字段,当您添加新国家/地区时,该字段永远不会被修改


您正在创建原始国家/地区列表的副本。。但是在创建副本后,您永远不会修改它

放置堆栈跟踪!!我猜这些数组中的一个或所有数组都小于
len
子类别[k]、caseid[k]、time[k]、newpost[k]
。。。但是我们该怎么帮你呢?嗨谢谢你的帮助response@user1259670-我不知道你在问什么关于你的国家班。您正在调用super();在一个没有超类可调用的类中。。
private ArrayList<Country> originalList;
private ArrayList<Country> countryList;