更改后刷新列表视图

时间:2016-04-06 15:41:35

标签: android listview

我写了一个包含listview和FAB的活动。我的问题是我在FAB的帮助下输入了一组新的行,这会将我带到另一个活动,但在输入listView的新行后,listView不会刷新。我需要关闭应用并重新启动以查看更改,但我不知道在哪里使用方法notifyDataSetChanged()

我将值传递给列表,并且我指定了编写适配器类的整个类。

public class ListDataAdapter1 extends ArrayAdapter {
    List list = new ArrayList<>();

    public ListDataAdapter1(Context context, int resource) {
        super(context, resource);
    }

    static class LayoutHandler {
        TextView ID, TYPE, AREA, LVLOE;
    }

    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutHandler layoutHandler;

        if(row == null)
        {
            LayoutInflater layoutinflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutinflater.inflate(R.layout.row_layout1,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.ID = (TextView) row.findViewById(R.id.id);
            layoutHandler.TYPE = (TextView) row.findViewById(R.id.type);
            layoutHandler.AREA = (TextView) row.findViewById(R.id.area);
            layoutHandler.LVLOE = (TextView) row.findViewById(R.id.lvlofe);
            row.setTag(layoutHandler);
        }
        else
        {
            layoutHandler = (LayoutHandler) row.getTag();
        }

        DataProvider1 dataProvider1 = (DataProvider1) this.getItem(position);
        layoutHandler.ID.setText(dataProvider1.getId1());
        Log.e(dataProvider1.getId1(), "Name of the value passed");
        layoutHandler.TYPE.setText(dataProvider1.getType1());
        layoutHandler.AREA.setText(dataProvider1.getArea1());
        layoutHandler.LVLOE.setText(dataProvider1.getLvloe1());

        return row;
    }
}

public class Addnew extends AppCompatActivity {
EditText ID,AREA,LVLOE;
String id,type,area,lvloe;
Button b1;
Context ctx=this;
Spinner SPINNER;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_new);
    setTitle("Add new entry ");
    ID = (EditText) findViewById(R.id.txtid);
    SPINNER = (Spinner) findViewById(R.id.spntype);
    adapter = ArrayAdapter.createFromResource(this,R.array.type_animals,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    SPINNER.setAdapter(adapter);
    SPINNER.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            type = parent.getItemAtPosition(position).toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    AREA = (EditText) findViewById(R.id.txtarea);
    LVLOE = (EditText) findViewById(R.id.txtlvlofe);
    b1= (Button) findViewById(R.id.adden);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            id = ID.getText().toString();

            area = AREA.getText().toString();
            lvloe = LVLOE.getText().toString();
            DbOp db = new DbOp(ctx);
            db.putInformation1(db, id, type, area, lvloe);


            Toast.makeText(getBaseContext(), "Sucessfully registered", Toast.LENGTH_LONG).show();

            finish();

        }
    });

}

}

public class DataProvider1 {
private String id1;
private String type1;
private String area1;
private String lvloe1;

public String getId1() {
    return id1;
}

public void setId1(String id1) {
    this.id1 = id1;
}

public String getType1() {
    return type1;
}

public void setType1(String type1) {
    this.type1 = type1;
}

public String getLvloe1() {
    return lvloe1;
}

public void setLvloe1(String lvloe1) {
    this.lvloe1 = lvloe1;
}

public String getArea1() {
    return area1;
}

public void setArea1(String area1) {
    this.area1 = area1;
}

public DataProvider1(String id1,String type1,String area1,String lvloe1 )
{
    this.id1 = id1;
    this.type1 = type1;
    this.area1 = area1;
    this.lvloe1 = lvloe1;

}

}

ListView listView;     EditText视图;

SQLiteDatabase sqLiteDatabase;
DbOp dbOp;
Cursor cursor;
ListDataAdapter1 listDataAdapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity);
    setTitle("Home ");

    listView = (ListView) findViewById(R.id.listView1);
    listDataAdapter1 = new ListDataAdapter1(getApplicationContext(),R.layout.row_layout);
    listView.setAdapter(listDataAdapter1);
    dbOp = new DbOp(getApplicationContext());
    sqLiteDatabase = dbOp.getReadableDatabase();
    cursor = dbOp.getInformation1(sqLiteDatabase);
    if(cursor.moveToFirst())
    {
        do{
            String id,type,area,lvlofe;
            id = cursor.getString(0);
            type = cursor.getString(1);
            area = cursor.getString(2);
            lvlofe = cursor.getString(3);
            DataProvider1 dataProvider1 = new DataProvider1(id,type,area,lvlofe);
            listDataAdapter1.add(dataProvider1);



        }while(cursor.moveToNext());
    }


  }
public void addnew(View view)
{
    Intent intent=new Intent(this,Addnew.class);
    startActivity(intent);
}
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.mymenu, menu);
    return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId()) {
        case R.id.update:
            Intent intent1 = new Intent(this,UpdateActivity1.class);
            startActivity(intent1);
            return super.onOptionsItemSelected(item);


        case R.id.delete:
            Intent intent= new Intent(this,DeleteEntry_Activity.class);
            startActivity(intent);
            return super.onOptionsItemSelected(item);


        /*case R.id.logout:
            Intent intent1 = new Intent(this,LoginActivity.class);
            startActivity(intent1);*/
            default:
            return super.onOptionsItemSelected(item);
    }
}

}

2 个答案:

答案 0 :(得分:2)

在此处使用notifyDataSetChanged

public void add(Object object){
        super.add(object);
        list.add(object);
        notifyDataSetChanged();
    }

<强>更新

也改变您的onClick方法:

public void onClick(View v) {
        id = ID.getText().toString();

        area = AREA.getText().toString();
        lvloe = LVLOE.getText().toString();
        DbOp db = new DbOp(ctx);
        adapter.add(id+" "+area+" "+lvloe);
        adapter.notifyDataSetChanged();
        db.putInformation1(db, id, type, area, lvloe);


        Toast.makeText(getBaseContext(), "Sucessfully registered", Toast.LENGTH_LONG).show();

        finish();

    }

答案 1 :(得分:-1)

在活动的onresume方法中使用此代码

dbOp = new DbOp(getApplicationContext());
sqLiteDatabase = dbOp.getReadableDatabase();
cursor = dbOp.getInformation1(sqLiteDatabase);
if(cursor.moveToFirst())
{
    do{
        String id,type,area,lvlofe;
        id = cursor.getString(0);
        type = cursor.getString(1);
        area = cursor.getString(2);
        lvlofe = cursor.getString(3);
        DataProvider1 dataProvider1 = new     DataProvider1(id,type,area,lvlofe);
        if(listDataAdapter1!=null){
        listDataAdapter1.add(dataProvider1);
 }else{
        listDataAdapter1 = new      ListDataAdapter1(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdapter1);
}


    }while(cursor.moveToNext());
}
cursor.close();

并在适配器类

中的add函数中调用notifyDataSetChange()