开发者

Android spinner change position of element after selection

开发者 https://www.devze.com 2023-02-04 08:43 出处:网络
Hi I got a problem with a spinner, a开发者_如何学Cfter selecting an item the position of elements in the list change, here\'s my code:

Hi I got a problem with a spinner, a开发者_如何学Cfter selecting an item the position of elements in the list change, here's my code: private static class ServerListItemAdapter extends 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;
    }
}

If I start with a list like { A,B,C} and I select A, the list became {A,B,A} but into the code, as you can see, there's no statement that ask android to do so.

Thanks


Take a guide from following code:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号