如何更新到新数据库,因为我尝试更新数据库,但它仍然没有更改,因此仍在获取旧数据库。我正在使用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();
}
}
我正在使用自定义适配器,这是自定义适配器扩展基本适配器中的获取视图代码。
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还是很陌生,非常感谢您的帮助..谢谢...
答案 0 :(得分:0)
问题已经解决。
我只是将customadapter.notifyDataSetChanged()放在开始点击列表视图上的新意图之前