listview没有得到更新的android

时间:2014-03-23 23:54:42

标签: android listview

我是Android新手,正在尝试为旅行帮助创建应用。它有几个组件,包括 - >使用户能够自定义清单。除默认列表外,还可以添加和删除项目。

添加, 我在没有XML的类文件中使用动态布局。它完美地运作:)

要从列表中删除项目, 我创建了一个adpater,一个listview,并试图删除所选项目。在几个“祝酒”的帮助下,我能够推导出一个项目正在从列表中删除,但视图没有得到更新。

我已经检查并尝试了很多解决方案,但它们似乎都没有工作。我正在附加我的java文件,其中显示和自定义列表。

代码似乎有点长,但它很容易理解。任何帮助将不胜感激! :)

public class Dynamic extends ListActivity{
    public String[] A = new String[100];
    public String[] B = new String[100];
    public int j=0, m=0, b=0, tot1=0, tot2=0;
    public int a[]=new int[100];
    CheckBox c, cc;
    ArrayList<String> list2 = new ArrayList<String>();
    ArrayList<String> list4 = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter2;
    private SparseBooleanArray sba; 

    @Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            ScrollView sv = new ScrollView(this);
        final LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        final ListView list3=new ListView(this);
        list3.setId(android.R.id.list);
        list3.setChoiceMode(list3.CHOICE_MODE_MULTIPLE);

        ll.addView(list3);
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list2);

            Bundle extras = getIntent().getExtras();
        if (extras != null) {
            A = extras.getStringArray("var");
            int i = extras.getInt("var2");
            B = extras.getStringArray("var3");
            int k = extras.getInt("var4");
                        for (j=0;j<i;j++)
            {
                cc = new CheckBox(this);
                cc.setText(A[j]);
                ll.addView(cc); 
                list2.add(A[j]);
                            adapter.notifyDataSetChanged();
            }
            for (m=0;m<k;m++)
            {
                cc = new CheckBox(this);
                cc.setText(B[m]);
                ll.addView(cc);
                list2.add(B[m]);
                            adapter.notifyDataSetChanged();
            }
        }
                    Button b = new Button(this);
            b.setText("Delete Item");
            ll.addView(b);
            b.setOnClickListener(new View.OnClickListener() 
            {   
            @Override
                public void onClick(View v) 
            {
                            AlertDialog.Builder alertDialog = new AlertDialog.Builder(Dynamic.this);
                            alertDialog.setTitle("Confirm Delete...");
                            alertDialog.setMessage("Are you sure you want to delete the selected item from the list?");
                            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() 
            {
                                 public void onClick(DialogInterface dialog,int which) 
                 {
                     sba=new SparseBooleanArray();
                     sba.clear();
                     sba=list3.getCheckedItemPositions();
                     ListView lv = getListView();   
                                     Toast.makeText(getApplicationContext(), "checked " + sba, Toast.LENGTH_SHORT).show();
                     int itemCount = getListView().getCount();
                 Toast.makeText(getApplicationContext(), "calc done " + itemCount, Toast.LENGTH_SHORT).show();
                                     for(int i=itemCount-1; i >= 0; i--){
                                         Toast.makeText(getApplicationContext(), "in the loop " + i, Toast.LENGTH_SHORT).show();
                                         Toast.makeText(getApplicationContext(), " " + sba.get(i), Toast.LENGTH_SHORT).show();
                         list2.add((list2.get(i))); 
                         Toast.makeText(getApplicationContext(), " " + list2.get(i), Toast.LENGTH_SHORT).show();
                         adapter.remove(list2.get(i));
                                         list3.invalidate();
                         adapter.notifyDataSetChanged();
                         list3.setAdapter(adapter);
                                         Toast.makeText(getApplicationContext(), " " + list2, Toast.LENGTH_SHORT).show();
                                         }
                                     sba.clear();
                                     }
                });     
                                alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                                    Toast.makeText(getApplicationContext(), "The item has NOT been deleted!", Toast.LENGTH_SHORT).show();
                    dialog.cancel();
                                    }
                               });
                               alertDialog.show();
               setListAdapter(adapter);
              }
        });     
        this.setContentView(sv);
  }
}

1 个答案:

答案 0 :(得分:0)

为了更新您的listView,请在您的adampter中尝试此操作

notifyDataSetChanged();

但是你应该在UI线程上运行它。在UI线程中创建一个处理程序,然后将Runable发布到它

像这样

private class Asyn_SaveData extends AsyncTask<Void, Void, Void> {       

    @Override
    protected Void doInBackground(Void... params) {
        //Do something here that will run in backGround 
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //and this function is called automatically by doInBackground
                    // after it finish its work
        //in your example refresh your ListView
        notifyDataSetChanged();
    }
}

并使用AsyncTask

 new Asyn_SaveData().execute(null,null,null);

只需记住AsyncTask必须是subClassed

myListView.invalidateViews();