gpt4 book ai didi

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

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

如何更新到新数据库,因为我尝试更新我的数据库,但它仍然没有改变,它仍然获取旧数据库。我正在使用 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/

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