如何获取微调器中所选位置的ID(或其他元素)?

时间:2018-07-02 13:47:56

标签: java sqlite spinner

有一个基本的Sqlite,可以在Spinner中选择该数据库(用户名)中的元素。选择用户时使用Textview。

例如,有两行:

用户John,密码123,

吉姆用户,密码321。

有必要在Spinner中选择“ John”时,在Textview字段中选择“ 123”,在Spinner中选择“ Jim”时,Textview字段为“ 321”。 我了解这个问题是Newbi,但无法解决)

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    Spinner allusers;
    List<String> users=new ArrayList<>();
    EditText uname,upassword;
    Button adduser;
    ArrayAdapter<String> adapter;
    DbHandler db;
    TextView myTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        allusers=(Spinner)findViewById(R.id.selectuser);
        uname=(EditText)findViewById(R.id.username);
        upassword=(EditText)findViewById(R.id.password);
        adduser=(Button)findViewById(R.id.adduser);
        db=new DbHandler(MainActivity.this);
        adduser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username=uname.getText().toString();
                String password=upassword.getText().toString();
                if(username.equalsIgnoreCase("")||password.equalsIgnoreCase(""))
                {
                    Toast.makeText(MainActivity.this,"Please Enter Username and Password",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    db.addUser(new User(username,password));
                    prepareData();
                    Toast.makeText(MainActivity.this,"User was Successfully added to Database",Toast.LENGTH_SHORT).show();
                }
            }
        });
        myTextView = (TextView) findViewById(R.id.mPassword);
        prepareData();
        allusers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {             
            //AA
            User selected = (User) parent.getItemAtPosition(position);
            myTextView.setText(selected.getPassword());
            Toast.makeText(getApplicationContext(), "" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}

public void prepareData() {
    users = db.getAllUsers();
    adapter = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1, users);
    allusers.setAdapter(adapter);
    }
}

User.java

public class User {
int id;
String name;
String password;
public User(String name, String password)
{
    this.name=name;
    this.password=password;
}
public User(int id, String name, String psd)
{
    this.id=id;
    this.name=name;
    this.password=psd;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
    }
}

DbHandler.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DbHandler extends SQLiteOpenHelper {
    private  static final int Db_Version=1;
    private static final String Db_Name="users";
    private static final String Table_Name="user";
    private static final String User_id="id";
    private static final String User_name="name";
    private static final String User_password="password";
    public DbHandler(Context context)
    {
        super(context,Db_Name,null,Db_Version);
    }
        @Override
    public void onCreate(SQLiteDatabase db) {
            String Create_Table="CREATE TABLE " + Table_Name + "(" + User_id
            + " INTEGER PRIMARY KEY," + User_name + " TEXT," + User_password + " TEXT" + ")";
        db.execSQL(Create_Table);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + Table_Name);
            onCreate(db);
    }
    public void addUser(User usr)
    {
            SQLiteDatabase db=this.getWritableDatabase();
            ContentValues cv=new ContentValues();
            cv.put(User_name,usr.getName());
            cv.put(User_password,usr.getPassword());
            db.insert(Table_Name, null, cv);
            db.close();
    }
    public int checkUser(User us)
    {
        int id=-1;
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("SELECT id FROM user WHERE name=? AND password=?",new String[]{us.getName(),us.getPassword()});
        if(cursor.getCount()>0) {
            cursor.moveToFirst();
            id=cursor.getInt(0);
            cursor.close();
        }
        return id;
    }

    public List<String> getAllUsers()
    {
        List<String> userlist=new ArrayList<>();
           SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("SELECT name FROM user",null);
        if(cursor.moveToFirst())
        {
        do {
            userlist.add(cursor.getString(0));
        }while (cursor.moveToNext());
    }
        cursor.close();
        db.close();
        return userlist;
    }
}

突出显示了无效的注释“ AA”代码。 请告诉我错误在哪里。 对不起我的英语不好) 谢谢。

0 个答案:

没有答案