0

こんにちは、私はアンドロイドの初心者です。デイナミックリストビューに要素を追加するのが難しいです。TextView(tv) をクリックすると、ArrayList の最後に要素を追加する必要がありますが、リストビューまでスクロールすると、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;

  }
   }
}

これが私の国のクラスです。このコードの何が問題なのか、誰か助けてもらえますか?

`enter code here`public class Country {

 String code = null;
 String name = null;
 String continent = null;
 String region = null;

 public Country(String code, String name, String continent, String region) {
  super();
  this.code = code;
  this.name = name;
  this.continent = continent;
  this.region = region;
 }

 public String getCode() {
  return code;
 }
 public void setCode(String code) {
  this.code = code;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getContinent() {
  return continent;
 }
 public void setContinent(String continent) {
  this.continent = continent;
 }
 public String getRegion() {
  return region;
 }
 public void setRegion(String region) {
  this.region = region;
 }

 @Override
 public String toString() {
  return  code + " " + name + " "
    + continent + " " + region;
 }
4

1 に答える 1

0

アダプタのこれらのフィールドを削除します

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

アダプターのスーパーコールに渡されるcountryListを変更していますが、アダプター内のフィールドとして格納されているcountryListから読み取っています。このフィールドは、新しい国を追加しても変更されません。

元のカントリーリストのコピーを作成していますが、作成後にコピーを変更することはありません。

于 2013-02-04T20:59:59.077 に答える