0

我对我在 android 中的 customListview 有疑问。我正在尝试使用 onclick 事件(tv.setOnClickListener)在列表中动态添加内容,我有我的客户国家/地区类,我使用 geter 和 seter 方法存储我的数据。问题:当我动态添加新内容并向下滚动到列表末尾时,我得到 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;

  }
   }
}
4

2 回答 2

1

我看到了问题。在您的getView方法中,您引用countryList. 你有两个countryList变量:一个在PostList班级里,一个在MyCustomAdapter班级里。您引用的是MyCustomerAdapter类中的那个,它不是适配器支持的数据。在方法中以这种方式获取您的Country对象getView

Country country = (Country) getItem(position);
于 2013-02-24T03:22:46.517 回答
0

这是你的 for 循环。您必须在动态添加信息之前对其进行初始化。mso 初始化它,然后在 for 循环内使用类方法添加该信息。应该能解决你的问题。

于 2013-02-24T03:27:30.007 回答