数据的GridView在不同的Android应用程序 [英] Data varying in GridView in Android App
问题描述
我正在开发Android应用程序可显示不同的历史名胜的信息。在我的应用程序,我做了一个网页,评论和评分的地方。该应用程序从数据库中检索用户名,评分和评论,并在GridView显示它。它显示的意见正确数量的但不是显示所有评论它重复一些评论。这里是我的code从数据库中检索数据。
谁能告诉什么是我的code ??
问题 类任务扩展的AsyncTask<字符串,字符串,太虚>
{
私人ProgressDialog progressDialog =新ProgressDialog(Comments.this);
InputStream为= NULL;
字符串结果=;
在preExecute保护无效()
{
如果(获取)
progressDialog.setMessage(检索评论...);
其他
progressDialog.setMessage(发帖评价......); progressDialog.show();
progressDialog.setOnCancelListener(新OnCancelListener()
{
@覆盖
公共无效onCancel(DialogInterface为arg0)
{
task.this.cancel(真);
}
});
} @覆盖
保护无效doInBackground(字符串... PARAMS)
{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httpPost =新HttpPost(url_select); 尝试
{
httpPost.setEntity(新UrlEn codedFormEntity(参数)); HTT presponse HTT presponse = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT presponse.getEntity(); //读取内容
是= httpEntity.getContent();
}
赶上(例外五)
{
Log.e(log_tag,在HTTP连接错误+ e.toString());
} 尝试
{
BR的BufferedReader =新的BufferedReader(新的InputStreamReader(是));
StringBuilder的SB =新的StringBuilder();
串线=;
而((行= br.readLine())!= NULL)
{
sb.append(行+\\ n);
}
is.close();
结果= sb.toString();
}
赶上(例外五)
{
// TODO:处理异常
Log.e(log_tag,错误转换结果+ e.toString());
}
返回null;
}
保护无效onPostExecute(虚空V)
{
尝试
{
如果(获取)
{
NAME =新的ArrayList<串GT;();
注释=新的ArrayList<串GT;();
等级=新的ArrayList<浮球GT;();
JSONArray Jarray =新JSONArray(结果); 的for(int i = 0; I< Jarray.length();我++)
{
JSONObject的Jasonobject =新的JSONObject();
Jasonobject = Jarray.getJSONObject(ⅰ); 字符串名称= NULL;
名称= Jasonobject.getString(名称); name.add(地名);
comment.add(Jasonobject.getString(意见));
rating.add((浮点)Jasonobject.getDouble(等级));
}
CustomGrid适配器=新CustomGrid(Comments.this,姓名,评论,评级);
grid.setAdapter(适配器); 得到= FALSE;
}
progressDialog.dismiss();
}
赶上(例外五)
{
// TODO:处理异常
Log.e(log_tag,错误分析数据+ e.toString());
}
}
}
适配器code:
公共类CustomGrid延伸BaseAdapter {
私人语境mContext;
私人最终的ArrayList<串GT;名称;
私人最终的ArrayList<串GT;评论;
私人最终的ArrayList<浮球GT;评分; 公共CustomGrid(上下文C,ArrayList的<串GT;名,ArrayList的<串GT;发表评论,ArrayList的<浮球GT;评级)
{
mContext = C;
this.name =名称;
this.comment =评论;
this.rating =评级;
}
@覆盖
公众诠释getCount将()
{
// TODO自动生成方法存根
返回comment.size();
}
@覆盖
公共对象的getItem(INT位置)
{
// TODO自动生成方法存根
返回null;
}
@覆盖
众长getItemId(INT位置)
{
// TODO自动生成方法存根
返回0;
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
// TODO自动生成方法存根
查看网格;
LayoutInflater吹气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
如果(convertView == NULL)
{
格=新景(mContext);
格= inflater.inflate(R.layout.grid,NULL);
TextView的textName =(TextView中)grid.findViewById(R.id.grid_name);
TextView的textComment =(TextView中)grid.findViewById(R.id.grid_comment);
的RatingBar ratingBar1 =(的RatingBar)grid.findViewById(R.id.grid_rating);
textName.setText(name.get(位置));
textComment.setText(comment.get(位置));
ratingBar1.setRating(rating.get(位置));
}
其他
{
格=(查看)convertView;
}
返回网格;
}
}
convertView
为空一次。之后膨胀并返回,由于 GridView控件
/ 的ListView
的回收机制,这将是永远不会空的再次的。你在做什么是分配给的TextView
是你的数据集在位置0的内容,然后在相同的内容一遍又一遍的池视图返回。更改 getView
这样的:
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 如果(convertView == NULL){
LayoutInflater吹气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid,NULL);
}
TextView的textName =(TextView中)convertView.findViewById(R.id.grid_name);
TextView的textComment =(TextView中)convertView.findViewById(R.id.grid_comment);
的RatingBar ratingBar1 =(的RatingBar)convertView.findViewById(R.id.grid_rating);
textName.setText(name.get(位置));
textComment.setText(comment.get(位置));
ratingBar1.setRating(rating.get(位置));
返回convertView;
}
还你可能想在 ViewHolder 格局看,这使得你的GridView /的ListView滚动更流畅
I am developing an Android app which displays information of different historical places. In my App, I have made a page to review and rate the place. The app is retrieving usernames, ratings, and comments from database and displaying it in a GridView. It is displaying correct number of comments but instead of displaying all the comments it duplicates some of the comments. Here is my code which retrieve data from database.
Can anyone tell what is the problem with my code??
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(Comments.this);
InputStream is = null ;
String result = "";
protected void onPreExecute()
{
if (get)
progressDialog.setMessage("Retrieving reviews...");
else
progressDialog.setMessage("Posting review...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener()
{
@Override
public void onCancel(DialogInterface arg0)
{
task.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
try
{
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//read content
is = httpEntity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}
catch (Exception e)
{
// TODO: handle exception
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
protected void onPostExecute(Void v)
{
try
{
if(get)
{
name=new ArrayList<String>();
comment=new ArrayList<String>();
rating=new ArrayList<Float>();
JSONArray Jarray = new JSONArray(result);
for(int i=0;i<Jarray.length();i++)
{
JSONObject Jasonobject = new JSONObject();
Jasonobject = Jarray.getJSONObject(i);
String names=null;
names=Jasonobject.getString("name");
name.add(names);
comment.add(Jasonobject.getString("comment"));
rating.add((float)Jasonobject.getDouble("rating"));
}
CustomGrid adapter = new CustomGrid(Comments.this, name,comment,rating);
grid.setAdapter(adapter);
get=false;
}
progressDialog.dismiss();
}
catch (Exception e)
{
// TODO: handle exception
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
Adapter Code:
public class CustomGrid extends BaseAdapter
{
private Context mContext;
private final ArrayList<String> name;
private final ArrayList<String> comment;
private final ArrayList<Float> rating;
public CustomGrid(Context c,ArrayList<String> name, ArrayList<String> comment, ArrayList<Float> rating )
{
mContext = c;
this.name= name;
this.comment = comment;
this.rating=rating;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return comment.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
TextView textName = (TextView) grid.findViewById(R.id.grid_name);
TextView textComment = (TextView) grid.findViewById(R.id.grid_comment);
RatingBar ratingBar1 = (RatingBar)grid.findViewById(R.id.grid_rating);
textName.setText(name.get(position));
textComment.setText(comment.get(position));
ratingBar1.setRating(rating.get(position));
}
else
{
grid = (View) convertView;
}
return grid;
}
}
convertView
is null just once. After you inflate and return it, due to the recycling mechanism of the GridView
/ListView
it will be never null again. What you are doing is assign to your TextView
s the content of your dataset at position 0, and then return on of the pooled view with the same content over and over. Change your getView
like:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid, null);
}
TextView textName = (TextView) convertView.findViewById(R.id.grid_name);
TextView textComment = (TextView) convertView.findViewById(R.id.grid_comment);
RatingBar ratingBar1 = (RatingBar)convertView.findViewById(R.id.grid_rating);
textName.setText(name.get(position));
textComment.setText(comment.get(position));
ratingBar1.setRating(rating.get(position));
return convertView;
}
also you probably want to look in the ViewHolder pattern, which makes your GridView/ListView scroll smoother
这篇关于数据的GridView在不同的Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!