2017-06-22 39 views
-1

我想刷新我的listview每插入,更新,从/删除数据到sqlite数据库。但它不能工作。我已经使用finishActivityFromChild(getParent(),1);但有时我的应用程序无法打开。我如何使用notifyDataSetChanged();为此代码或任何方式?如何刷新我的ListView以处理数据?像 - 插入,更新和删除

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)); 
     } 
    }); 
}} 
+0

用户notifydatasetchanged()方法。在你的数组列表中进行修改之后。 –

+0

如果您不想通知整个列表,则在适配器内使用“notifyItemChange(int position)”或“notifyItemInserted”方法 –

回答

-1

只需清除您的ArrayList如下while循环中:

 while (data.moveToNext()){ 

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

希望,它帮助。

相关问题