0

I've got a problem similar to the one described here and a lot of the solutions on that page look like they work. The problem is, I don't know where I'm supposed to put the code that they wrote. I've tried putting it in the onCreate method and it doesnt work. The solution I'm planning on using is setting a static number to a different int every time a different item is selected and then smoothscrolling to that position when the page is created. Unfortunately, it doesnt seem to work if i just plop it into the onCreate method. Where am I supposed to put it? My code is below if you want to see it.

public class ElemSearch extends Periodic
{
private static int focusNum=22;
ListView mListView;
ArrayAdapter<String> adapter;

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.elem_search_layout);

    //This block is for the titlebar
    ImageButton back = (ImageButton) findViewById(R.id.back_button);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ElemSearch.this, Periodic.class);
            startActivity(intent);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);
            v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        }
    });
    //End titlebar block

    //Search Bar code
    final EditText inputSearch;
    inputSearch = (EditText) findViewById(R.id.editText);
    //Search Bar code continues later

    Resources res = getResources();
    final String[] elements = res.getStringArray(R.array.elements);

    String[] myKeys = getResources().getStringArray(R.array.elements);
    mListView = (ListView)findViewById(R.id.elemList);

    adapter= new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name,elements );
    mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myKeys));
    final List<String> elemList = Arrays.asList(getResources().getStringArray(R.array.elements));

    //Search Bar code continues
    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            ElemSearch.this.adapter.getFilter().filter(cs);

            ArrayList<String> temp = new ArrayList<String>();
            int textlength = inputSearch.getText().length();
            temp.clear();
            for (int i = 0; i < elemList.size(); i++) {
                if (textlength <= elemList.get(i).length()) {
                    if (inputSearch.getText().toString().equalsIgnoreCase(
                            (String)
                                    elemList.get(i).subSequence(0,
                                            textlength))) {
                        temp.add(elemList.get(i));
                    }
                }
            }
            mListView.setAdapter(new ArrayAdapter<String>(ElemSearch.this, android.R.layout.simple_list_item_1, temp));
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

    //mListView is list view type
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            //note that because arrays make zero sense and start at 0, the position is one int less than
            //the element's atomic number.
            initializeList(position);
            //Intent n = new Intent(getApplicationContext(), MainActivity.class);
            //n.putExtra("position", position);
            //startActivity(n);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);
            mListView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

        }

    });

    ImageButton toTop = (ImageButton) findViewById(R.id.topButton);
    toTop.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            mListView.smoothScrollToPosition(0);
            mListView.setFastScrollEnabled(true);
            v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        }
    });
}

public void initializeList(int position)
{
    Intent n;
    if(position==0)
    {
        n = new Intent(getApplicationContext(), Hydrogen.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=0;
    }
    if(position==1)
    {
        n = new Intent(getApplicationContext(), Helium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=1;
    }
    if(position==2)
    {
        n = new Intent(getApplicationContext(), Lithium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=2;
    }
    if(position==3)
    {
        n = new Intent(getApplicationContext(), Beryllium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=3;
    }
    if(position==4)
    {
        n = new Intent(getApplicationContext(), Boron.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=4;
    }
    if(position==5)
    {
        n = new Intent(getApplicationContext(), Carbon.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=5;
    }
    if(position==6)
    {
        n = new Intent(getApplicationContext(), Nitrogen.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=6;
    }
    if(position==7)
    {
        n = new Intent(getApplicationContext(), Oxygen.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=7;
    }
    if(position==8)
    {
        n = new Intent(getApplicationContext(), Flourine.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=8;
    }
    if(position==9)
    {
        n = new Intent(getApplicationContext(), Neon.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=9;
    }
    if(position==10)
    {
        n = new Intent(getApplicationContext(), Sodium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=10;
    }
    if(position==11)
    {
        n = new Intent(getApplicationContext(), Magnesium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=11;
    }
    if(position==12)
    {
        n = new Intent(getApplicationContext(), Aluminum.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=12;
    }
    if(position==13)
    {
        n = new Intent(getApplicationContext(), Silicon.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=13;
    }
    if(position==14)
    {
        n = new Intent(getApplicationContext(), Phosphorus.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=14;
    }
    if(position==15)
    {
        n = new Intent(getApplicationContext(), Sulfur.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=15;
    }
    if(position==16)
    {
        n = new Intent(getApplicationContext(), Chlorine.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=16;
    }
    if(position==17)
    {
        n = new Intent(getApplicationContext(), Argon.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=17;
    }
    if(position==18)
    {
        n = new Intent(getApplicationContext(), Potassium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=18;
    }
    if(position==19)
    {
        n = new Intent(getApplicationContext(), Calcium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==20)
    {
        n = new Intent(getApplicationContext(), Scandium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==21)
    {
        n = new Intent(getApplicationContext(), Titanium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==22)
    {
        n = new Intent(getApplicationContext(), Vanadium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==23)
    {
        n = new Intent(getApplicationContext(), Chromium.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==24)
    {
        n = new Intent(getApplicationContext(), Manganese.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==25)
    {
        n = new Intent(getApplicationContext(), Iron.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==26)
    {
        n = new Intent(getApplicationContext(), Cobalt.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==27)
    {
        n = new Intent(getApplicationContext(), Nickel.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==28)
    {
        n = new Intent(getApplicationContext(), Copper.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
    if(position==29)
    {
        n = new Intent(getApplicationContext(), Zinc.class);
        n.putExtra("position", position);
        startActivity(n);
        focusNum=position;
    }
}

@Override
protected void onResume()
{
    super.onResume();
    mListView.smoothScrollToPosition(focusNum);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Community
  • 1
  • 1
Luke Wang
  • 133
  • 9

1 Answers1

0

OK, so I managed to fix this problem after messing around for quite some time. My fix isn't 100% optimal as I never found the core of the problem, but hey, the solution works so here we go. The idea is to have a static number that is set to a coord on the list everytime a list item is selected. Then in the onCreate method, use setSelection(focusNum) rather than smoothScrollTo(focusNum). It works perfectly for me.

Luke Wang
  • 133
  • 9