我有一个使用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);
}
我的活动样本:
case 0
代码的工作原理除了case 1
case 2
之外等等,直到case 99
它的内容相同,即1。如何做到这一点,无论如何什么位置是指数的数字始终保持不变?
答案 0 :(得分:0)
不是假设每个元素将具有哪个位置,而是有两种选择:
adapter.getItem(position)
并从那里开始工作。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;
}