我在片段中有一个listview。它从Json加载数据。单击列表项后,片段事务开始(替换)。通过按下后退按钮(在新片段中)再次出现列表片段,但问题是:
1:它再次加载Asynctask(我不想要它)
2.它在列表中的前一项上添加新项目。你可以说listview项目中的重复发生。每次单击listview项目abd然后返回到listview片段时,它会将相同的项目添加到列表的末尾。我的代码是这样的:
public class Followed extends Fragment {
public Followed(){}
ArrayList<Country> countryList = new ArrayList<Country>();
Country country;
ListView listView;
JSONParser jsonParser = new JSONParser();
public static String received_id;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
MyCustomAdapter dataAdapter = null;
ProgressDialog pDialog;
int success;
private static String url_all_followed = "http://www.hitel.ir/FarsiPlanet/Followed.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_RATE = "rate";
private static final String TAG_RATINGCOUNT = "ratingcount";
private static final String TAG_SHORT_DESCRIPTION = "short_description";
// products JSONArray
JSONArray products = null;
String Category;
String username;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.items_list, container, false);
loadSavedPreferences();
listView = (ListView) rootView.findViewById(R.id.listView1);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
Bundle data = new Bundle();
data.putString(TAG_PID, pid);
data.putString("TAG_Category", "restaurant");
Fragment fragment = new Details_Restaurant();
FragmentManager fm = getFragmentManager();
fragment.setArguments(data);
fm.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack("f_02asda").commit();
}
});
new loadMoreListView().execute();
return rootView;
}
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView code;
TextView name;
RatingBar rate;
TextView ratingcount;
TextView short_description;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.country_info, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.ratingcount = (TextView) convertView.findViewById(R.id.ratingCount);
holder.short_description = (TextView) convertView.findViewById(R.id.short_description);
holder.rate = (RatingBar) convertView.findViewById(R.id.ratingBar1);
holder.code = (TextView) convertView.findViewById(R.id.pid);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Country country = countryList.get(position);
holder.name.setText(country.getName());
holder.ratingcount.setText(country.getRatingCount());
holder.short_description.setText(country.getShort_description());
holder.rate.setRating(country.getRate());
holder.code.setText(country.getCode());
return convertView;
}
}
class loadMoreListView extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("درحال دريافت اطلاعات...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "sajad"));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_all_followed, "GET", params);
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
Float rate = (float) c.getInt(TAG_RATE);
String ratingcount = c.getString(TAG_RATINGCOUNT);
String short_description = c.getString(TAG_SHORT_DESCRIPTION);
country = new Country(id,name,rate,ratingcount,short_description,"","");
countryList.add(country);
}
}
else{
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
if (success == 0) {
Toast.makeText(getActivity(), "هیچ موردی یافت نشد!", Toast.LENGTH_SHORT).show();
}
pDialog.dismiss();
dataAdapter = new MyCustomAdapter(getActivity(),
R.layout.country_info, countryList);
listView.setAdapter(dataAdapter);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getActivity());
username = sharedPreferences.getString("username", "");
}
}
答案 0 :(得分:0)
好的。发现重复问题。我在点击列表项后清除了适配器。
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
Bundle data = new Bundle();
data.putString(TAG_PID, pid);
data.putString("TAG_Category", "restaurant");
Fragment fragment = new Details_Restaurant();
FragmentManager fm = getFragmentManager();
fragment.setArguments(data);
fm.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack("f_02asda").commit();
dataAdapter.clear();
}
});