0

I am building an app that contains a listview. What I am trying to accomplish is when I click on each unique item in the listview, each item will set a particular Java class. I put all of the Java Classes that I want to be able to select inside of a Java package, but I am also considering putting them with the rest of the java classes. To give some context, each Java class sets a certain xml file. The reason why I can't just set the xml file is because it requires java to function as well. One of the things that needs to be taken into consideration is that there are multiple Java classes which will need to be disregarded, except for my adapter class, the main activity, and the additional java class that is selected.

Here is the MainActivity. I am using if statements: if (item_position==1) {then set a particular java class}. if (item_position==2) {then set another java class}. The problem is that I am unable to figure out and find out what to put inside the brackets to set the Java class.

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();

Here is an example of one of the Java classes that I am trying to select. I am also open to setting the xml class by which item I selcted in one of the Java classes instead of the 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() {

}
Fgf567
  • 21
  • 8
  • This reads like you're trying to do something in the Activity that should be done in the adapter. – Gabe Sechan Nov 15 '17 at 18:32
  • What you want do exactly inside on click listener...are you try to open dialogue or another activity or fragments ??? – Dilip Nov 15 '17 at 18:39
  • I not sure which of those this will count towards. An open dialogue? I am trying to choose between keyboard layouts- which require both Java and xml to function. – Fgf567 Nov 15 '17 at 18:47
  • set this at viewholder, https://stackoverflow.com/questions/27081787/onclicklistener-for-cardview – kyorilys Nov 16 '17 at 01:18
  • Can you explain your reasoning behind your suggestion? – Fgf567 Nov 16 '17 at 01:59

0 Answers0