如何获得在微调器上选择的项目的ID?

时间:2017-12-19 13:21:42

标签: java android database android-spinner insert-into

我创建了一个活动,其中用户需要从微调器中选择一个名称,在txtbox中输入一条消息并单击一个按钮以发送消息。微调器中有2个字符串(名字和姓氏)。数据库中的每个名称都有一个唯一的ID。 如何从数据库中获取名称的ID?

示例数据:ID:265958998,Fname:Leo,Lname:Cruz

以下是我的代码

发送按钮:

            btnsend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                String USERID=getIntent().toString();

                db.open();
            *//    db.insertMessage(USERID,  );*
                db.close();

                et10.setText("");
                Toast.makeText(getApplicationContext(),
                        "Message Sent", Toast.LENGTH_SHORT).show();}}
            });

将消息插入db:

   void insertMessage(String sender, String recipient) {
    SQLiteDatabase db = DBHelper.getWritableDatabase();

    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_SENDER, sender);
    initialValues.put(KEY_RECIPIENT, recipient);
    db.insert(TABLE_MESSAGE, null, initialValues);


    db.close(); // Closing database connection
}

Spinnerquery:

           public List<String> getAllRegisteredInfoAcc(){
    List<String> labels = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT tableinfo.*, tableacc.*" +
            "FROM tableinfo JOIN tableacc ON tableinfo.userid = tableacc.userid WHERE tableacc.userstatus=? ";
    Cursor cursor = db.rawQuery(selectQuery , new String[]{ "REGISTERED"});

    SQLiteDatabase db = DBHelper.getReadableDatabase();
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(4)+""+cursor.getString(3));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();
    return labels;
}

CallSpinner:

        spn1();

}
public void spn1() {
    // database handler
    DBAdapter db = new DBAdapter(getApplicationContext());
    List<String> lables = db.getAllRegisteredInfoAcc();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

     dataAdapter.setDropDownViewResource
         (android.R.layout.simple_spinner_dropdown_item);
    spnr1.setAdapter(dataAdapter);
}

2 个答案:

答案 0 :(得分:0)

你应该为微调器创建一个Adapter,将数据从sqlite加载到一个Objects数组中,然后通过toString()函数映射反射到微调器中的数组中的对象。

首先为对象创建一个类并添加toString()函数

public class MyUser {
 private int id = 0; 
 private String name = ""; 
 private String lastName ="";

 public MyUser(){}


 public String toString()
 {
    return(name + " " + lastName);
 }

}

然后将数据从sqlite加载到该对象的数组中。

在微调器中

users.addAll(myDataSource.getUsers());

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, users);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnr1.setPrompt("Select your User");
        spnr1.setAdapter(
                new NothingSelectedSpinnerAdapter(
                        adapter,
                        R.layout.row_state_register_nothing_selected,
                        // R.layout.contact_spinner_nothing_selected_dropdown, // Optional
                        this));

所以在onItemClickListener()中:

spnr1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                if (position > 0) { //because of the prompt
                    userID = users.get(position - 1).getUserId(); 
                    // DO WHAT YOU WANT
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

答案 1 :(得分:0)

发送按钮监听器:

         btnsend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             String USERID=getIntent().toString();  //userid from login activity
               int a = spinnername.getSelectedItemPosition();

                Cursor data = database.getallUsers();
                data.moveToPosition(a);

                database.open();
                //insert sender, recipient, message...
               database.insertMessage(USERID,data.getString(0)  ,et10.getText().toString());

                database.close();
                et10.setText("");
                Toast.makeText(getApplicationContext(),
                        "Message Sent", Toast.LENGTH_SHORT).show(); 
          }});