Android 将筛选器应用于列表后,TextWatcher和ListView元素的位置出现问题

Android 将筛选器应用于列表后,TextWatcher和ListView元素的位置出现问题,android,android-listview,android-adapter,textwatcher,android-filterable,Android,Android Listview,Android Adapter,Textwatcher,Android Filterable,我正在开发一个android应用程序,它显示一个包含SQL数据库数据的列表视图 单击列表视图元素后,将打开一个新活动,其中包含有关该元素的信息 我已经实现了一个Textwatcher来在列表中进行搜索,并且该列表会按照应该的方式进行更新 问题是,当我单击更新列表的列表元素时,它会打开一个包含旧位置数据的活动,例如单击Zinn ID:939。它将打开一个新的活动,其中包含Altbach ID:940数据。 在TextWatcher工作并将其传递到onItemClick方法后,更新元素位置的最佳

我正在开发一个android应用程序,它显示一个包含SQL数据库数据的列表视图

单击列表视图元素后,将打开一个新活动,其中包含有关该元素的信息

我已经实现了一个
Textwatcher
来在列表中进行搜索,并且该列表会按照应该的方式进行更新

问题是,当我单击更新列表的列表元素时,它会打开一个包含旧位置数据的活动,例如单击Zinn ID:939。它将打开一个新的活动,其中包含Altbach ID:940数据。 在
TextWatcher
工作并将其传递到
onItemClick
方法后,更新元素位置的最佳方法是什么?或者,在
TextWatcher
工作后,如何获得更新的列表? 提前谢谢你

那是我的班级

    public class LibraryActivity extends AppCompatActivity implements View.OnClickListener {

    static String db_name = "test_stolpersteine_db.sqlite";
    StolpersteineDAO stolpersteinedao;
    List<Stolpersteine> stolpersteine_list;
    ListView list_view;
    private ArrayAdapter<CharSequence> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library);

        ////////////////////////////// Database ////////////////////////////////////////////////////
        final File dbFile = this.getDatabasePath(db_name); // we create a database path

        if (!dbFile.exists()) {     // we checking if the directory is existing
            try {
                copyDatabaseFile(dbFile.getAbsolutePath());  // method to copy the database
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        ////// Database
        startDatabase();
        stolpersteine_list = stolpersteinedao.getAllStolpersteine();
        list_view = findViewById(R.id.list);
        adapter = createAdapterHtml(stolpersteine_list);
        list_view.setAdapter(adapter);
        ////////////////////////////// Database ////////////////////////////////////////////////////

        //////////////////////////// Buttons ///////////////////////////////////////////////////////
        Button button_home = findViewById(R.id.button_home);
        Button button_map = findViewById(R.id.button_map);
        Button button_library = findViewById(R.id.button_library);

        button_home.setOnClickListener(this);
        button_map.setOnClickListener(this);
        button_library.setOnClickListener(this);

        button_library.setEnabled(false);
        /////////////////////////// Buttons ////////////////////////////////////////////////////////

        EditText theFilter = findViewById(R.id.edit_text);

        theFilter.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                (LibraryActivity.this).adapter.getFilter().filter(charSequence);
            }
            @Override
            public void afterTextChanged(Editable charSequence) {
            }
        });

        //// OnClickListener
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String url = stolpersteine_list.get(position).getURL();
                Intent i = new Intent(getApplicationContext(), RecordActivity.class);

                i.putExtra("url", url);
                i.putExtra("first_name", stolpersteine_list.get(position).getFirst_Name());
                i.putExtra("name", stolpersteine_list.get(position).getName());
                startActivity(i);


            }  // end of listener

        });

    } // end of onCreate method
    

    ////////////////////////////// Database ////////////////////////////////////////////////////////
    private void copyDatabaseFile(String destinationPath) throws IOException {

        InputStream assetsDB = this.getAssets().open(db_name);
        OutputStream dbOut = new FileOutputStream(destinationPath);

        byte[] buffer = new byte[1024];

        int length;

        while ((length = assetsDB.read(buffer)) > 0) {

            dbOut.write(buffer, 0, length);
        }

        dbOut.flush();
        dbOut.close();
    } //end of copyDatabaseFile method


    private ArrayAdapter<CharSequence> createAdapterHtml(List<Stolpersteine> s_list) {

        Spanned[] html_array = new Spanned[s_list.size()];

        for (int i = 0; i < s_list.size(); i++) {
            html_array[i] = Html.fromHtml(
                    s_list.get(i).getName()
                    + " "
                    + "ID: "+ s_list.get(i).getID() + "<br></i>"

                    + s_list.get(i).getHouse_Number() +" "
                    + s_list.get(i).getHouse_Number_Extension() + "</i>");
        }

        ArrayAdapter<CharSequence> my_adapter =
                new ArrayAdapter<CharSequence>(this, R.layout.list_item, html_array);

        return my_adapter;

    } // end of createAdapterHtml

    private void startDatabase() {
        AppDatabase database = Room.databaseBuilder(this, AppDatabase.class, db_name)
                .allowMainThreadQueries()
                .build();

        stolpersteinedao = database.getStolpersteineDAO();

    } // end of startDatabase method
    ////////////////////////////// Database ////////////////////////////////////////////////////////

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button_home:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.button_map:
                startActivity(new Intent(this, MapboxActivity.class));
                break;
            case R.id.button_library:
                startActivity(new Intent(this, LibraryActivity.class));
                break;
        }

    }
} // end of class
公共类LibraryActivity扩展AppCompatActivity实现View.OnClickListener{
静态字符串db_name=“test_stolpersteine_db.sqlite”;
斯托尔珀斯坦道斯托尔珀斯坦道;
列表Stolperstein_列表;
列表视图列表视图;
专用阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_库);
//////////////////////////////数据库////////////////////////////////////////////////////
final File dbFile=this.getDatabasePath(db_name);//我们创建一个数据库路径
如果(!dbFile.exists()){//我们将检查目录是否存在
试一试{
copyDatabaseFile(dbFile.getAbsolutePath());//复制数据库的方法
}捕获(IOE异常){
e、 printStackTrace();
}
}
//////数据库
startDatabase();
stolpersteine_list=stolpersteinedao.getAllStolpersteine();
列表\视图=findViewById(R.id.list);
适配器=createAdapterHtml(stolpersteine_列表);
列表\视图.设置适配器(适配器);
//////////////////////////////数据库////////////////////////////////////////////////////
////////////////////////////钮扣///////////////////////////////////////////////////////
Button Button\u home=findViewById(R.id.Button\u home);
按钮地图=findViewById(R.id.Button地图);
Button Button_library=findViewById(R.id.Button_library);
按钮_home.setOnClickListener(此);
按钮映射。setOnClickListener(此);
button_library.setOnClickListener(此);
按钮库。设置启用(错误);
///////////////////////////钮扣////////////////////////////////////////////////////////
EditText过滤器=findviewbyd(R.id.edit\u text);
addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(CharSequence CharSequence、int start、int count、int after){
}
@凌驾
public void onTextChanged(CharSequence CharSequence、int start、int before、int count){
(LibraryActivity.this).adapter.getFilter().filter(charSequence);
}
@凌驾
public void PostTextChanged(可编辑字符序列){
}
});
////onclick侦听器
list_view.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串url=stolpersteine_list.get(position.getURL();
Intent i=新的Intent(getApplicationContext(),RecordActivity.class);
i、 putExtra(“url”,url);
i、 putExtra(“first_name”,stolpersteine_list.get(position.getFirst_name());
i、 putExtra(“name”,stolpersteine_list.get(position.getName());
星触觉(i);
}//侦听器结束
});
}//onCreate方法的结束
//////////////////////////////数据库////////////////////////////////////////////////////////
私有void copyDatabaseFile(字符串destinationPath)引发IOException{
InputStream assetsDB=this.getAssets().open(db\U名称);
OutputStream dbOut=新文件OutputStream(destinationPath);
字节[]缓冲区=新字节[1024];
整数长度;
而((长度=assetsDB.read(buffer))>0){
写入(缓冲区,0,长度);
}
dbOut.flush();
dbOut.close();
}//copyDatabaseFile方法的结尾
专用阵列适配器createAdapterHtml(列表s\U列表){
span[]html_数组=新的span[s_list.size()];
对于(int i=0;i”
+s_list.get(i).getHouse_Number()+“”
+s_list.get(i.getHouse_Number_Extension()+);
}
阵列适配我的适配器=
新的ArrayAdapter(this,R.layout.list_项,html_数组);
返回我的_适配器;
}//createAdapterHtml的结尾
私有void startDatabase(){
AppDatabase database=Room.databaseBuilder(此,AppDatabase.class,db\U名称)
.allowMainThreadQueries()
.build();
stolpersteinedao=database.getStolpersteineDAO();
}//startDatabase方法的结尾
//////////////////////////////数据库////////////////////////////////////////////////////////
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.button\u主页:
startActivity(新意图(this,MainActivity.class));
打破
案例R.id.按钮图:
startActivity(新意图(此,MapboxActivity.class));
打破
//// OnClickListener
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        
        // Getting the clicked item from the filtered list
        Stolpersteine filteredItem = (Stolpersteine) ((ListView) parent).getAdapter().getItem(position);

        // Getting the original position
        int originalPos = getList().indexOf(filteredItem);

        // Use originalPos instead of position in your code:
        
        String url = stolpersteine_list.get(originalPos).getURL();
        Intent i = new Intent(getApplicationContext(), RecordActivity.class);

        i.putExtra("url", url);
        i.putExtra("first_name", stolpersteine_list.get(originalPos).getFirst_Name());
        i.putExtra("name", stolpersteine_list.get(originalPos).getName());
        startActivity(i);


    }  // end of listener
});