android 无法在微调器上获取所选项目

wgmfuz8q  于 2023-05-21  发布在  Android
关注(0)|答案(2)|浏览(110)

我试图从数据库中检索数据的基础上选择的项目微调。下面是我的数据库结构

以下是我的活动:

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; 
    
}

因此,当用户在spinner上选择一个项目时,nama_hero处的数据将根据spinner上选择的名称hero显示在listview中。但是当我运行我的代码时,它只显示数据库中数据的顶部,所以当我在spinner上选择名称Sven时,在listview上显示的名称不是“Sven”,而是“bristleback”。当我选择其他英雄的名字时也是如此。
我想我的方法,以获得名称的选定项目在微调不工作
请帮我解决这个问题。

2hh7jdfx

2hh7jdfx1#

完成!!我已经解决了这个问题,感谢stackoverflow我改变了我的setOnclickListener变成这样b1.setOnClickListener(this);
并将之前中的所有项移动到:

public void onClick(View v) {

}
efzxgjgh

efzxgjgh2#

尝试获取所选项目内部ClickListener

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

相关问题