listview中带有自定义适配器的Textview没有得到更新

时间:2016-03-17 05:52:18

标签: android listview textview

我有一个带有自定义适配器的列表视图。

以下是我的getView方法

@RouteConfig([
    {path: '/product/:id', component: ProductDetailComponentParam,
     as: 'ProductDetail', data: {isProd: true}}])

export class ProductDetailComponentParam {
    productID: string;
    constructor(params: RouteParams, data: RouteData) {
        this.productID = params.get('id');

        console.log('Is this prod environment', data.get('isProd'));
    }
}

然后我尝试更新代码中的texview @Override public View getView(int position, View convertView, ViewGroup parent) { view = convertView; if (convertView == null) { //inflate the view for each row of listview view = layoutInflater.inflate(R.layout.rating_item, null); mViewHolder = new ViewHolder(); mViewHolder.mstatus = (TextView) view.findViewById(R.id.ratingStatus); mViewHolder.mCheckBox = (CheckBox) view.findViewById(R.id.checkBox); view.setTag(mViewHolder); } else { mViewHolder = (ViewHolder) view.getTag(); } mViewHolder.mstatus.setText("Status"); return view; } ,如下所示,

mStatus

问题是texview正在设置但是在ui中,变化是不可见的。我的意思是,我得到的Toast消息是“New Status”但是在ui中,更改没有受到影响,textview仍然显示旧文本RatingAdapter ratingAdapter = new RatingAdapter(getActivity(), starOneList); TextView status = (TextView) getViewByPosition(0, mListView).findViewById(R.id.ratingStatus); status.setText("New Status"); mListView.setAdapter(ratingAdapter); Toast.makeText(getActivity(), "Log" + status.getText().toString(), Toast.LENGTH_SHORT).show(); ratingAdapter.notifyDataSetChanged(); 。为什么会这样?

5 个答案:

答案 0 :(得分:3)

每次都必须在textview上传递要更新的文本。 你现在每次静态设置textview,即mViewHolder.mstatus.setText(" Status"); 而不是这个你的代码将像这样mViewHolder.mstatus.setText(newtext);其中newtext是一个保存新字符串值的变量

答案 1 :(得分:2)

使用以下代码在更新到适配器后刷新适配器:

youradapter.notifyDataSetChanged();

答案 2 :(得分:1)

在getview方法的textview中设置标记,当你在fragment中调用时,使用yourTextview.getTag()获取标记,然后将文本设置为

答案 3 :(得分:1)

你必须这样定义。

 if (convertView == null) {
                //inflate the view for each row of listview
                view = layoutInflater.inflate(R.layout.rating_item, null);
                mViewHolder = new ViewHolder();
                mViewHolder.setTag(R.id.ratingStatus, mViewHolder.findViewById(R.id.ratingStatus));
                mViewHolder.setTag(R.id.checkBox, mViewHolder.findViewById(R.id.checkBox));
                view.setTag(mViewHolder);
       } else {
                mViewHolder = (ViewHolder) view.getTag();
              }

           mViewHolder.mstatus.setText("Status");
            return view;
        }

答案 4 :(得分:1)

试试这个。

    {
        if (convertView == null) {
            //inflate the view for each row of listview
            view = layoutInflater.inflate(R.layout.rating_item, null);  

            starOneList= HERE_GET_YOUR_LIST_ITEM USING POSITION;  

            mViewHolder = new ViewHolder();
            mViewHolder.mstatus = (TextView) view.findViewById(R.id.ratingStatus);
            mViewHolder.mCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
            view.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) view.getTag();
        }

        mViewHolder.mstatus.setText(starOneList.status); // this is your value that change in activity and only need to notify adapter
        return view;  
}

然后

// if string array
starOneList[POSITION]="New Status";  
//or   if model class
starOneList.get(POSITION).setStatus("New Status")

    RatingAdapter ratingAdapter = new RatingAdapter(getActivity(), starOneList);

     mListView.setAdapter(ratingAdapter);
     Toast.makeText(getActivity(), "Log" +status.getText().toString(), Toast.LENGTH_SHORT).show();
      ratingAdapter.notifyDataSetChanged();