在ListView中使用getFilter时,SharedPreference获取错误的索引

时间:2013-12-14 20:37:43

标签: java android listview android-listview

我有一个使用ArrayAdapter的ListView,我还实现了getFilter函数在ListView内进行搜索。我使用SharedPreference来获取ListView的索引,以显示有关下一个Activity的一些信息。搜索时,如果索引不同,则会导致我的应用显示错误的信息。

case 0:
                    tvT = (TextView) findViewById(R.id.txtTitle);
                    strT = tvT.getText().toString();
                    inPos = strT.indexOf(".");
                    strT = strT.substring(0, inPos);
                    inPos = Integer.valueOf(strT);
                    Toast.makeText(getApplicationContext(), String.valueOf(inPos), 2000).show();
                    //moveToActivity(String.valueOf(inPos-1)); //0
                    break;
                case 1:
                    tvT = (TextView) findViewById(R.id.txtTitle);
                    strT = tvT.getText().toString();
                    inPos = strT.indexOf(".");
                    strT = strT.substring(0, inPos);
                    inPos = Integer.valueOf(strT);
                    Toast.makeText(getApplicationContext(), String.valueOf(inPos), 2000).show();
                    //moveToActivity("1"); //1
                    break;
                case 2:
                    tvT = (TextView) findViewById(R.id.txtTitle);
                    strT = tvT.getText().toString();
                    inPos = strT.indexOf(".");
                    strT = strT.substring(0, inPos);
                    inPos = Integer.valueOf(strT);
                    Toast.makeText(getApplicationContext(), String.valueOf(inPos), 2000).show();
                    //moveToActivity("2"); //2
                    break;
                case 3:
                    tvT = (TextView) findViewById(R.id.txtTitle);
                    strT = tvT.getText().toString();
                    inPos = strT.indexOf(".");
                    strT2 = strT.substring(0, inPos);
                    inPos = Integer.valueOf(strT2);
                    Toast.makeText(getApplicationContext(), strT, 2000).show();
                    //moveToActivity("3"); //3
                    break;
                case 4:
                    tvT = (TextView) findViewById(R.id.txtTitle);
                    strT = tvT.getText().toString();
                    inPos = strT.indexOf(".");
                    strT2 = strT.substring(0, inPos);
                    inPos = Integer.valueOf(strT2);
                    Toast.makeText(getApplicationContext(), String.valueOf(inPos), 2000).show();
                    //moveToActivity("4"); //4
                    break;


public void moveToActivity(String strWhich) {
        editor.putString("NamePosition", strWhich);
        editor.commit();
        Intent myIntent = new Intent(MainActivity.this, NameDisplay.class);
        startActivityForResult(myIntent, 0);
        overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
    }

我的活动样本:

enter image description here

case 0代码的工作原理除了case 1 case 2之外等等,直到case 99它的内容相同,即1。如何做到这一点,无论如何什么位置是指数的数字始终保持不变?

2 个答案:

答案 0 :(得分:0)

不是假设每个元素将具有哪个位置,而是有两种选择:

  1. 使用adapter.getItem(position)并从那里开始工作。
  2. 实施adapter.getItemId(position)并切换项目的ID,而不是位置(您的适配器必须实现getItemId(int position)

答案 1 :(得分:0)

我用以下内容修复了它:

String check = String.valueOf(dataList.getItemAtPosition(position));
int pos = check.indexOf(".");
int pos2 = check.indexOf(" ");
String strP = check.substring(pos2+1, pos);
int pos3 = Integer.valueOf(strP) - 1;

switch(position) {
    case 0:
    Log.i("pos", String.valueOf(pos));
    Log.i("pos2", String.valueOf(pos2));
    Log.i("strP", strP);
    Log.i("check", check);
    moveToActivity(String.valueOf(pos3)); //0
    break;
    case 1:
        moveToActivity(String.valueOf(pos3)); //1
        break;
    case 2:
        moveToActivity(String.valueOf(pos3)); //2
        break;
    case 3:
        moveToActivity(String.valueOf(pos3)); //3
        break;
}