1

我想创建我的自定义 baseadapter 所以我尝试以下操作:

MainActivity.java

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.util.ArrayList;

public class SignupActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        String [] arr = {"USA","Canda","Germany","Italy"};
        Spinner S1 = (Spinner) findViewById(R.id.Spinner_Signup);
        CustomCountryAdpater adapter = new CustomCountryAdpater(this, arr);
        S1.setAdapter(adapter);
    }
}

CustomCountryAdpater.java

import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.content.Context;
import java.util.ArrayList;
import android.widget.TextView;

public class CustomCountryAdpater extends BaseAdapter {
    public Context con;
    //public ArrayList<Country> tempList;
    String [] arr;
    public LayoutInflater myInflater;
    public CustomCountryAdpater (Context C,String [] arr)
    {
        this.con = C;
        this.arr = arr;
        this.myInflater = (LayoutInflater) C.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.arr.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return this.arr[arg0];
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        if (arg1==null)
        {
            arg1 = myInflater.inflate(android.R.layout.simple_list_item_1, arg2, false);
            TextView TT = (TextView) arg1.findViewById(android.R.id.text1);
            TT.setText(this.arr[arg0]);
        }       
        return arg1;
    }

}

当我第一次单击微调器时,我得到了正确的列表:

在此处输入图像描述

但是如果我选择德国,例如并再次单击微调器,我会得到以下没有德国的列表!

在此处输入图像描述

谁能告诉我为什么?!!!

4

1 回答 1

1

因为您有条件地更新文本。您使用的 arg1 可能是旧的缓存视图。

在 if 之后移动这些行:

TextView TT = (TextView) arg1.findViewById(android.R.id.text1);
TT.setText(this.arr[arg0]);
于 2013-07-13T21:13:58.303 回答