如何刷新我的列表视图以操作数据?喜欢 - 插入,更新和删除



我正在尝试为每个插入,更新,删除从/中删除sqlite database.ut的listView。但是,它无法正常工作。我已经使用了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));
        }
    });
}}

只需清除您的阵列列表,而循环如下:

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

希望,它会有所帮助。

相关内容

最新更新