我想在列表视图中使用notifyDataSetAdpter。我了解到我可以在更新或删除数据时使用它。就我而言,我的整个数据将显示在listView中,而我正在从MySQL调用此数据。现在,我只是将数据库值显示在列表中。没有删除。我希望该方法在我尝试使用此方法的列表中显示更新的数据库值,但它不起作用。在我的情况下,能否请您告诉我该使用它的确切程度和确切位置。
这是我的代码:
See_Issue.java
package com.example.mi.mikpiadmin;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class See_Issue extends AppCompatActivity implements ListView.OnItemClickListener {
ListView listView1;
public static final String URL_GET_ISSUE = "http://10.238.4.175/new/one.php";
public static final String TAG_JSON_ARRAY="results";
public static final String TAG_ID = "id";
public static final String TAG_STORE_NAME = "store_name";
public static final String TAG_ISSUE = "issue";
public static final String TAG_DESCRIBE = "describe";
private String JSON_ISSUE_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_see__issue);
listView1=(ListView)findViewById(R.id.list_see_issue) ;
listView1.setOnItemClickListener(this);
getJSON();
}
private void showEmployee(){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_ISSUE_STRING);
JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String id = jo.getString(TAG_STORE_NAME);
String name = jo.getString(TAG_ISSUE);
String describe = jo.getString(TAG_DESCRIBE);
HashMap<String,String> employees = new HashMap<>();
employees.put(TAG_STORE_NAME,id);
employees.put(TAG_ISSUE,name);
employees.put(TAG_DESCRIBE,describe);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
See_Issue.this, list, R.layout.list_item,
new String[]{TAG_STORE_NAME,TAG_ISSUE,TAG_DESCRIBE},
new int[]{R.id.id, R.id.name, R.id.feedback});
listView1.setAdapter(adapter);
((ListAdapter) listView1.getAdapter()).notifyDataSetChanged();
}
private void getJSON(){
class GetJSON extends AsyncTask<Void,Void,String> {
private ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(See_Issue.this,"Fetching Data","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_ISSUE_STRING = s;
showEmployee();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL_GET_ISSUE);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, See_Feedback.class);
HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
String empId = map.get(Config.TAG_ID).toString();
intent.putExtra(Config.EMP_ID,empId);
startActivity(intent);
}
}
希望您能对您有所帮助。
最佳答案
您可以使用adapter.notifyDataSetChanged();
:
修改适配器中的数据后,在Adapter对象上调用notifyDataSetChanged()。
它通知附加的观察者基础数据已更改,任何反映该数据集的View均应刷新自身。
例如
((BaseAdapter) yourListView.getAdapter()).notifyDataSetChanged();
要么
adapter.notifyDataSetChanged();