我想创建自定义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;
}
}
当我第一次点击微调器时,我得到了正确的列表:
但如果我选择德国,例如再次点击微调器,我会得到以下没有德国的清单!
谁能告诉我为什么?!!!
答案 0 :(得分:1)
因为您有条件地更新了文本。您使用的arg1可能是旧的缓存视图。
在if:
之后移动这些行TextView TT = (TextView) arg1.findViewById(android.R.id.text1);
TT.setText(this.arr[arg0]);