简体   繁体   English

Android 列表查看项目 position

[英]Android listview item position

in my app there is a listview.在我的应用程序中有一个列表视图。 When user clicks on an item, an alertdialog appears.当用户点击一个项目时,会出现一个警告对话框。 User chooses item "Delete" from the dialog and the item from the listview is removed.用户从对话框中选择项目“删除”,列表视图中的项目被删除。 Problem is, the position of the listview is always null in my code, so the remove command acts very strange.问题是,列表视图的 position 在我的代码中始终是 null,因此删除命令的行为非常奇怪。 In the code below, when user clicks on Delete in the Alert dialog, the lines of a file are read into an array, and i want to delete the item i selected in the listview.在下面的代码中,当用户在警报对话框中单击删除时,文件的行被读入数组,我想删除我在列表视图中选择的项目。 For this i am using the position variable of the onitemclick().为此,我使用 onitemclick() 的 position 变量。

lv1.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> a, View v, final int position, long id) {

     Object o = lv1.getItemAtPosition(position);
     keyword = o.toString();
     AlertDialog.Builder builder2 = new AlertDialog.Builder(Bind.this);
         builder2.setTitle("Select");
        final CharSequence[] selection2 = select.toArray(new CharSequence[valasztas.size()]);
         builder2.setItems(selection2, new DialogInterface.OnClickListener()
         {
            public void onClick(DialogInterface dialog, int item2)
            {       
                if (selection2[item2].equals("Delete"))
                { 
                    try { 
                         File rootdir = Environment.getExternalStorageDirectory();
                         File yourFile = new File(rootdir, "tomato50.txt");
                         FileReader filereader = new FileReader(yourFile);
                            BufferedReader br = new BufferedReader(filereader);
                            String line; 
                            while((line = br.readLine()) != null) 
                        {
                              assignArrT.add(line);

                        } 
                               br.close();
                    }
                      catch (IOException e) 
                      { 
                          e.printStackTrace(); 
                      }
                assignArrT.remove(position);  //this is null

position is an int, it cannot be null. position 是一个 int,它不能是 null。 If you are getting a null pointer on that line, it must be assignArrT that is null.如果您在该行上获得 null 指针,则它必须是 assignArrT,即 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM