列表视图平滑滚动到异步任务中的特定位置不起作用

时间:2015-08-19 05:38:03

标签: android json listview scroll

我知道这个问题之前已被问过很多次但是我的问题有点不同。

我正在开发一个项目,我从Webservice获取JSON数据。在这个应用程序中我们可以投票到不同类型的食品项目,列表视图将根据投票排序(最大投票到最小投票)。在投票上单击任何列表项列表视图刷新并显示新的已排序项目列表。

我得到的JSON也提供了我投​​票的最新排序项目的索引位置。

所以现在我获得了投票项目的位置,我想在listview重新加载后将其滚动到该特定位置。我试过通过互联网提供的所有方法,但它不适合我。

请帮助这是我的代码。

这是我的RemoteDataTask异步类代码

public class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(ItemRankActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Loading All Awesomeness of " + title);

        // Set progressdialog message
        mProgressDialog
                .setMessage("Wait.. While we Generating the Best Dishes for you.");
        // Show progressdialog
        mProgressDialog.show();
    }

    protected Void doInBackground(Void... params) {
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(URL, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                DishesArray = jsonObj.getJSONArray(TAG_DATA);

                // looping through All Contacts
                for (int i = 0; i < DishesArray.length(); i++) {
                    JSONObject c = DishesArray.getJSONObject(i);
                    arrayList.add(new ItemRankData(c.getString(TAG_PIC), c
                            .getString(TAG_TITLE), c
                            .getString(TAG_RESTAURANT), c
                            .getString(TAG_VOTES), c.getString(TAG_DISHID),
                            c.getString(TAG_FBID),
                            c.getString(TAG_IsVOTED), c
                                    .getString(TAG_IsTOP), c
                                    .getString(TAG_TopImage), c
                                    .getString(TAG_Position)));
                    if (c.getString(TAG_IsTOP).equalsIgnoreCase("yes")) {
                        isTop = "yes";
                        Log.e("Is it on TOP", isTop);

                        if (c.getString(TAG_TopImage) != "") {
                            TopImageURL = c.getString(TAG_TopImage);
                            Log.e("Top image URL", TopImageURL);
                            VotedPosition = c.getString("position");
                        }
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        Log.v("post", "After query post");

        Picasso.with(mContext).load(TopImageURL).into(img_item_image);
        PositionOfVote = Integer.parseInt(VotedPosition);
        lv_items_lists.smoothScrollToPosition(PositionOfVote);
        adapter.notifyDataSetChanged();

        ClassDataManager.setListViewHeightBasedOnChildren(lv_items_lists);
        Log.e("POST Execute Hit", "True");


        mProgressDialog.dismiss();

    }
}

我的自定义适配器类代码

public class ItemRankAdapter extends BaseAdapter {
String URL;
JSONArray BestCatogryArray = null;
private LayoutInflater inflater = null;
private ArrayList<ItemRankData> mlist;
private Context context;
ProgressDialog mProgressDialog;
private static String TAG_DATA = "data";
private static String TAG_VOTES = "votes";
JSONArray DishesArray = null;
String VOTES;
ViewHolder holder;
VOTING voting = new VOTING();
int POS;
public ItemRankAdapter(Context c, ArrayList<ItemRankData> mlist) {
    this.context = c;
    this.mlist = mlist;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressLint("DefaultLocale")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ImageLoader imageLoader;
    imageLoader = ImageLoader.getInstance();
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.custom_item_rank, parent,
                false);
        holder = new ViewHolder();
        holder.tv_item_serialNo = (TextView) convertView
                .findViewById(R.id.tv_item_serialNo);
        holder.tv_item_name = (TextView) convertView
                .findViewById(R.id.tv_item_name);
        holder.tv_item_restaurant = (TextView) convertView
                .findViewById(R.id.tv_item_restaurant);
        holder.tv_item_like_count = (TextView) convertView
                .findViewById(R.id.tv_item_like_count);
        holder.img_logo = (ImageView) convertView
                .findViewById(R.id.img_logo);
        holder.img_top_position = (ImageView) convertView
                .findViewById(R.id.img_top_position);
        holder.ll_likes = (LinearLayout) convertView
                .findViewById(R.id.ll_likes);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final ItemRankData rankData = mlist.get(position);
    holder.tv_item_name.setText(rankData.itemName);
    holder.tv_item_restaurant.setText(rankData.itemRestaurantName);
    holder.tv_item_like_count.setText(rankData.itemLikes);
    Log.e("Position Value is " + position,
            " And The Name of Item Along with it is " + rankData.itemName);
    String imageUrl = rankData.itemRetaurantLogo;
    // imageLoader.displayImage(imageUrl, holder.img_logo);
    Picasso.with(context).load(imageUrl).into(holder.img_logo);

    if (position == 0) {
        holder.tv_item_serialNo.setVisibility(View.GONE);
        holder.img_top_position.setVisibility(View.VISIBLE);

    } else {
        holder.tv_item_serialNo.setVisibility(View.VISIBLE);
        holder.img_top_position.setVisibility(View.GONE);
        String s1 = String.format("%02d", (position + 1));
        holder.tv_item_serialNo.setText(s1);

    }
    if (rankData.isVoted.equalsIgnoreCase("yes")) {
        holder.ll_likes
                .setBackgroundResource(R.drawable.top_selected_border);
        holder.tv_item_like_count.setTextColor(context.getResources()
                .getColor(R.color.text_selected));
    } else {
        holder.ll_likes.setBackgroundResource(R.drawable.normal_border);
        holder.tv_item_like_count.setTextColor(context.getResources()
                .getColor(R.color.text_normal));

    }

    holder.ll_likes.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String dishId = rankData.dishId;
            URL = "http://Here I Write My URL?"
                    + "dishid="
                    + dishId
                    + "&"
                    + "fbid="
                    + PrefrencesClass.getStringPreference(context,
                            Constants.APPSPREF, Constants.USER_ID);
            new DataTask().execute();
            ((ItemRankActivity) context).UpdateRemoteDataRankActivity();
        }

    });

    return convertView;

}

static class ViewHolder {
    TextView tv_item_serialNo;
    TextView tv_item_name;
    TextView tv_item_restaurant;
    TextView tv_item_like_count;
    ImageView img_logo;
    LinearLayout ll_add;
    ImageView img_top_position;
    LinearLayout ll_likes;
    ImageView img_item_image;
}

//The Class to Send VOTE update URL to Server
private class DataTask extends AsyncTask<Void, Void, Void> {

    protected Void doInBackground(Void... params) {
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(URL, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);

        return null;

    }

}}

请帮助我,我正在努力解决这个问题的方法和地点 任何帮助都会很棒:)

2 个答案:

答案 0 :(得分:2)

lv_items_lists.setSelection(PositionOfVote);

后试试
adapter.notifyDataSetChanged();

我希望它能帮到你

答案 1 :(得分:0)

试试这个。

lv_items_lists.setSelection(PositionOfVote);

而不是

lv_items_lists.smoothScrollToPosition(PositionOfVote);

我希望它有所帮助!