首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将数据传递给getView方法

如何将数据传递给getView方法
EN

Stack Overflow用户
提问于 2018-06-17 04:45:32
回答 2查看 212关注 0票数 1

我正在使用sendData方法获取从activity发送到我的适配器的变量数据,但每次我尝试在getView方法中访问它时,它都会重置为0,请帮助。我试着调试代码,以检查来自活动的数据是否通过,并且看起来没有问题。我还创建了一个getNumb方法,但仍然将变量重置为0。下面是我的适配器代码:

代码语言:javascript
复制
public class WorkoutListAdapterTwo extends BaseAdapter {

private int y;

public WorkoutListAdapterTwo() {

}

public int sendData(int x){

    this.y = x;
    return y;
}

public int getNumb(){
    return this.y;
}



private static LayoutInflater mLayoutInflater = null;

public WorkoutListAdapterTwo(Activity ctx) {
    this.mLayoutInflater = ctx.getLayoutInflater();
}

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

@Override
public Object getItem(int position) {
    return WorkoutContentTwo.WORKOUTSTWO.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    int j = getNumb();
    WorkoutTwo workout = (WorkoutTwo) getItem(j);

    String [] arrOfStrings = workout.name.split(",");

    if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.adapter_workout_row, parent, false);

        holder = new ViewHolder();
        holder.id = (TextView) convertView.findViewById(R.id.workout_id);
        holder.name = (TextView) convertView.findViewById(R.id.workout_name);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Set the content for the ListView row
    //holder.id.setText(workout.id);
    //holder.name.setText(arrOfStrings[i]);
    holder.id.setText(Integer.toString(position+1));
    holder.name.setText(workout.ArrList[position]);


    // Set the color for the ListView row
    holder.id.setBackgroundColor(workout.dark);
    holder.name.setBackgroundColor(workout.light);

    return convertView;
}

下面我在调用我的方法的地方添加代码:

代码语言:javascript
复制
 public void onItemSelected(int position) {
    // Start the detail activity for the selected workout ID.
    Intent detailIntent = new Intent(this, WorkoutDetailActivityTwo.class);
    detailIntent.putExtra(WorkoutDetailFragmentTwo.ARG_WORKOUT_POS, position);
    WorkoutListAdapterTwo newAdd = new WorkoutListAdapterTwo();
    newAdd.sendData(position);
    newAdd.notifyDataSetChanged();
    startActivity(detailIntent);
}
EN

回答 2

Stack Overflow用户

发布于 2018-06-17 05:07:45

您是否通知适配器在更新该值后更新视图?如果直到适配器完成第一次绘制后才调用sendData(),则需要调用notifyDataSetChanged(),以便适配器知道应该使用新值来更新视图。

票数 0
EN

Stack Overflow用户

发布于 2018-06-17 05:25:22

您必须创建一个接口,或者您可以根据使用情况使用notifyDataSetChanged。如果您正在执行搜索操作,则使用notifyDataSetChanged方法,但是如果您想发送一些数据,则

片段或活动

代码语言:javascript
复制
public class ExampleFragment extends Fragment implements MyAdapter.SuccessResponse{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View contentView  = inflater.inflate(R.layout.my_layout, container, false);
    MyAdapter myAdapter = new MyAdapter(getActivity(), 0);
    myAdapter.successResponse = this;
    return contentView;
}

@Override
public void onSuccess() {

}
}

适配器代码

代码语言:javascript
复制
class MyAdapter extends ArrayAdapter{
SuccessResponse successResponse;

public MyAdapter(Context context, int resource) {
    super(context, resource);
}

public interface SuccessResponse{
    void onSuccess();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //ur views
    linearLayout.setOnClickListener(new View.OnClickListener{
        @Override
        public void onClick (View view){
            if(successResponse!=null)
                successResponse.onSuccess();
        }
    })
}

}

您可以使用我在这里使用arrayadaper任何类型的适配器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50891607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档