在AlertDialog解除后更新片段中的ListView

时间:2018-02-24 17:37:35

标签: java android listview fragment

我的Fragment包含ListView。我正在使用对话框向数据库添加一些值,并且我想在解除对话框后更新此ListView。此外,当我更改标签时,ListView未更新,但是当应用程序打开和关闭时,ListView会更新。

FragmentDialog类如下:

片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_teams, container, false);

    listViewTeams = rootView.findViewById(R.id.listView_teams);

    TeamDatabase teamDatabase = new TeamDatabase(getContext());
    teamDatabase.open();
    arrayListTeam = teamDatabase.getAllTeams();
    teamDatabase.close();

    int resID = R.layout.team_list_item;
    teamListArrayAdapter = new TeamListArrayAdapter(getContext(), resID, arrayListTeam);
    listViewTeams.setAdapter(teamListArrayAdapter);

    return rootView;
}

对话框onClick方法:

@Override
public void onClick(View view) {
   int id = view.getId();

   switch (id){
       case R.id.button_alertDialogAddTeam_cancel:
           this.dismiss();
           break;
       case R.id.button_alertDialogAddTeam_ok:
           Team team = new Team();
           team.setName(editTextTeamName.getText().toString());
           team.setCode(editTextTeamCode.getText().toString());

           TeamDatabase teamDatabase = new TeamDatabase(getContext());
           teamDatabase.open();
           if(teamDatabase.addNewTeam(team)) {
               Toast.makeText(getContext(), team.getCode() + " - " +
                       team.getName() + " was added successfully", Toast.LENGTH_SHORT).show();
           }

           this.dismiss();
           break;
   }
}

TeamDatabase类:

    public static final String TABLE_NAME = "team";

private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_CODE = "code";
private static final String KEY_EMBLEM = "emblem";

private Context context;

public static final String CREATE_TABLE = "CREATE TABLE "+
        TABLE_NAME + " ("+
        KEY_ID  + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "+
        KEY_NAME + " TEXT NOT NULL, "+
        KEY_CODE + " TEXT NOT NULL, " +
        KEY_EMBLEM + " TEXT);";


public TeamDatabase(Context context) {
    super(context);
    this.context = context;
}

public boolean addNewTeam(Team team){
    ContentValues contentValues = new ContentValues();

    contentValues.put(KEY_NAME, team.getName());
    contentValues.put(KEY_CODE, team.getCode());

    return db.insert(TABLE_NAME, null, contentValues) > 0;
}

public ArrayList<Team> getAllTeams()
{
    ArrayList<Team> teams = new ArrayList<Team>();

    Cursor cursor = db.query(TABLE_NAME, new String[]{KEY_ID,
            KEY_NAME,
            KEY_CODE}, null, null, null, null, null);

    while(cursor.moveToNext()) {

        Team team = new Team();

        team.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
        team.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
        team.setCode(cursor.getString(cursor.getColumnIndex(KEY_CODE)));

        teams.add(team);
    }
    return teams;
}

DatabaseHelper类:

private static final String DATABASE_NAME = "fixtureCreator.db";
private static final int DATABASE_VERSION = 1;

public SQLiteDatabase db;

public DatabaseHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(TeamDatabase.CREATE_TABLE);
}



@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersinon) {
    Log.w("TaskDBAdapter", "Upgrading from version " +
            oldVersion + " to " + newVersinon + ", which will destroy all old data");

    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TeamDatabase.TABLE_NAME);
    onCreate(sqLiteDatabase);
}

public DatabaseHelper open() throws SQLException {
    try{
        db = this.getWritableDatabase();
    }catch (SQLException e){
        db = this.getReadableDatabase();
    }
    return this;
}

public void close(){
    db.close();
}

3 个答案:

答案 0 :(得分:2)

点击第一个活动中的项目后,使用startActivityForResult()

开始第二个活动

然后在第二个活动中,在解除对话框后,在该按钮调用的onClick中,

intent.putExtra("new data", "item text");
setResult(RESULT_OK, intent);
finish();

现在您回到第一个活动,在这里您必须实施onActivityResult()回调。

您可以从该intent的附加内容中提取数据,并在数组中设置相应的项目并调用notifyDataSetChanged()。

理想情况下,你应该这样做。

答案 1 :(得分:1)

您应该在取消拨号之前调用notifyDataSetChanged

<强> teamListArrayAdapter.notifyDataSetChanged();

您应该更改下面的代码

   @Override
public void onClick(View view) {
   int id = view.getId();

   switch (id){
       case R.id.button_alertDialogAddTeam_cancel:
           this.dismiss();
           break;
       case R.id.button_alertDialogAddTeam_ok:
           Team team = new Team();
           team.setName(editTextTeamName.getText().toString());
           team.setCode(editTextTeamCode.getText().toString());

           TeamDatabase teamDatabase = new TeamDatabase(getContext());
           teamDatabase.open();
           if(teamDatabase.addNewTeam(team)) {
               Toast.makeText(getContext(), team.getCode() + " - " +
                       team.getName() + " was added successfully", Toast.LENGTH_SHORT).show();
           }
           arrayListTeam = teamDatabase.getAllTeams();
           teamListArrayAdapter.notifyDataSetChanged();
           this.dismiss();
           break;
   }
}

答案 2 :(得分:0)

在onCreateView中添加此代码:

teamListArrayAdapter.notifyDataSetChanged();