我有一个ListView,它显示从服务器获取的项目列表。我用EditText创建了一个ALertDialog,它接受用户输入并进行API调用,并将此用户输入添加到items服务器。新的更新项目再次显示在ListView中。
当列表显示正确显示时,问题是最终的。但是当用户输入发布时,它会随机显示,而不是显示在列表的末尾。
这是我的代码: -
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
boolean worked = true;
postedString = input1.getText().toString();
if(postedString .length()==0 || postedString =="")
{
Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
input1.findFocus();
worked = false;
}
else if(worked && postedString !="")
{
dialog.dismiss();
pd = new ProgressDialog(Details.this);
pd.setMessage("Posting..");
pd.show();
pd.setCancelable(true);
Post(postedString );
}
}
private void Post(String postedString)
{
// TODO Auto-generated method stub
String postCommentUrl = EndPoints.PostURL;
new PostComment().execute(postCommentUrl);
String getRequestForComments = myurl;
new FetchItems().execute(getRequest);
}
})
private class PostComment extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
HttpResponse response =null;
String resultString = "";
String myResponseBody = "" ;
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost request = new HttpPost(params[0]);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(8);
nameValuePair.add(my params)
try
{
request.setEntity(new UrlEncodedFormEntity(nameValuePair));
response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()== 200)
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = entity.getContent();
myResponseBody = convertToString(inputStream);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return myResponseBody;
}
private class FetchItems extends AsyncTask<String,String, Void>
{
// TODO Auto-generated method stub
@Override
protected Void doInBackground(String... params)
{
// TODO Auto-generated method stub
HttpResponse response =null;
String resultString = "";
String myResponseBody = "" ;
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpGet request = new HttpGet(params[0]);
try
{
response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()== 200)
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = entity.getContent();
myResponseBody = convertToString(inputStream);
try {
JSONObject jsonResponse = new JSONObject(myResponseBody);
JSONArray mtUsers = jsonResponse.getJSONArray("ListOfComments");
for (int i = 0; i < mtUsers.length(); i++)
{
JSONObject menuObject = mtUsers.getJSONObject(i);
String authorName= parsed code
map = new HashMap<String,String>();
map.put(my params)
myList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Collections.reverse(myList);
getList = myList;
}
}
}
catch(Exception e)
{
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
if(pd!=null && pd.isShowing())
pd.dismiss();
itemsAdapter = (ListAdapter) new CAdapter(Details.this, getList);
myList.setAdapter(itemsAdapter);
ListViewInScrollViewHeight.setListViewHeightBasedOnChildren(myList);
myList.invalidate();
myList.refreshDrawableState();
myList.post(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
myList.setSelection(itemsAdapter.getCount()-1);
}
});
}
//适配器
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
LazyViewHolder viewHolder;
if(convertView==null)
{
vi = inflater.inflate(com.zevenpooja.attini.R.layout.comments_row,null);
viewHolder = new LazyViewHolder();
viewHolder.title = (TextView)vi.findViewById(R.id.title); // title
viewHolder.description = (TextView)vi.findViewById(R.id.description); // artist name
viewHolder.pubDate = (TextView)vi.findViewById(R.id.txtPubDate);
vi.setTag(viewHolder);
}
else
{
viewHolder = (LazyViewHolder)vi.getTag();
}
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
//Set title
String myTitle =song.get(Details.AUTHORDISPLAYNAME);
StringBuilder sb = new StringBuilder(myTitle);
int i = 0;
while ((i = sb.indexOf(" ", i + 40)) != -1)
{
sb.replace(i, i + 1, "\n");
}
viewHolder.title.setText(sb.toString());
//Set description
String mydescription = song.get(Details.TEXT);
viewHolder.description.setText(mydescription);
// Set image
// String bitmapUrl = song.get(Home.avatarUrl);
//Set date
String myDate = song.get(NewsDetails.DATEUPDATED);
final String OLD_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
final String NEW_FORMAT = "HH:mm, MMMM dd, yyyy";
String newDate ="";
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
try
{
Date d = sdf.parse(myDate);
sdf.applyLocalizedPattern(NEW_FORMAT);
newDate = sdf.format(d);
viewHolder.pubDate.setText(newDate);
}
catch (java.text.ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return vi;
}
答案 0 :(得分:0)
您检查了返回的JSONArray吗?它是否包含右边的项目?如果不是,那么问题是服务器端脚本如何查询数据库。例如,如果您要查询mysql数据库,则可以使用ORDER BY id DESC
按ID对结果进行排序。