android - 如何在android ListView 中使用notifyDataSetAdapter?

标签 android mysql listview

我想在 ListView 中使用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();:

Call notifyDataSetChanged() on your Adapter object once you've modified the data in that adapter.

It notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

例如

   ((BaseAdapter) yourListView.getAdapter()).notifyDataSetChanged();

adapter.notifyDataSetChanged();

关于android - 如何在android ListView 中使用notifyDataSetAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46053346/

相关文章:

java - 如何将数据从sqlite缓存到listview?

Javafx 更新对象属性上的 ListCell

android - 无法创建 nomedia 文件

mysql - 用 MySQL 中的另一个表更新表

php - 持久连接 : MySQL FOUND_ROWS() results

PHP - 当有人尝试使用已被占用的用户名进行注册时,MySQL 错误 "Error: Query was empty"

android ListView 是空的 Logcat 什么都不显示

Android AdMob 中介 - 不接收中介网络广告

android - 在 kivy 发行版中包含 PySerial 库

android - 如何可靠地从 AttributeSet 中获取颜色?