嗨我有一个微调器的问题,在选择一个项目后,列表中元素的位置发生变化,这是我的代码: 私有静态类ServerListItemAdapter扩展BaseAdapter {
private ArrayList<ServerData> serverList;
private LayoutInflater mInflater;
public ServerListItemAdapter(Context context,ArrayList<ServerData> _serverList) {
mInflater = LayoutInflater.from(context);
serverList=_serverList;
}
@Override
public int getCount() {
return serverList.size();
}
@Override
public Object getItem(int pos) {
return serverList.get(pos);
}
@Override
public long getItemId(int pos) {
return serverList.get(pos).hashCode();
}
private void printServerList(){
Log.d(TAG, "PRINT - start");
int pos=0;
for(ServerData server:serverList){
Log.d(TAG, pos+":"+server.serverName);
pos++;
}
Log.d(TAG, "PRINT - end");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "getView -start- pos:"+position);
printServerList();
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.select_server_list_item, null);
holder = new ViewHolder();
holder.server=serverList.get(position);
Log.d(TAG,"Pos:"+position+", server:"+holder.server.serverName);
holder.txtServerName=(TextView)convertView.findViewById(R.id.txt_server_name);
Log.d(TAG, "Set text for position "+ position+ ": "+ holder.server.serverName);
holder.txtServerName.setText(serverList.get(position).serverName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Log.d(TAG, "getView -end- pos:"+position);
return convertView;
}
static class ViewHolder {
TextView txtServerName;
ServerData server;
}
}
private class ServerData{
public String serverName="";
public String serverURL="";
public ServerData(String _serverName,String _serverURL){
serverName=_serverName;
serverURL=_serverURL;
}
}
如果我从{A,B,C}这样的列表开始,我选择A,列表变为{A,B,A},但是在代码中,正如您所看到的,没有声明要求android做如此。
由于
答案 0 :(得分:-2)
从以下代码中获取指南:
SharedPreferences prefPos = MainMenuActivityOld.this.getSharedPreferences("SITE_N_BOREHOLE_POSITION", 0);
CheckReply.selectedSitePosition = temp = prefPos.getInt("SitePos",CheckReply.selectedSitePosition);
Spinner s1 = (Spinner) findViewById(R.id.spinnerSiteList);
ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for(int i = 0; i<CheckReply.siteSelectedAdapter.getCount(); i++){
// for highlighting the last selected site in spinner
if(i==0)
{
adapter.add((CharSequence) (CheckReply.siteSelectedAdapter.getItem(CheckReply.selectedSitePosition)));
}
else{
if(i == (CheckReply.selectedSitePosition))
{
adapter.add((CharSequence) CheckReply.siteSelectedAdapter.getItem(0));
}
else
{
adapter.add((CharSequence) CheckReply.siteSelectedAdapter.getItem(i));
}
}
}
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
int newPosition = 0;
if(position == 0){
newPosition = temp;
}
else {
if(position == temp){
newPosition = 0;
}
else
{
newPosition =position;
}
}
CheckReply.selectedSitePosition = newPosition;
SharedPreferences.Editor prefSiteNBoreHolePosSet = MainMenuActivityOld.this.getSharedPreferences("SITE_N_BOREHOLE_POSITION", 0).edit();
prefSiteNBoreHolePosSet.putInt("SitePos", CheckReply.selectedSitePosition);