如何将数据传递给getView方法

时间:2018-06-16 20:45:32

标签: java android listview

  

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

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

我在这里添加了我调用方法的代码:

 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);
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您必须创建一个接口,或者您可以使用notifyDataSetChanged取决于使用,如果您正在进行搜索操作然后使用notifyDataSetChanged方法,但如果您想发送一些数据,那么

片段或活动

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() {

}
}

适配器代码

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的任何类型的适配器。