gpt4 book ai didi

android - 在 Android 2.3.3 中创建自定义 BaseAdapter

转载 作者:行者123 更新时间:2023-11-30 03:29:53 26 4
gpt4 key购买 nike

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

主 Activity .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;
}

}

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

enter image description here

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

enter image description here

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

最佳答案

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

在 if 之后移动这些行:

TextView TT = (TextView) arg1.findViewById(android.R.id.text1);
TT.setText(this.arr[arg0]);

关于android - 在 Android 2.3.3 中创建自定义 BaseAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17634200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com