gpt4 book ai didi

php - 通过android编辑mysql

转载 作者:行者123 更新时间:2023-11-29 11:59:53 24 4
gpt4 key购买 nike

我希望有人能帮助我解决我的问题:我想删除mysql中的一个条目!到目前为止它有效!唯一的问题是:它会立即从服务器中删除,但不会在应用程序中删除!如果我刷新 ListView ,它仍然存在(但不在服务器上)。几分钟后,它也从 listView 中消失了。但是我如何编辑此 Activity ,该条目也会立即在应用程序中删除? (我从教程中复制了这段代码,因为我对 android 编程不太熟悉)

package info.androidhive.loginandregistration;

import java.util.ArrayList;
...
import android.widget.TextView;

public class AlleTischeActivity extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;
private SwipeRefreshLayout mSwipeRefreshLayout;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> productsList;

// url to get all products list
private static String url_all_products = "http://***/get_all_products.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_TISCHE = "tische";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "tischnummer";

// products JSONArray
JSONArray tische = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alle_tische);

SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new LoadAllProducts().execute();
}

});
// Hashmap for ListView
new LoadAllProducts().execute();
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread

// Get listview
ListView lv = getListView();


// on selecting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.id)).getText().toString();

//Starting new intent
Intent in = new Intent(getApplicationContext(), EditTischActivity.class);
//sending pid to next activity
in.putExtra(TAG_ID, pid);

// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});

}

// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
new LoadAllProducts().execute();
}

}


/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
//This is the constructor you should add over here
public LoadAllProducts(){
productsList = new ArrayList<HashMap<String, String>>();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AlleTischeActivity.this);
pDialog.setMessage("Lade offene Tische. Bitte warten...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = JSONParser.makeHttpRequest(url_all_products, "GET", params);


// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
tische = json.getJSONArray(TAG_TISCHE);

// looping through All Products
for (int i = 0; i < tische.length(); i++) {
JSONObject c = tische.getJSONObject(i);

// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);

// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
TischActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/*** After completing background task Dismiss the progress dialog ***/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/*** Updating parsed JSON data into ListView ***/
ListAdapter adapter = new SimpleAdapter(
AlleTischeActivity.this, productsList,
R.layout.list_item, new String[] { TAG_ID,
TAG_NAME},
new int[] { R.id.id, R.id.tischnummer, R.id.biernummer });
// updating listview
setListAdapter(adapter);

}
});

}


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.allmenu, menu);
return true;
}

@Override

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh_table:
new LoadAllProducts().execute();
break;
//case R.id.menuitem2:
//Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
//.show();
//break;

default:
break;
}

return true;
}
}

最佳答案

首先,将适配器从 SimpleAdapter 更改为至ArrayAdapter .

类似于:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(AlleTischeActivity.this, android.R.layout.simple_list_item_1, yourDataArray);

并将其设置为:

setListAdapter(adapter);

然后,当您想要删除行时,请尝试使用 ArrayAdapter 中的 remove()。像这样的东西:

ArrayAdapter<String> myAdapter = (ArrayAdapter<String>)getListView().getAdapter();
myAdapter.remove(myAdapter.getItem(position)); // Where position is the position of the row you want to delete, you can get it through the "onClickListener()".
myAdapter.notifyDataSetChanged();
<小时/>

或者您可以基于 BaseAdapter 编写自己的适配器来保存您的数据,并且您可以在其中实现您自己的 remove 方法。 This question , this answerthis tutorial涵盖了如何做到这一点。

编辑:当然,这完全取决于您希望 ListView 的外观以及您想要显示的数据。

<小时/>

第二次编辑: TL;DR:简短的答案...删除服务器上的数据后,调用此行:new LoadAllProducts().execute(); 重新加载数据。

关于php - 通过android编辑mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32571598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com