java - 我的数据库已经更新,但我的 android studio 仍然没有更新,仍然显示以前获取的数据库

标签 java android mysql

如何更新到新数据库,因为我尝试更新我的数据库,但它仍然没有改变,它仍然获取旧数据库。我正在使用 php 文件连接到数据库 mysql 并使用 JSON 解析数据。

这是代码..

public class TampilUser extends AppCompatActivity {

    ListView listView;
    CustomAdapter customAdapter;
    ArrayList<User> user = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tampil_user);

        listView = (ListView) findViewById(R.id.listUser);

        getJSON("Link here");
    }

    @Override
    public void onResume() {
        super.onResume();

        getJSON("Link here");
    }

    private void getJSON(final String urlWebService) {

        class GetJSON extends AsyncTask<Void, Void, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                try {
                    loadIntoListView(s);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected String doInBackground(Void... voids) {
                try {
                    URL url = new URL(urlWebService);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String json;
                    sb.setLength(0);
                    while ((json = bufferedReader.readLine()) != null) {
                        sb.append(json + "\n");
                    }
                    bufferedReader.close();
                    con.disconnect();
                    return sb.toString().trim();
                } catch (Exception e) {
                    return null;
                }
            }
        }
        GetJSON getJSON = new GetJSON();
        getJSON.execute();
    }

    public void loadIntoListView(String json) throws JSONException {
        JSONArray jsonArray = new JSONArray(json);
        user = new ArrayList<>();
        user.clear();
        User u;
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            String email = obj.getString("email");
            String nama = obj.getString("nama");

            u = new User();
            u.setEmail(email);
            u.setNama(nama);

            user.add(u);
        }
        customAdapter = new CustomAdapter(getApplicationContext(),user);
        listView.setAdapter(customAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String nama = customAdapter.user.get(position).getNama();
                String email = customAdapter.user.get(position).getEmail();
                Intent i = new Intent(getApplicationContext(), DetailUser.class);

                i.putExtra("email", email);
                i.putExtra("nama", nama);
                startActivityForResult(i, 0);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        getJSON("Link here"); 
        customAdapter.notifyDataSetChanged();
        customAdapter.notifyDataSetInvalidated();
    }
}

我正在使用自定义适配器,这是自定义适配器扩展基本适配器中的获取 View 代码。

public View getView(int position, View convertView, ViewGroup parent) {

   if(convertView==null)
   {
       convertView=inflater.inflate(R.layout.daftar_user,parent,false);
   }
   TextView textView_email = (TextView) convertView.findViewById(R.id.textView_email);
   TextView textView_nama = (TextView) convertView.findViewById(R.id.textView_nama);

   textView_email.setText(user.get(position).getEmail());
   textView_nama.setText(user.get(position).getNama());

   return convertView;
}

我已经尝试了很多事情,但仍然没有改变..而且我还是 stackoverflow 的新手,我将非常感谢你的帮助..谢谢...

最佳答案

问题已经解决。

我只是在点击 ListView 上启动新 Intent 之前放置了 customadapter.notifyDataSetChanged()

关于java - 我的数据库已经更新,但我的 android studio 仍然没有更新,仍然显示以前获取的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52403000/

相关文章:

Java 编码器 "input encoding"

java - Android上的JavaFX-Gradle找不到JDK

java - 如何在maven应用程序中使用eclipse swing设计器?

java - Android 后退按钮导致 Bad Keycharmap 警告

android - ListView 中的可滚动 TextView ?

php - 我的数据库显示 160 个条目,但我只有 80 个

mysql - 无法连接到远程mysql数据库服务器

java - 如何使用 JPA/EclipseLink 获取更新时更改的属性

java - Camera2 和 Mediarecorder 输出高质量预览但低质量 mp4

php - mysql db 对所有主机连接开放