问题描述
package com.example.ak.aapppcom;
import android.content.Intent;
import android.database.Cursor;
导入android.os.AsyncTask;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android。 widget.RatingBar;
import android.widget.Spinner;
import java.util.ArrayList;
公共类MainActivity扩展AppCompatActivity实现AdapterView.OnItemSelectedListener {
Button iv;
EditText et1;
RatingBar ra1;
Spinner spn;
private SQLController SQLcon;
@O verride
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv =(按钮)findViewById(R.id.imageButton);
et1 =(EditText)findViewById(R.id.et1) ;
ra1 =(RatingBar)findViewById(R.id.ratingBar1);
spn =(Spinner)findViewById(R.id.spin);
spn.setOnItemSelectedListener(this);
SQLcon = new SQLController(this);
//打开数据库
SQLcon.open( );
loadtospinner();
}
public void loadtospinner(){
Cursor c = SQLcon.readData();
ArrayList< string> al = new ArrayList< string>();
c.moveToFirst();
while(!c.isAfterLast()){
String name = c.getString(c.getColumnIndex(DBhelper.COMMENTS));
al .add(姓名);
c.moveToNext();
}
}
@Override
public boolean onCreateOptionsMenu(菜单菜单){
//给菜单充气;这会将项目添加到操作栏中。
getMenuInflater()。inflate(R.menu.pop_up,menu); //菜单资源,菜单
返回true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.item1:
//Toast.makeText (getApplicationContext(),Item 1 Selected ,Toast.LENGTH_LONG)。show();
Intent i = new Intent(getApplicationContext(),Item1.class);
startActivity(i);
返回true;
case R.id.item2:
//Toast.makeText (getApplicationContext(),Item 2 Selected,Toast.LENGTH_LONG) .show();
Intent i1 = new Intent(getApplicationContext(),Item1.class);
startActivity(i1);
return是的;
case R.id.item3:
//Toast.makeText (getApplicationContext(),Item 3 Selected,Toast.LENGTH_LONG).show();
Intent i2 = new Intent(getApplicationContext(),Item1.class);
startActivity(i2);
返回true;
默认值:
返回super.onOptionsItemSelected(item);
}
}
@Override
public void onItemSelected(AdapterView<?> parent,View view,int position,long id){
String getcom;
getcom =;
int getrate;
getrate = 0;
SQLController handlerInfo = new SQLController(getBaseContext());
handlerInfo.open();
Cursor cursor = handlerInfo.readData();
if(cursor.moveToFirst()){
do {
getcom = cursor.getString(1);
getrate = cursor.getInt(2);
} while(cursor.moveToNext ());
}
handlerInfo.close();
}
@Override
public void onNothingSelected(AdapterView<?> parent){
}
类MyAsync扩展AsyncTask< Void,Void,Void>
{
私人Dialog PD;
@Override
protected Void doInBackground(Void ... params)
{
String s = et1.getText()。toString();
int rate = ra1.getNumStars();
//打开数据库
SQLcon.open();
//将数据插入表格
SQLcon.insertData(com,rate );
返回null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
loadtospinner();
PD.dismiss();
}
}
}
我的尝试:
i想要将编辑文本的值存储到微调器中。请给我建议
package com.example.ak.aapppcom;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
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.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Button iv;
EditText et1;
RatingBar ra1;
Spinner spn;
private SQLController SQLcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (Button) findViewById(R.id.imageButton);
et1 = (EditText) findViewById(R.id.et1);
ra1 = (RatingBar) findViewById(R.id.ratingBar1);
spn = (Spinner) findViewById(R.id.spin);
spn.setOnItemSelectedListener(this);
SQLcon = new SQLController(this);
// opening database
SQLcon.open();
loadtospinner();
}
public void loadtospinner() {
Cursor c = SQLcon.readData();
ArrayList<string> al = new ArrayList<string>();
c.moveToFirst();
while (!c.isAfterLast()) {
String name = c.getString(c.getColumnIndex(DBhelper.COMMENTS));
al.add(name);
c.moveToNext();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pop_up, menu);//Menu Resource, Menu
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
//Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), Item1.class);
startActivity(i);
return true;
case R.id.item2:
//Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
Intent i1 = new Intent(getApplicationContext(), Item1.class);
startActivity(i1);
return true;
case R.id.item3:
//Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
Intent i2 = new Intent(getApplicationContext(), Item1.class);
startActivity(i2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String getcom;
getcom = "";
int getrate;
getrate = 0;
SQLController handlerInfo = new SQLController(getBaseContext());
handlerInfo.open();
Cursor cursor = handlerInfo.readData();
if (cursor.moveToFirst()) {
do {
getcom = cursor.getString(1);
getrate = cursor.getInt(2);
} while (cursor.moveToNext());
}
handlerInfo.close();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
class MyAsync extends AsyncTask<Void, Void, Void>
{
private Dialog PD;
@Override
protected Void doInBackground(Void... params)
{
String s = et1.getText().toString();
int rate = ra1.getNumStars();
// opening database
SQLcon.open();
// insert data into table
SQLcon.insertData(com, rate);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
loadtospinner();
PD.dismiss();
}
}
}
What I have tried:
i want to store the value of Edit text in to spinner. please give me the suggestion
这篇关于将文本值转换为微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!