如何在listview android中使用notifyDataSetAdapter?
我想在列表视图中使用notifyDataSetAdpter。我了解到,当我的数据更新或删除时,我可以使用它。在我的例子中,我的全部数据将显示在一个listView中,我从MySQL调用这些数据。现在,我只是在列表中显示DB值。没有删除。我希望此方法在列表中显示更新的DB值,我尝试使用此方法,但无效。你能告诉我,在我的情况下,我应该在哪里使用它吗 这是我的密码:如何在listview android中使用notifyDataSetAdapter?,android,mysql,listview,Android,Mysql,Listview,我想在列表视图中使用notifyDataSetAdpter。我了解到,当我的数据更新或删除时,我可以使用它。在我的例子中,我的全部数据将显示在一个listView中,我从MySQL调用这些数据。现在,我只是在列表中显示DB值。没有删除。我希望此方法在列表中显示更新的DB值,我尝试使用此方法,但无效。你能告诉我,在我的情况下,我应该在哪里使用它吗 这是我的密码: See_Issue.java package com.example.mi.mikpiadmin; import android.ap
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);
}
}
参见_Issue.java
包com.example.mi.mikpiadmin;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类See_问题扩展AppCompativeActivity实现ListView.OnItemClickListener{
列表视图列表视图1;
公共静态最终字符串URL\u获取\u问题=”http://10.238.4.175/new/one.php";
公共静态最终字符串标记_JSON_ARRAY=“results”;
公共静态最终字符串标记\u ID=“ID”;
公共静态最终字符串标记\u STORE\u NAME=“STORE\u NAME”;
公共静态最终字符串标记_ISSUE=“ISSUE”;
公共静态最终字符串标记_descripe=“descripe”;
私有字符串JSON_发布_字符串;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u请参见发布);
listView1=(ListView)findViewById(R.id.list\u见发布);
listView1.setOnItemClickListener(此);
getJSON();
}
私人雇员(){
JSONObject JSONObject=null;
ArrayList=新建ArrayList();
试一试{
jsonObject=新的jsonObject(JSON_发布_字符串);
JSONArray result=jsonObject.getJSONArray(TAG_JSON_数组);
对于(int i=0;i您可以使用适配器.notifyDataSetChanged();
:
修改适配器中的数据后,调用适配器对象
它通知附加的观察者基础数据已更改,任何反映数据集的视图都应刷新自身
例如
((BaseAdapter) yourListView.getAdapter()).notifyDataSetChanged();
或
您可以使用适配器。notifyDataSetChanged();: 修改适配器中的数据后,调用适配器对象 它通知附加的观察者基础数据已更改,任何反映数据集的视图都应刷新自身 例如
((BaseAdapter) yourListView.getAdapter()).notifyDataSetChanged();
或
要实现这一点,您需要做一些更改- 1-使这些全球化-
private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
private HashMapAdapter adapter;
private ArrayList list=new ArrayList();
私有hashmappadapter;
2-编写自己的自定义适配器类-
public class HashMapAdapter extends BaseAdapter {
private HashMap<String, String> mData = new HashMap<String, String>();
private String[] mKeys;
public HashMapAdapter(HashMap<String, String> data){
mData = data;
mKeys = mData.keySet().toArray(new String[data.size()]);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(mKeys[position]);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
String key = mKeys[pos];
String Value = getItem(pos).toString();
//do your view stuff here
return convertView;
}
public void updateData(HashMap<String, String> updatedData){
mData = updatedData;
mKeys = updatedData.keySet().toArray(new String[data.size()]);
//notifying dataset changed
notifyDataSetChanged();
}
}
公共类HashMapAdapter扩展了BaseAdapter{
私有HashMap mData=新HashMap();
私有字符串[]mKeys;
公共HashMapAdapter(HashMap数据){
mData=数据;
mKeys=mData.keySet().toArray(新字符串[data.size()]);
}
@凌驾
public int getCount(){
返回mData.size();
}
@凌驾
公共对象getItem(int位置){
返回mData.get(mKeys[position]);
}
@凌驾
公共长getItemId(int arg0){
返回arg0;
}
@凌驾
公共视图getView(int pos、视图转换视图、视图组父视图){
字符串键=mKeys[pos];
字符串值=getItem(pos).toString();
//这里有你的观点吗
返回视图;
}
public void updateData(HashMap updateData){
mData=更新数据;
mKeys=updateData.keySet().toArray(新字符串[data.size()]);
//通知数据集已更改
notifyDataSetChanged();
}
}
3-更改showEmployee()方法
private void showEmployee(){
JSONObject JSONObject=null;
ArrayList=新建ArrayList();
试一试{
jsonObject=新的jsonObject(JSON_发布_字符串);
JSONArray result=jsonObject.getJSONArray(TAG_JSON_数组);
对于(inti=0;i,您必须进行一些更改才能实现这一点-
1-使这些全球化-
private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
private HashMapAdapter adapter;
private ArrayList list=new ArrayList();
私有hashmappadapter;
2-编写自己的自定义适配器类-
public class HashMapAdapter extends BaseAdapter {
private HashMap<String, String> mData = new HashMap<String, String>();
private String[] mKeys;
public HashMapAdapter(HashMap<String, String> data){
mData = data;
mKeys = mData.keySet().toArray(new String[data.size()]);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(mKeys[position]);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
String key = mKeys[pos];
String Value = getItem(pos).toString();
//do your view stuff here
return convertView;
}
public void updateData(HashMap<String, String> updatedData){
mData = updatedData;
mKeys = updatedData.keySet().toArray(new String[data.size()]);
//notifying dataset changed
notifyDataSetChanged();
}
}
公共类HashMapAdapter扩展了BaseAdapter{
私有HashMap mData=新HashMap();
私有字符串[]mKeys;
公共HashMapAdapter(HashMap数据){
mData=数据;
mKeys=mData.keySet().toArray(新字符串[data.size()]);
}
@凌驾
public int getCount(){
返回mData.size();
}
@凌驾
公共对象getItem(int位置){
返回mData.get(mKeys[position]);
}
@凌驾
公共长getItemId(int arg0){
返回arg0;
}
@凌驾
公共视图getView(int pos、视图转换视图、视图组父视图){
字符串键=mKeys[pos];
字符串值=getItem(pos).toString();
//这里有你的观点吗
返回视图;
}
public void updateData(HashMap updateData){
mData=更新数据;
mKeys=updateData.keySet().toArray(新字符串[data.size()]);
//通知数据集已更改
notifyDataSetChanged();
}
}
3-更改showEmployee()方法
private void showEmployee(){
JSONObject JSONObject=null;
ArrayList=新建ArrayList();
试一试{
jsonObject=新的jsonObject(JSON_发布_字符串);
JSONArray result=jsonObject.getJSONArray(TAG_JSON_数组);
对于(int i=0;i您在哪里对列表进行更改?在方法showEmployee()中,我尝试在列表适配器中使用此方法,但它不起作用