Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在从其他活动中删除列表项后更新我的ListView?_Android_Sqlite_Listview - Fatal编程技术网

Android 如何在从其他活动中删除列表项后更新我的ListView?

Android 如何在从其他活动中删除列表项后更新我的ListView?,android,sqlite,listview,Android,Sqlite,Listview,我在删除函数中遇到了问题。当我单击delete时,它会工作并删除sqlite中的记录。但问题是在删除它之后,它会跳回使用listview的上一个活动。但是被删除的记录仍然会出现在那里。我需要从listview活动中退出,然后再次返回,直到它消失。 我该怎么办 这里是我在数据库中的删除功能 public Integer deleteData (String property) { SQLiteDatabase db = this.getReadableDatabase(); ret

我在删除函数中遇到了问题。当我单击delete时,它会工作并删除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();
    }

    }
}
公共类Sellerhouse扩展了ListActivity{
TextView house_property;
字符串全名;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.布局、活动、卖方、房屋);
Intent i=getIntent();
全名=i.getStringExtra(“全名”);
}
恢复时公开作废()
{
super.onResume();
房屋SDB列表=新房屋SDB(本);
//list.insertProduct();
ArrayList houseList=list.getSellerHouseList(全名);
如果(houseList.size()!=0){
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
house_property=(TextView)view.findViewById(R.id.shouse_property);
字符串houseproperty=house_property.getText().toString();
//入户详情
Intent objIndent=newintent(getApplicationContext(),SellerHouseDetail.class);
objIndent.putExtra(“房产”,房产);
星形触觉(明显);
}
});
ListAdapter adapter=new SimpleAdapter(Sellerhouse.this,houseList,R.layout.activity\u seller\u house\u info,新字符串[]{“id”,“property”,“type”,“state”},新int[]{R.id.shouse\u id,R.id.shouse\u property,R.id.shouse\u type,R.id.shouse\u state});
setListAdapter(适配器);
}否则{
Toast.makeText(这个“找不到房子!”,Toast.LENGTH_SHORT.show();
}
}
}
试试这个

    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
试试这个

    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

在这种情况下,只需使用
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\u cancelled

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();
在您的销售室中 重写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,方法如下

 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,请调用此方法。

在这种情况下,只需使用
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\u cancelled

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();
在您的销售室中 重写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,方法如下

 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,请调用此方法。

此部分存在问题:

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();
}
if(houseList.size()!=0){
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
house_property=(TextView)view.findViewById(R.id.shouse_property);
字符串houseproperty=house_property.getText().toString();
//入户详情
Intent objIndent=newintent(getApplicationContext(),SellerHouseDetail.class);
objIndent.putExtra(“房产”,房产);
星形触觉(明显);
}
});
ListAdapter adapter=new SimpleAdapter(Sellerhouse.this,houseList,R.layout.activity\u seller\u house\u info,新字符串[]{“id”,“property”,“type”,“state”},新int[]{R.id.shouse\u id,R.id.shouse\u property,R.id.shouse\u type,R.id.shouse\u state});
setListAdapter(适配器);
}否则{
Toast.makeText(这个“找不到房子!”,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();
}
ListView lv=getListView();
ListAdapter adapter=new SimpleAdapter(Sellerhouse.this,houseList,R.layout.activity\u seller\u house\u info,新字符串[]{“id”,“property”,“type”,“state”},新int[]{R.id.shouse\u id,R.id.shouse\u property,R.id.shouse\u type,R.id.shouse\u state});
setListAdapter(适配器);
如果(houseList.size()!=0){
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
house_property=(TextView)view.findViewById(R.id.shouse_property);
字符串houseproperty=house_property.getText().toString();
//入户详情
Intent objIndent=newintent(getApplicationContext(),SellerHouseDetail.class);
objIndent.putExtra(“房产”,房产);
星形触觉(明显);
}
});
}否则{
Toast.makeText(这个“找不到房子!”,Toast.LENGTH_SHORT.show();
}
你很好:),
虽然这不是一种优化的方法,但现在它可以解决您的问题。

问题就在这一部分:

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();
}
if(houseList.size()!=0){
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
house_property=(TextView)view.findViewById(R.id.shouse_property);
字符串houseproperty=house_property.getText().toString();