ssummeraw 2013-05-22 02:12 采纳率: 10%
浏览 5341
已采纳

Arrayindexoutofboundsexception 异常

这个问题是关于android中customListview 的。我想使用onclick事件(tv.setOnClickListener)在list中动态的添加内容。我有自定义的country类使用geter and seter 方法来储存数据。当我动态的添加新的内容,然后滚动到list的底部时,获取 Arrayindexoutofboundexception 异常。

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;
  }
   }
}
  • 写回答

3条回答 默认 最新

  • Curie-87 2013-05-22 09:19
    关注

    在你的 getView 方法中,你提到 countryList,有两个 countryList 变量:一个是在 PostList 类中,一个是在 MyCustomAdapter 类中。你提及到的那个是在 MyCustomerAdapter 类中,没有适配器支持数据。在 getView 方法获取 Country 对象:

    Country country = (Country) getItem(position);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化