如何刷新ListView以处理数据?喜欢 - 插入,更新和放大删除

时间:2017-06-22 13:15:37

标签: android listview

我正在尝试刷新我的listview,每次插入,更新,从/进入sqlite数据库中删除数据。但是,它无法正常工作。我已经使用了finishActivityFromChild(getParent(),1);但有时我的应用程序无法打开。 我如何使用notifyDataSetChanged();对于这个代码或任何方式?

public class MainActivity extends AppCompatActivity {

DbHelper myDB;
Button btn_feedback;
FloatingActionButton btn_fab;
private String selectedName;
private int selectedID;
private boolean isUserClickedBackButton = false;

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

    myDB = new DbHelper(this);
    final ListView listView = (ListView)findViewById(R.id.listview_main);
    registerForContextMenu(listView);

    Intent receivedIntent = getIntent();
    selectedID = receivedIntent.getIntExtra("id",-1);
    selectedName = receivedIntent.getStringExtra("item");

    final ArrayList<String> theList = new ArrayList<>();
    Cursor data = myDB.getListContents();

    if (data.getCount() == 0){
        Toast.makeText(getApplicationContext(),"Data Empty !",Toast.LENGTH_SHORT).show();
    }
    else {
        while (data.moveToNext()){
            theList.add(data.getString(1));
            ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
            listView.setAdapter(listAdapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                    String item1 = parent.getItemAtPosition(position).toString();
                    Toast.makeText(getApplicationContext(),"You Select - " + theList.get(position),Toast.LENGTH_SHORT).show();

                    Cursor data = myDB.getItemID(item1);
                    int itemID = -1;
                    while(data.moveToNext()){
                        itemID = data.getInt(0);
                    }
                    if(itemID > -1){
                        Intent editScreenIntent = new Intent(getApplicationContext(), Edit_Main.class);
                        editScreenIntent.putExtra("id",itemID);
                        editScreenIntent.putExtra("ITEM1",item1);
                        startActivity(editScreenIntent);
                    }
                    else{
                        Toast.makeText(getApplicationContext(),"No ID Associated With That Name !",Toast.LENGTH_SHORT);
                    }
                }
            });
        }
    }

    btn_fab = (FloatingActionButton)findViewById(R.id.fab);
    btn_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), Create_Main.class));
        }
    });
}}

1 个答案:

答案 0 :(得分:-1)

在while循环中清除你的arrayList,如下所示:

     while (data.moveToNext()){

//////////////////////////////////////////////////
            if(theList!=null && theList.size()>0){
               theList.clear();
            }
/////////////////////////////////////
            theList.add(data.getString(1));
            .....
            .....
}

希望,这有帮助。