parniaznet
یک شنبه 16 فروردین 1394, 19:57 عصر
سلام
من برای نمایش اطلاعات دیتابیس از گرید ویو استفاده می کنم، در فرگمنتم دکمه بروزرسانی دارم که اطلاعات جدید رو از سرور میگیره، می خوام UI پس از اتمام اسینک تسک رفرش بشه و مقادیر جدید رو هم گرید ویو نمایش بده
برای اینکار اومدم از متد onProgressUpdate برای مقدار دهی جدید به ایتم ها و ست کردن اداپتر استفاده کردم اما کار نکرد.
این کدها :
protected void onProgressUpdate(String... values) {
int save = db.shgoroh("khanevade");
Goroh = new String [save];
model = new String [save];
for (int i = 0; i <save; i++) {
Goroh= db.ngoroh("khanevade", i, 1);
model= db.ngoroh("khanevade", i, 11);
}
CustomGrid adapter = new CustomGrid(getActivity(), Goroh, imageId);
grid.setAdapter(adapter);
}
کد های کامل فرگمنت :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate the root view of the fragment
View fragmentView = inflater.inflate(R.layout.khanevadeh, container, false);
db = new database(getActivity());
db.open();
setHasOptionsMenu(true);
id = db.akharinid("khanevade", 0);
grid=(GridView)fragmentView.findViewById(R.id.grid _khanevadeh);
int save = db.shgoroh("khanevade");
Goroh = new String [save];
model = new String [save];
for (int i = 0; i <save; i++) {
Goroh[i]= db.ngoroh("khanevade", i, 1);
model[i]= db.ngoroh("khanevade", i, 11);
}
CustomGrid adapter = new CustomGrid(getActivity(), Goroh, imageId);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (model[position].equals("1")) {
Intent intent = new Intent(getActivity(), Onvanha_khanevadeh.class);
intent.putExtra("goroh", Goroh[position]);
intent.putExtra("zirgoroh", "0");
startActivity(intent);
} else {
Intent intent = new Intent(getActivity(), Zirgoroh_khanevadeh.class);
intent.putExtra("goroh", Goroh[position]);
startActivity(intent);
}
}
});
return fragmentView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
public void showToast(String msg){
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
NetAsync(getView());
return true;
case R.id.copyLink:
showToast("Copy link was clicked.");
return true;
case R.id.share:
showToast("Share was clicked.");
return true;
case R.id.action_settings:
showToast("actionsettings was clicked.");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void NetAsync(View view){
new NetCheck().execute();
}
private class NetCheck extends AsyncTask<String,String,Boolean> {
private ProgressDialog nDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
nDialog = new ProgressDialog(getActivity());
nDialog.setMessage("Loading..");
nDialog.setTitle("Checking Network");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVIT Y_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (result==true) {
nDialog.dismiss();
new ProcessUpdatedb().execute();
} else {
nDialog.dismiss();
Toast.makeText(getActivity(), "مشکل در ارتباط با اینترنت", Toast.LENGTH_SHORT).show();
}
}
}
private class ProcessUpdatedb extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Updating Database ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("id", id));
JSONObject json = jParser.getJSONFromUrl(url_all_products, params1);
Log.d("All Products: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_ID);
String goroh = c.getString(TAG_GOROH);
String zirgoroh = c.getString(TAG_ZIRGOROH);
String name = c.getString(TAG_NAME);
String matn = c.getString(TAG_MATN);
String tasvir = c.getString(TAG_TASVIR);
String like = c.getString(TAG_LIKE);
String rate = c.getString(TAG_RATE);
String model = c.getString(TAG_MODEL);
db.update_khanevade(id, goroh, zirgoroh, name, matn, tasvir, like, rate, model);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity(),MainActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
int save = db.shgoroh("khanevade");
Goroh = new String [save];
model = new String [save];
for (int i = 0; i <save; i++) {
Goroh[i]= db.ngoroh("khanevade", i, 1);
model[i]= db.ngoroh("khanevade", i, 11);
}
CustomGrid adapter = new CustomGrid(getActivity(), Goroh, imageId);
grid.setAdapter(adapter);
}
@Override
protected void onPostExecute(JSONObject result) {
pDialog.dismiss();
Toast.makeText(getActivity(), "آپدیت شد حالشو ببر پسر", Toast.LENGTH_SHORT).show();
super.onPostExecute(result);
}
}
}
کجا رو اشتباه انجام میدم و درستش به چه صورت هست؟
من برای نمایش اطلاعات دیتابیس از گرید ویو استفاده می کنم، در فرگمنتم دکمه بروزرسانی دارم که اطلاعات جدید رو از سرور میگیره، می خوام UI پس از اتمام اسینک تسک رفرش بشه و مقادیر جدید رو هم گرید ویو نمایش بده
برای اینکار اومدم از متد onProgressUpdate برای مقدار دهی جدید به ایتم ها و ست کردن اداپتر استفاده کردم اما کار نکرد.
این کدها :
protected void onProgressUpdate(String... values) {
int save = db.shgoroh("khanevade");
Goroh = new String [save];
model = new String [save];
for (int i = 0; i <save; i++) {
Goroh= db.ngoroh("khanevade", i, 1);
model= db.ngoroh("khanevade", i, 11);
}
CustomGrid adapter = new CustomGrid(getActivity(), Goroh, imageId);
grid.setAdapter(adapter);
}
کد های کامل فرگمنت :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate the root view of the fragment
View fragmentView = inflater.inflate(R.layout.khanevadeh, container, false);
db = new database(getActivity());
db.open();
setHasOptionsMenu(true);
id = db.akharinid("khanevade", 0);
grid=(GridView)fragmentView.findViewById(R.id.grid _khanevadeh);
int save = db.shgoroh("khanevade");
Goroh = new String [save];
model = new String [save];
for (int i = 0; i <save; i++) {
Goroh[i]= db.ngoroh("khanevade", i, 1);
model[i]= db.ngoroh("khanevade", i, 11);
}
CustomGrid adapter = new CustomGrid(getActivity(), Goroh, imageId);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (model[position].equals("1")) {
Intent intent = new Intent(getActivity(), Onvanha_khanevadeh.class);
intent.putExtra("goroh", Goroh[position]);
intent.putExtra("zirgoroh", "0");
startActivity(intent);
} else {
Intent intent = new Intent(getActivity(), Zirgoroh_khanevadeh.class);
intent.putExtra("goroh", Goroh[position]);
startActivity(intent);
}
}
});
return fragmentView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
public void showToast(String msg){
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
NetAsync(getView());
return true;
case R.id.copyLink:
showToast("Copy link was clicked.");
return true;
case R.id.share:
showToast("Share was clicked.");
return true;
case R.id.action_settings:
showToast("actionsettings was clicked.");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void NetAsync(View view){
new NetCheck().execute();
}
private class NetCheck extends AsyncTask<String,String,Boolean> {
private ProgressDialog nDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
nDialog = new ProgressDialog(getActivity());
nDialog.setMessage("Loading..");
nDialog.setTitle("Checking Network");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVIT Y_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (result==true) {
nDialog.dismiss();
new ProcessUpdatedb().execute();
} else {
nDialog.dismiss();
Toast.makeText(getActivity(), "مشکل در ارتباط با اینترنت", Toast.LENGTH_SHORT).show();
}
}
}
private class ProcessUpdatedb extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Updating Database ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("id", id));
JSONObject json = jParser.getJSONFromUrl(url_all_products, params1);
Log.d("All Products: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_ID);
String goroh = c.getString(TAG_GOROH);
String zirgoroh = c.getString(TAG_ZIRGOROH);
String name = c.getString(TAG_NAME);
String matn = c.getString(TAG_MATN);
String tasvir = c.getString(TAG_TASVIR);
String like = c.getString(TAG_LIKE);
String rate = c.getString(TAG_RATE);
String model = c.getString(TAG_MODEL);
db.update_khanevade(id, goroh, zirgoroh, name, matn, tasvir, like, rate, model);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity(),MainActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
int save = db.shgoroh("khanevade");
Goroh = new String [save];
model = new String [save];
for (int i = 0; i <save; i++) {
Goroh[i]= db.ngoroh("khanevade", i, 1);
model[i]= db.ngoroh("khanevade", i, 11);
}
CustomGrid adapter = new CustomGrid(getActivity(), Goroh, imageId);
grid.setAdapter(adapter);
}
@Override
protected void onPostExecute(JSONObject result) {
pDialog.dismiss();
Toast.makeText(getActivity(), "آپدیت شد حالشو ببر پسر", Toast.LENGTH_SHORT).show();
super.onPostExecute(result);
}
}
}
کجا رو اشتباه انجام میدم و درستش به چه صورت هست؟