gpt4 book ai didi

java - 如何将数据传递给 getView 方法

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:58 25 4
gpt4 key购买 nike

I'm getting a variable data sent from activity to my adapter by using sendData method, but every time when I try to access it in my getView method it resets to 0, please help. I've tried to debug code to check if the data from an activity is passing and it looks ok. I also created a getNumb method but still, the variable resets to 0. Here is my adapter code:

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

最佳答案

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

关于java - 如何将数据传递给 getView 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50891607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com