从另一个Activity删除列表项后如何更新我的ListView?

时间:2016-01-31 09:15:17

标签: android sqlite listview

我在删除功能中遇到问题。当我点击删除时,它可以工作并删除sqlite中的记录。但问题是在删除之后,它会跳回到使用listview的上一个活动。但删除的记录仍然出现在那里。我需要从listview活动中退出并再次返回它只是消失。 我该怎么办?

这是我在数据库中的删除功能

public Integer deleteData (String property)
{
    SQLiteDatabase db = this.getReadableDatabase();
    return db.delete(houseContract.houseEntry.table2_name,"Property = ?",new String[] {property});
}

这是我的删除按钮适用于活动

public class SellerHouseDetail extends AppCompatActivity {
EditText etAddress,etDetail,etShowID;
TextView txType,txProperty,txPrice,txState,txTitle,txOther,txSize,txFullname;
Button bDelete;
String type,property,price,state,address,title,other,size,detail,fullname;
HousesDB db = new HousesDB(this);
Houses houses;

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

    txType = (TextView) findViewById(R.id.txTypes);
    txProperty = (TextView) findViewById(R.id.txProperty);
    txPrice = (TextView) findViewById(R.id.txPrice);
    txState = (TextView) findViewById(R.id.txState);
    etAddress = (EditText) findViewById(R.id.etAddress);
    txTitle = (TextView) findViewById(R.id.txTitle);
    txOther = (TextView) findViewById(R.id.txOther);
    txSize = (TextView) findViewById(R.id.txSize);
    txFullname = (TextView) findViewById(R.id.txFullname);
    etDetail = (EditText) findViewById(R.id.etDetail);
    etShowID = (EditText) findViewById(R.id.etShowID);
    bDelete = (Button) findViewById(R.id.bDelete);

    fullname = txFullname.getText().toString();
    type = txType.getText().toString();
    property = txProperty.getText().toString();
    price = txPrice.getText().toString();
    state = txState.getText().toString();
    address = etAddress.getText().toString();
    title = txTitle.getText().toString();
    other = txOther.getText().toString();
    size = txSize.getText().toString();
    detail = etDetail.getText().toString();

    Intent i = getIntent();
    property = i.getStringExtra("house_property");
}

public void onResume()
{
    super.onResume();
    txProperty.setText(property);

    houses = db.getInfo(property);

    txType.setText(houses.getTypes());
    txFullname.setText(houses.getFullname());
    txPrice.setText(houses.getPrice());
    txState.setText(houses.getState());
    etAddress.setText(houses.getAddress());
    txTitle.setText(houses.getTitle());
    txOther.setText(houses.getOther());
    txSize.setText(houses.getSize());
    etDetail.setText(houses.getDetail());

    DeleteData();
}
public void DeleteData() {
    bDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(SellerHouseDetail.this);
            dialog.setTitle("Delete Record");
            dialog.setContentView(R.layout.activity_delete_layout);
            dialog.show();

            Button bYes = (Button) dialog.findViewById(R.id.bYes);
            Button bNo = (Button) dialog.findViewById(R.id.bNo);

            bYes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Integer deletedRows = db.deleteData(txProperty.getText().toString());
                    if(deletedRows > 0)
                    {
                        Toast.makeText(SellerHouseDetail.this,"House Deleted",Toast.LENGTH_LONG).show();
                        finish();
                    }
                    else
                    {
                        Toast.makeText(SellerHouseDetail.this,"House not Deleted",Toast.LENGTH_LONG).show();
                    }
                }
            });

            bNo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });
        }
    });
    }
}

这是我的listview活动有问题

public class SellerHouses extends ListActivity {
TextView house_property;
String fullname;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seller_houses);
    Intent i = getIntent();
    fullname = i.getStringExtra("fullname");
}
public void onResume()
{
    super.onResume();
    HousesDB list = new HousesDB(this);
    // list.insertProduct();
    ArrayList<HashMap<String, String>> houseList =  list.getSellerHouseList(fullname);
    if(houseList.size()!=0) {
        ListView lv = getListView();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                house_property = (TextView) view.findViewById(R.id.shouse_property);
                String houseproperty = house_property.getText().toString();

                //Go to House Detail

                Intent objIndent = new Intent(getApplicationContext(),SellerHouseDetail.class);
                objIndent.putExtra("house_property",houseproperty);
                startActivity(objIndent);
            }
        });
        ListAdapter adapter = new SimpleAdapter( SellerHouses.this,houseList, R.layout.activity_seller_house_info, new String[] { "id","property","type","state"}, new int[] {R.id.shouse_Id, R.id.shouse_property, R.id.shouse_type, R.id.shouse_state});
        setListAdapter(adapter);
    }else{
        Toast.makeText(this, "No Houses is found!", Toast.LENGTH_SHORT).show();
    }

    }
}

3 个答案:

答案 0 :(得分:1)

在这种情况下,您只能使用startActivityForResult方法

Intent objIndent = new Intent(getApplicationContext(),SellerHouseDetail.class);
     objIndent.putExtra("house_property",houseproperty);
            startActivityForResult(objIndent, 1);

如果您删除了某些内容,则可以从 SellerHouseDetail

中将结果发送为正常值
Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();

如果您没有删除任何内容,请设置RESULT_CANCELED

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

SellerHouses 中 覆盖onActivityResult()方法

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

 if(resultCode == RESULT_OK){      
     //Update your listview like
    ListAdapter adapter = new SimpleAdapter( SellerHouses.this,houseList, R.layout.activity_seller_house_info, new String[] { "id","property","type","state"}, new int[] {R.id.shouse_Id, R.id.shouse_property, R.id.shouse_type, R.id.shouse_state});
    setListAdapter(adapter);
 }
 if (resultCode == RESULT_CANCELED) {    
     //You don't need to refresh
  }
 }
}

注意:我建议您解压缩代码以使数据适应您的listview,方法如下面的refreshListView

 public void refreshListView( //Required parameters if necessary) {

ListAdapter adapter = new SimpleAdapter( SellerHouses.this,houseList, R.layout.activity_seller_house_info, new String[] { "id","property","type","state"}, new int[] {R.id.shouse_Id, R.id.shouse_property, R.id.shouse_type, R.id.shouse_state});
        setListAdapter(adapter);
 }

如果您需要刷新ListView,请调用此方法。

答案 1 :(得分:1)

问题在于此部分:

if(houseList.size()!=0) {
    ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

            house_property = (TextView) view.findViewById(R.id.shouse_property);
            String houseproperty = house_property.getText().toString();

            //Go to House Detail

            Intent objIndent = new Intent(getApplicationContext(),SellerHouseDetail.class);
            objIndent.putExtra("house_property",houseproperty);
            startActivity(objIndent);
        }
    });
    ListAdapter adapter = new SimpleAdapter( SellerHouses.this,houseList, R.layout.activity_seller_house_info, new String[] { "id","property","type","state"}, new int[] {R.id.shouse_Id, R.id.shouse_property, R.id.shouse_type, R.id.shouse_state});
    setListAdapter(adapter);
}else{
    Toast.makeText(this, "No Houses is found!", Toast.LENGTH_SHORT).show();
}

这样做:

ListView lv = getListView();
ListAdapter adapter = new SimpleAdapter( SellerHouses.this,houseList, R.layout.activity_seller_house_info, new String[] { "id","property","type","state"}, new int[] {R.id.shouse_Id, R.id.shouse_property, R.id.shouse_type, R.id.shouse_state});
setListAdapter(adapter);

if(houseList.size()!=0) {       
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            house_property = (TextView) view.findViewById(R.id.shouse_property);
            String houseproperty = house_property.getText().toString();

            //Go to House Detail

            Intent objIndent = new Intent(getApplicationContext(),SellerHouseDetail.class);
            objIndent.putExtra("house_property",houseproperty);
            startActivity(objIndent);
        }
    });

}else{
    Toast.makeText(this, "No Houses is found!", Toast.LENGTH_SHORT).show();
}

你很好:), 虽然这不是优化的方法,但现在它将解决您的问题。

答案 2 :(得分:0)

试试这个

    ListAdapter adapter = new SimpleAdapter( SellerHouses.this,houseList, R.layout.activity_seller_house_info, new String[] { "id","property","type","state"}, new int[] {R.id.shouse_Id, R.id.shouse_property, R.id.shouse_type, R.id.shouse_state});
    setListAdapter(adapter);
    adapter.notifyDataSetChanged() //refresh listview