在MainActivity

时间:2017-11-15 18:31:18

标签: java android class listview

我正在构建一个包含listview的应用。我想要完成的是当我点击listview中的每个唯一项时,每个项目将设置一个特定的Java类。我把我希望能够在Java包中选择的所有Java类放在一起,但我也在考虑将它们与其他java类一起使用。为了给出一些上下文,每个Java类都设置一个特定的xml文件。我不能只设置xml文件的原因是因为它需要java才能运行。需要考虑的一件事是,除了我的适配器类,主要活动和所选的其他java类之外,还需要忽略多个Java类。

这是MainActivity。我正在使用if语句:if (item_position==1) {then set a particular java class}. if (item_position==2) {then set another java class}.问题是我无法找出并找出放在括号内的内容来设置Java类。

public class MainActivity extends AppCompatActivity {

    ArrayList<String> selectedItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        selectedItems = new ArrayList<String>();
        ListView chl = (ListView) findViewById(R.id.fruitelector);
        chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        String[] items = {"Apple", "Pear", "Banana", "Melon", "Grape", "Blueberry"};
        Adapter adapter=new Adapter(this,items);
        chl.setAdapter(adapter);

        chl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                int item_position=i;
                if (item_position==1){
                    ////set a particular Java class
                }
                if (item_position==2){
                    ///set another particular java class
                }                    
                Toast.makeText(MainActivity.this, "Item position "+item_position+" clicked", Toast.LENGTH_SHORT).show();

以下是我尝试选择的其中一个Java类的示例。我也愿意设置xml类,我在其中一个Java类而不是MainActivity中选择了该项。

@Override
public View onCreateInputView() {
    kv = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
    keyboard = new Keyboard(this, R.xml.qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return kv;
}


private void playClick(int keyCode) {
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    switch (keyCode) {
        case 32:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
            break;
        case Keyboard.KEYCODE_DONE:
        case 10:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
            break;
        case Keyboard.KEYCODE_DELETE:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
            break;
        default:
            am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
    }
}

@Override
public void onPress(int primaryCode) {

}

@Override
public void onRelease(int primaryCode) {

}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
    playClick(primaryCode);
    switch (primaryCode) {
        case Keyboard.KEYCODE_DELETE:
            ic.deleteSurroundingText(1, 0);
            break;
        case Keyboard.KEYCODE_SHIFT:
            caps = !caps;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            break;
        case Keyboard.KEYCODE_DONE:
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
            break;
        default:
            char code = (char) primaryCode;
            if (Character.isLetter(code) && caps) {
                code = Character.toUpperCase(code);
            }
            ic.commitText(String.valueOf(code), 1);

    }
}

@Override
public void onText(CharSequence text) {

}

@Override
public void swipeLeft() {

}

@Override
public void swipeRight() {

}

@Override
public void swipeDown() {

}

@Override
public void swipeUp() {

}

0 个答案:

没有答案