在微调器上获取所选项目将不起作用

时间:2014-04-20 14:10:23

标签: android listview

我试图在微调器上从基于数据库的选定项检索数据。 这是我的数据库结构Database Structure

这是我的活动:

public class help_activity extends Activity implements OnClickListener{

Spinner spinner1;
SQLiteConnector sqlConnect;
ListView lvUsers;
Button b1;



String colors[] = {"Bristleback","Sven","Tiny","Undying", "Naix","Weaver","Spectre","Lich"};    
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_help);

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    lvUsers = (ListView) findViewById(R.id.listView1);
    b1 = (Button) findViewById(R.id.btn1);
    sqlConnect = new SQLiteConnector(this);
    addListenerOnSpinnerItemSelection();

    final String nameSelected = spinner1.getSelectedItem().toString();      

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, sqlConnect.getAllRecord(nameSelected));

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            if (nameSelected.equals("Bristleback")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            } 
            else if (nameSelected.equals("Sven")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            else if (nameSelected.equals("Tiny")) {
                lvUsers.setAdapter(adapter);
            } 
            else if (nameSelected.equals("Undying")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            else if (nameSelected.equals("Naix")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            else if (nameSelected.equals("Weaver")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            else if (nameSelected.equals("Spectre")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            else if (nameSelected.equals("Lich")) {
                lvUsers.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            // TODO Auto-generated method stub

        }
    });





}
public void addListenerOnSpinnerItemSelection() {


        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
        (this, android.R.layout.simple_spinner_dropdown_item,colors );
        spinner1.setAdapter(spinnerArrayAdapter);
      }



@Override
public void onClick(View v) {



}

}

这是我从数据库中检索数据的方法:

public List<String> getAllRecord(String nameSelected) { 

    List<String> namagambar = new ArrayList<String>(); 
    String selectQuery = "SELECT * FROM " + TABLE_RECORD + " WHERE "
    + HERO_NAME + "=?"; 

    database = dbHelper.getReadableDatabase(); 
    cursor = database.rawQuery(selectQuery, new String[]{nameSelected}); 

    if (cursor.moveToFirst()) { 

        do { 

            namagambar.add(cursor.getString(1)); 
            } while (cursor.moveToNext()); 

    } 
    database.close(); 
    return namagambar; 

} 

因此,当用户在微调器上选择一个项目时,nama_hero上的数据将根据微调器上选定的名称英雄显示在列表视图中。但是当我运行我的代码时,它只显示了数据库中的数据顶部 因此,当我在微调器上选择名称Sven时,listview上显示的名称不是“sven”而是“bristleback”。当我选择其他英雄名字时也会发生这种情况。

我认为我在旋转器中获取所选项目名称的方法不起作用

请帮我解决这个问题。

已解决!!!

2 个答案:

答案 0 :(得分:1)

完成!! 我已经解决了这个问题,感谢stackoverflow 我将setOnclickListener更改为此b1.setOnClickListener(this);

移动之前的所有项目:

public void onClick(View v) {


}

答案 1 :(得分:0)

尝试在 ClickListener

中选择项目
b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String nameSelected = spinner1.getSelectedItem().toString();