在android listview中加载更多按钮不工作/触发

时间:2015-09-17 16:02:24

标签: android listview

我是Android的新手,我试图显示一个评论列表,其中包含更多"加载更多"按钮。但不知怎的,我的按钮没有触发。听众似乎没有做任何事情...... 这是我的代码:

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.
        }

    }
}

}

因此,当我第一次在我的页面上获得时,我得到了前10个评论,但是当我点击我的&#34;加载更多&#34;按钮:没有任何反应。 我已经在getIdTask()上设置了一个分数,因为应该在click事件上调用,我永远不会到达那里。我似乎无法找到为什么我的按钮没有任何反应。 另外,我想可能会多次调用AsyncTask可能是问题,但我尝试了#34; OnCreate&#34;获得没有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>

谢谢

1 个答案:

答案 0 :(得分:0)

我终于理解为什么我的onclick听众根本没有工作/触发,我花了一些时间:-( 我在populateListView方法(第一行)中有这个代码

setContentView(R.layout.activity_comment);

我删除了它,所有听众都会再次工作...... 可能这里有些困惑,说实话我真的不明白: - (