嗨我有弹出按钮。当我点击弹出按钮时,它会显示单选按钮。对于每个单选按钮选择,我在UI中获取数据和更新。 问题是第一个单选按钮正在更新UI。但对于第二个单选按钮等,UI不会更新。怎么解决这个问题?
代码如下:
public class DataList extends Activity {
private Button popUpClick;
private Dialog sortFilterDialog;
private RadioGroup radioGroup;
private RadioButton ascToDesradioButton, desToAscradioButton,
highToLowradioButton, lowToHighradioButton, popularityradioButton;
private int popupselectionItem;
private ImageView closeButton;
private String sessionId;
private String sortName,sortOrder;
ArrayList<SortFilterProducts> personsList;
private ListView list;
private DataListAdapter gridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sortfilterclick);
personsList= new ArrayList<SortFilterProducts>();
popUpClick = (Button) findViewById(R.id.popupButton);
list=(ListView)findViewById(R.id.sortFilterList);
gridAdapter = new DataListAdapter(this, R.layout.sort_filter_listrow, personsList);
list.setAdapter(gridAdapter);
popUpClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sortFilterDialog = new Dialog(SortFilterPopupActivity.this);
sortFilterDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
sortFilterDialog.setContentView(R.layout.sortfilterrow);
radioGroup = (RadioGroup) sortFilterDialog
.findViewById(R.id.radioGroup);
ascToDesradioButton = (RadioButton) sortFilterDialog
.findViewById(R.id.asc_to_des);
desToAscradioButton = (RadioButton) sortFilterDialog
.findViewById(R.id.des_to_asc);
highToLowradioButton = (RadioButton) sortFilterDialog
.findViewById(R.id.high_to_low);
lowToHighradioButton = (RadioButton) sortFilterDialog
.findViewById(R.id.low_to_high);
popularityradioButton = (RadioButton) sortFilterDialog
.findViewById(R.id.popularity);
ascToDesradioButton
.setOnClickListener(radioButtonOnClickListener);
desToAscradioButton
.setOnClickListener(radioButtonOnClickListener);
highToLowradioButton
.setOnClickListener(radioButtonOnClickListener);
lowToHighradioButton
.setOnClickListener(radioButtonOnClickListener);
popularityradioButton
.setOnClickListener(radioButtonOnClickListener);
}
private final OnClickListener radioButtonOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (popupselectionItem = v.getId()) {
case R.id.asc_to_des:
sortName="atoz";
sortOrder="SORT_ASC";
new SortFilterElement().execute();
//new AscToDesElements().execute();
break;
case R.id.des_to_asc:
sortName="atoz";
sortOrder="SORT_DESC";
new SortFilterElement().execute();
//new DescToAscElements().execute();
break;
case R.id.high_to_low:
sortName="lowtohigh";
sortOrder="SORT_ASC";
new SortFilterElement().execute();
//new PriceHightoLow().execute();
break;
case R.id.low_to_high:
sortName="lowtohigh";
sortOrder="SORT_DESC";
//new PriceLowtoHigh().execute();
new SortFilterElement().execute();
break;
case R.id.popularity:
sortName="popularity";
sortOrder="SORT_ASC";
//new Popularity().execute();
new SortFilterElement().execute();
break;
default:
}
sortFilterDialog.dismiss();
}
class SortFilterElement extends AsyncTask<String,String,String>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog=new ProgressDialog(SortFilterPopupActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected String doInBackground(String... args) {
try {
SoapSerializationEnvelope env = new SoapSerializationEnvelope(
SoapSerializationEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
if (sessionId == null) {
JSONObject json = new JSONObject();
json.put("page", "1");
json.put("limit", "10");
json.put("sort_name", sortName);
json.put("sort_order", sortOrder);
String params = json.toString();
requests.addProperty("args", params);
env.setOutputSoapObject(requests);
androidHttpTransport.call("", env);
Object results = env.getResponse();
Log.e("Sort results", results.toString());
if (results.toString() != null) {
JSONObject jsono = new JSONObject(results
.toString());
JSONArray jarray = jsono
.getJSONArray("result");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
SortFilterProducts products = new SortFilterProducts();
String id = object.getString("id");
int productPrice = object.getInt("price");
String imageUrl = object
.getString("image_url");
int ratings=object.getInt("ratings");
products.setProductName(productName);
products.setImageUrl(imageUrl);
personsList.add(products);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.cancel();
gridAdapter.notifyDataSetChanged();
}
}
};
});
}
}
和我的DataListAdapter:
public class DataListAdapter extends BaseAdapter {
LayoutInflater layoutInflater;
int Resource;
ViewHolder viewHolder;
Activity activity;
public ImageLoader loader;
private final ArrayList<SortFilterProducts> itemLists;
public DataListAdapter(Activity a, int resource,
ArrayList<SortFilterProducts> itemList) {
layoutInflater = (LayoutInflater) a
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
itemLists = itemList;
activity = a;
loader = new ImageLoader(a.getApplicationContext());
}
@Override
public int getCount() {
return itemLists.size();
}
@Override
public Object getItem(int position) {
return itemLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= convertView;
try {
if(v==null){
viewHolder = new ViewHolder();
v = layoutInflater.inflate(Resource, null);
viewHolder.productName=(TextView)v.findViewById(R.id.productName);
viewHolder.productPrice=(TextView)v.findViewById(R.id.price);
v.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) v.getTag();
}
final String productName=itemLists.get(position).getProductName();
final int productPrice=itemLists.get(position).getProductPrice();
viewHolder.productName.setText(productName);
viewHolder.productPrice.setText(Integer.toString(productPrice));
}
catch (Exception ex) {
ex.printStackTrace();
}
return v;
}
static class ViewHolder {
public TextView productName,productPrice;
}
}
答案 0 :(得分:1)
在扩展传递给适配器的 ArrayList&lt; SortFilterProducts&gt; 时,使ListView的项无效。
list.invalidateViews();
祝你好运。 :)
答案 1 :(得分:1)
正如我在评论中提到的那样,您应该在列表中添加新对象后调用notifyDataSetChanged()
方法。
首先,在DataListAdapter
public void addItem(SortFilterProducts product) {
itemLists.add(product);
notifyDataSetChanged();
}
然后,删除personsList.add(products);
方法中的行onCreate()
并添加以下行。
DataListAdapter adapter = (DataListAdapter)list.getAdapter();
adapter.addItem(products);
每当添加新项目时,ListView
都会自动更新。
希望这可能会有所帮助。