我已经被这个问题困扰了好几天,没有看到任何解决方案。
我通过 php 将 mysql 中的数据传递到 json 数组中。我正在循环遍历 json 并将数据放入哈希图中,我需要将数据集提取为整数,并在我自定义构建的 getView 中的 switch/case 或 if 语句中使用它。
评分数据以字符串形式出现,我需要使用一些 if statemetns 来根据评分设置星数,但我在将其转换为 getView() 中的 int 时遇到问题
任何帮助都将获得评论 = json.getJSONArray(TAG_REVIEWS);
//looping through all the reviews
for(int i=0; i<reviews.length(); i++){
JSONObject r = reviews.getJSONObject(i);
//Storing each JSON items of id and user name from the array in variables
String rating = r.getString(TAG_RATING);
String content = r.getString(TAG_CONTENT);
String userName = r.getString(TAG_USER_NAME);
//new hashmap object to hold the items from JSONArray
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_RATING, rating); //THIS IS THE DATA I WILL NEED AS INT
map.put(TAG_CONTENT, content);
map.put(TAG_USER_NAME, userName);
map.put(TAG_RANK, rank);
//adding items to the arraylist object
reviewList.add(map);
}
}else{
Intent noReviews = new Intent(getApplicationContext(),
AllProductsActivity.class);
//adding flags to close all prior activities
noReviews.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(noReviews);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new CustomReviewListAdapter(
ProductReviews.this, reviewList,
R.layout.list_review_item_layout, new String[] {TAG_CONTENT,
TAG_RANK, TAG_RATING
});
setListAdapter(adapter);
}
对于我的自定义适配器,请参阅下面的所有 IF 语句
我想做的是从哈希图中获取KEY“评级,将其转换为int并执行if语句来设置drawable”
public class CustomReviewListAdapter extends ArrayAdapter<HashMap<String, String>> {
private ArrayList<HashMap<String, String>> data;
private Context context;
private LayoutInflater mInflater;
private int viewId;
private String[] tag;
private static final String TAG_CONTENT = "content";
private static final String TAG_RATING = "rating";
private static final String TAG_USER_NAME = "user_name";
public CustomReviewListAdapter(Context c,
ArrayList<HashMap<String, String>> data,
int viewId, String[] tag) {
super(c, viewId, data );
this.context = c;
this.data= data;
this.viewId = viewId ;
}
@Override
public int getCount() {
return data.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
Holder holder;
if (convertView == null) {
// Inflate the view since it does not exist
if (vi == null) {
mInflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = mInflater.inflate(R.layout.list_review_item_layout, null);
}
holder = new Holder();
holder.textView = (TextView) vi.findViewById(R.id.content);
holder.userName = (TextView) vi.findViewById(R.id.userName);
holder.imageView = (ImageView) vi.findViewById(R.id.num_stars);
vi.setTag(holder);
}else {
holder = (Holder) vi.getTag();
}
HashMap<String, String> currentData = new HashMap<String, String>();
currentData = data.get(position);
if (currentData != null) {
holder.textView.setText(currentData.get(TAG_CONTENT));
holder.userName.setText(currentData.get(TAG_USER_NAME));
//if(s == "5" ){
//holder.imageView.setImageResource(R.drawable.one_half);
//}else if(s == "4"){
//holder.imageView.setImageResource(R.drawable.two_stars);
//}else if(s == "1"){
//holder.imageView.setImageResource(R.drawable.three);
//}else if(s == "2"){
//holder.imageView.setImageResource(R.drawable.three_half);
//}else{
//holder.imageView.setImageResource(R.drawable.five_star);
//}
}
return vi;