我正在尝试从sqlite表填充微调项目
我不知道怎么做,我试着效仿其他例子,但我认为它与我的不同
最简单的方法是什么?如何为每个项目设置onclicklistener
提前致谢
MainActivity + sqliteopenhelper
public ArrayList<String> getTableValues() {
ArrayList<String> my_array = new ArrayList<String>();
try {
SQLiteDatabase db = this.getWritableDatabase();
Cursor resultPPNAME = db.rawQuery("select NAMEPPL from " + Table_name2, null);
if (resultPPNAME.moveToFirst()) {
String PPLNAMESPIN = resultPPNAME.getString(0);
my_array.add(PPLNAMESPIN);
}
while (resultPPNAME.moveToNext()) ;
} catch (Exception e) {
}
return my_array;
}
MainActivity
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
DB db;
Button addmed,addpl;
TextView PPLNAMERES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PPLNAMERES = (TextView)findViewById(R.id.PPLNAMEGETNAME);
db = new DB(this);
listView();
String spinValue = db.getMainData().toString();
ArrayList<String> my_array= new ArrayList<String>();
my_array = db.getTableValues();
Spinner spinner = (Spinner)findViewById(R.id.spin1);
ArrayAdapter<String> my_adapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,my_array);
spinner.setAdapter(my_adapter);
}
public void addmedView(View view){
Intent ADDMEDVIEW = new Intent(this,ADDMEDCINEVIEW.class);
startActivity(ADDMEDVIEW);
}
public void addpplview(View view){
Intent ADDPPLVIEWS = new Intent(this,ADDPPLVIEW.class);
startActivity(ADDPPLVIEWS);
}
public void listView(){
Cursor res= db.getMainData();
if(res.moveToFirst()){
PPLNAMERES.setText(res.getString(0));
} else{
Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_LONG).show();
return;
}}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}}
适配器代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PPLNAMERES = (TextView)findViewById(R.id.PPLNAMEGETNAME);
db = new DB(this);
ArrayList<String> my_array= new ArrayList<String>();
my_array = db.getTableValues();
Spinner spinner = (Spinner)findViewById(R.id.spin1);
ArrayAdapter<String> my_adapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,my_array);
spinner.setAdapter(my_adapter);
}
onitemselected方法
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) { }
答案 0 :(得分:1)
//试试这个。
public ArrayList<String> getTableValues() {
ArrayList<String> my_array = new ArrayList<String>();
try {
SQLiteDatabase db = this.getWritableDatabase();
Cursor resultPPNAME = db.rawQuery("select NAMEPPL from " + Table_name2, null);
// Change Made HERE
resultPPNAME.moveToFirst();
while (cur.isAfterLast() == false) {
String PPLNAMESPIN = resultPPNAME.getString(0);
cur.moveToNext();
my_array.add(PPLNAMESPIN);
}
} catch (Exception e) {
}
return my_array;
}