开发者

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. 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().

lv1.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?&g开发者_StackOverflowt; 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. If you are getting a null pointer on that line, it must be assignArrT that is null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜