在android listview中加载更多按钮不工作/未触发
我是Android新手,我正试图用“加载更多”按钮显示评论列表。但不知怎么的,我的按钮没有被触发。似乎听者什么都没做。。。 这是我的密码:在android listview中加载更多按钮不工作/未触发,android,listview,Android,Listview,我是Android新手,我正试图用“加载更多”按钮显示评论列表。但不知怎么的,我的按钮没有被触发。似乎听者什么都没做。。。 这是我的密码: public class CommentActivity extends AppCompatActivity { ListView listComments; private String FullJson = ""; private static int current_page = 0; private int offset = 0; public
public class CommentActivity extends AppCompatActivity {
ListView listComments;
private String FullJson = "";
private static int current_page = 0;
private int offset = 0;
public int getOffset() {
return current_page * 10;
}
private ProgressDialog pDialog = null;
protected String qid = "", udida="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
listComments = (ListView) findViewById(R.id.listComments);
//get list of comments on this question
Intent intent = getIntent();
qid = intent.getStringExtra(QuestionActivity.EXTRA_QID);
udida = intent.getStringExtra(QuestionActivity.EXTRA_MESSAGE);
FullJson = intent.getStringExtra(QuestionActivity.EXTRA_JSON);
new HttpAsyncTask().execute("http://heycrowd.com/requests/questionWithComments.json?questionId="+qid+"&offset="+getOffset());
// LoadMore button
Button btnLoadMore = (Button) findViewById(R.id.load_more);;
//Listening to Load More button click event
btnLoadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Starting a new async task
new GetIdTask().execute();
}
});
}
public String getIdThread () {
try {
Context ctx = CommentActivity.this.getApplicationContext();
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(ctx);
final String id = adInfo.getId();
return id;
} catch (GooglePlayServicesRepairableException e) {
return "Error 1";
} catch (IOException e) {
return "Error 2";
// Unrecoverable error connecting to Google Play services (e.g.,
// the old version of the service doesn't support getting AdvertisingId).
} catch (GooglePlayServicesNotAvailableException e) {
return"Error 3";
// Google Play services is not available entirely.
}
}
private class GetIdTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// Showing progress dialog before sending http request
pDialog = new ProgressDialog(CommentActivity.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... urls) {
return getIdThread();
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
new HttpAsyncTask().execute("http://heycrowd.com/requests/questionWithComments.json?questionId="+qid+"&offset="+getOffset());
// closing progress dialog
pDialog.dismiss();
}
}
private List<HashMap<String,String>> getListItems(JSONArray FullJson) {
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0; i< FullJson.length() ;i++){
HashMap<String, String> hm = new HashMap<String,String>();
try {
hm.put("text", FullJson.getJSONObject(i).getString("text"));
hm.put("date", FullJson.getJSONObject(i).getString("created_at"));
//get user (picture + name)
JSONObject userObj = FullJson.getJSONObject(i).getJSONObject("user");
hm.put("user_id", userObj.getString("user_id"));
hm.put("user_name", userObj.getString("name"));
hm.put("user_img",userObj.getString("image_url"));
}
catch (JSONException e) {
//some exception handler code.
}
aList.add(hm);
}
return aList;
}
private void populateListView(JSONArray FullJson) {
setContentView(R.layout.activity_comment);
List<HashMap<String,String>> aList = getListItems(FullJson);
ListView listCommentView = (ListView) findViewById(R.id.listComments);
int currentPosition = listCommentView.getFirstVisiblePosition();
CommentAdapter adapter =new CommentAdapter(this, aList );
// Setting the adapter to the listView
listCommentView.setAdapter(adapter);
// Setting new scroll position
listCommentView.setSelectionFromTop(currentPosition + 1, 0);
//get logged user's image (at the moment, default image)
ImageLoader imageLoader = new ImageLoader(this.getApplicationContext());
ImageView image=(ImageView)findViewById(R.id.me_img);
image.setImageResource(R.drawable.ic_launcher);
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return Utils.GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(final String result) {
try {
JSONObject json = new JSONObject(result);
JSONArray commentsArr = json.getJSONObject("question").getJSONArray("comments"); // get comments array
populateListView(commentsArr);
} catch (JSONException e) {
//some exception handler code.
}
}
}
公共类CommentActivity扩展了AppCompatActivity{
列表视图列表注释;
私有字符串FullJson=“”;
私有静态int当前页面=0;
私有整数偏移=0;
公共int getOffset(){
返回当前页面*10;
}
private ProgressDialog pDialog=null;
受保护字符串qid=“”,udida=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
listComments=(ListView)findViewById(R.id.listComments);
//获取有关此问题的评论列表
Intent=getIntent();
qid=intent.getStringExtra(QuestionActivity.EXTRA_qid);
udida=intent.getStringExtra(QuestionActivity.EXTRA_消息);
FullJson=intent.getStringExtra(QuestionActivity.EXTRA_JSON);
新建HttpAsyncTask()。执行(“http://heycrowd.com/requests/questionWithComments.json?questionId=“+qid+”&offset=“+getOffset());
//加载更多按钮
按钮btnLoadMore=(按钮)findViewById(R.id.load_more);;
//正在侦听加载更多按钮单击事件
btnLoadMore.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//启动新的异步任务
新建GetIdTask().execute();
}
});
}
公共字符串getIdThread(){
试一试{
Context ctx=CommentActivity.this.getApplicationContext();
advisingidclient.Info adInfo=advisingidclient.getadvisingidinfo(ctx);
最后一个字符串id=adInfo.getId();
返回id;
}捕获(谷歌游戏服务可修复例外){
返回“错误1”;
}捕获(IOE异常){
返回“错误2”;
//连接到Google Play服务时出现不可恢复的错误(例如。,
//旧版本的服务不支持获取AdvertisingId)。
}捕获(谷歌PlayServicesNotAvailableException){
返回“错误3”;
//Google Play服务并不完全可用。
}
}
私有类GetIdTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
//发送http请求前显示进度对话框
pDialog=新建进度对话框(CommentActivity.this);
pDialog.setMessage(“请稍候…”);
pDialog.setUndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…URL){
返回getIdThread();
}
//onPostExecute显示异步任务的结果。
@凌驾
受保护的void onPostExecute(字符串结果){
新建HttpAsyncTask()。执行(“http://heycrowd.com/requests/questionWithComments.json?questionId=“+qid+”&offset=“+getOffset());
//关闭进度对话框
pDialog.disclose();
}
}
私有列表getListItems(JSONArray FullJson){
//列表中的每一行存储国家名称、货币和国旗
列表列表=新的ArrayList();
for(int i=0;i
}
因此,当我第一次在我的页面上得到评论时,我会得到前10条评论,但当我点击“加载更多”按钮时:什么都没有发生。
我在getIdTask()上设置了一个断点,这个断点应该在单击事件中被调用,但我从来没有实现过。我似乎不明白为什么我的按钮对任何东西都没有反应。
另外,我认为可能会多次调用AsyncTask是个问题,但我尝试了“OnCreate”,在没有AsyncTask的情况下获得前10行,然后什么都不起作用了。。。
我从昨天起就被困在这里了,请帮忙
我还添加了布局XML,以防万一:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Load more comments"
android:id="@+id/load_more"
android:layout_marginBottom="10dp"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listComments"
android:focusable="false"
android:layout_weight="1.0"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/me_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="user"
android:padding="5dp"
/>
<EditText
android:id="@+id/addComment"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="20dp"
android:minLines="3"
android:lines="5"
android:gravity="left|top"
android:scrollbars="vertical"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</LinearLayout>
谢谢你我最后
setContentView(R.layout.activity_comment);