Android Spinner的ArrayAdapter返回Parse.com objectid而不是username

时间:2015-10-07 18:24:45

标签: android parse-platform

我有一个微调器,可以下载我数据库中的所有用户,然后你可以选择一个,当发生这种情况时,我需要来自该对象的2个字符串。为此,我将parse.com用户添加为对象。然而,这导致我的Spinner包含objectids而不是在微调器中显示的用户名。

我已经在这里工作了几个小时试图理解我如何使微调器添加为对象并显示该对象的字符串。这是代码;

 public class BLETestActivity extends FragmentActivity implements AdapterView.OnItemSelectedListener{
private TextView out;
private ParseUser currentUser = ParseUser.getCurrentUser();
private String username;    Context context;
private BroadcastReceiver mReceiver;
private BluetoothDevice devicenew;
private ArrayList<String> list = new ArrayList<>();
private ArrayList<String> excluded = new ArrayList<>();

private Button mActionButton;

private ArrayList<ParseObject> nameList = new ArrayList<>();

private ArrayAdapter adapter;
final BluetoothAdapter btadapter = BluetoothAdapter.getDefaultAdapter();

private Spinner users;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ble2);

    out = (TextView) findViewById(R.id.out);

    // Getting the Bluetooth adapter
    users = (Spinner) findViewById(R.id.spinner);

    adapter = new ArrayAdapter<>(
            getApplicationContext(), android.R.layout.simple_list_item_1, nameList);
    uniSpinnerSetup();
    users.setOnItemSelectedListener(this);
    //out.append("\nAdapter: " + btadapter);
    String device_bt = btadapter.getAddress();

    // Check for Bluetooth support in the first place
    // Emulator doesn't support Bluetooth and will return null
    if (btadapter == null) {
        out.append("\nBluetooth NOT supported. Aborting.");
        return;


    }


    // Starting the device discovery
   // out.append("\nStarting discovery...");
    btadapter.cancelDiscovery();
    btadapter.startDiscovery();
    //FINDER NYE ENHEDER
    mReceiver = new BroadcastReceiver()

    {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            //Finding devices
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                devicenew = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                System.out.print(devicenew.getAddress());
                Log.d("Bluetooth", "jeg når her til");
     //           out.append("\nDevices Found:");
                if (!list.contains(devicenew.getAddress())) {
                    list.add(devicenew.getAddress());
                    Log.d("Devicenew", "adder");
                    System.out.print(devicenew.getAddress());
                    if(list != null) {
                        for(String object : list) {
                            Log.d("Object", "gennemløbes");
                            ScanBL();
                        }
                    }

                }
       //         out.append("\nFound device: " + devicenew);

         //       out.append("\nDone with discovery...");

            }
        }
    };
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter);

    // Listing devices

    currentUser.put("BT_ID", device_bt);
    currentUser.saveInBackground();
    context = getApplicationContext();

}
public void uniSpinnerSetup()
{

    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("_User");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null) {
                for (ParseObject object : list) {
                    nameList.add(object);
                    System.out.println("ADDING!");
                }

                users.setAdapter(adapter);


            } else {
                e.printStackTrace();
            }

        }

    });


}
@Override
public void onItemSelected(AdapterView parent, View v, int position, long id) {
    // TODO Auto-generated method stub
    Log.d("Onitem", "her?");
    ParseObject theSelectedObject = (ParseObject) adapter.getItem(position);

    if(excluded.contains(theSelectedObject.getString("BT_ID"))) {
        Log.d("excluded", "removed");
        excluded.remove(theSelectedObject.getString("BT_ID"));
        Toast.makeText(parent.getContext(), theSelectedObject.getString("username") + " er fjernet fra overvågning!", Toast.LENGTH_LONG).show();
        ScanBL();
    } else {
        excluded.add(theSelectedObject.getString("BT_ID"));
        Toast.makeText(parent.getContext(), "Valgt " +
                theSelectedObject.getString("username") + ", " + theSelectedObject.getString("BT_ID"), Toast.LENGTH_LONG).show();
        ScanBL();
    }
    System.out.println(excluded);
}

@Override
public void onNothingSelected(AdapterView parent) {
    // TODO Auto-generated method stub
    // Do nothing.
}
public void ScanBL() {
    final ParseQuery<ParseUser> queryParseUser = ParseUser.getQuery();
    queryParseUser.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> BTList, ParseException arg1) {
            // TODO Auto-generated method stub
            Log.d("Parse", "we made it this far");

            if (BTList != null && arg1 == null && list != null) {
                for (ParseUser parseObject : BTList) {
                    if (parseObject.getString("BT_ID") != null) {
                        for (String string : list) {
                            if (string.equals(parseObject.getString("BT_ID")) && excluded.contains(parseObject.getString("BT_ID"))) {
                                String BTuser = parseObject.getString("BT_ID");
                                String user = parseObject.getString("username");
                                Toast toast = Toast.makeText(context, "Wow, " + user + " is nearby!", Toast.LENGTH_SHORT);
                                toast.show();
                                //out.append("\nFound nearby device: " + user);
                                btadapter.cancelDiscovery();
                                btadapter.startDiscovery();
                                Log.d("Bluetooth", "burde parse?");
                            }
                        }
                    }
                }
            }

            else {
                Log.d("Bluetooth", "Fejl i returnering af data: ");
            }
        }
    });
}
public class MyAdapter extends ArrayAdapter<ParseObject>{
    private LayoutInflater mInflater;
    private int mResource;
    public MyAdapter(Context context, int resource, ParseObject[] objects) {
        super(context, resource, objects);
        mInflater= LayoutInflater.from(context);
        mResource=resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        TextView text;
        if (convertView == null) {
            view = mInflater.inflate(mResource, parent, false);
        } else {
            view = convertView;
        }
        text = (TextView) view; // assume your layout is only a textview
        ParseObject item = getItem(position);
        text.setText(item.getString("username"));
        return view;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_item_new_thingy:
            //Toast.makeText(this, "ADD!", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(BLETestActivity.this, MyPreferenceActivity.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_maps, container, false);
        return rootView;
    }

}



 }

我的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/out"/>

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:spinnerMode="dropdown"
        android:layout_below="@+id/out"
        android:layout_toEndOf="@+id/out"
        android:dropDownSelector="#6269ff" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

您正在将解析对象(因此适配器调用的toString()方法)添加到列表而不是用户名。

                for (ParseObject object : list) {
                    nameList.add(object);
                    System.out.println("ADDING!");
                }

应该是:

                for (ParseObject object : list) {
                    nameList.add(object.getString("username"));
                    System.out.println("ADDING!");
                }

但我认为你应该覆盖你的ArrayAdapter的getView方法来处理parseobjects。

如果你想坚持使用ArrayAdapter,你可以这样做:

public class MyAdapter extends ArrayAdapter<ParseObject>{
    private LayoutInflater mInflater;
    private int mResource;
    public MyAdapter(Context context, int resource, ParseObject[] objects) {
        super(context, resource, objects);
        mInflater= LayoutInflater.from(context);
        mResource=resource;
    }

    public MyAdapter(Context context, int resource, List<ParseObject> objects) {
        super(context, resource, objects);
        mInflater= LayoutInflater.from(context);
        mResource=resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        TextView text;
        if (convertView == null) {
            view = mInflater.inflate(mResource, parent, false);
        } else {
            view = convertView;
        }
        text = (TextView) view; // assume your layout is only a textview
        ParseObject item = getItem(position);
        text.setText(item.getString("username"));
        return view;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position,convertView,parent);
    }
}

答案 1 :(得分:0)

像这样更改 onItemSelected 代码。

brew reinstall gnuplot --with-aquaterm