ArrayAdapter 构建 ArrayList 未正确处理点击事件

ArrayAdapter building an ArrayList not handling click event correctly

我基本上有 1 个 activity 启动另一个并构建一个 ArrayList 来填充新的 activity。我正在尝试处理列表点击事件,并从列表中传回被点击的图片或文字名称。
这是我的 ArrayAdapter:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    StatesHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new StatesHolder();
        holder.statePic = (ImageView) row.findViewById(R.id.imgStatePic);
        holder.stateName = (TextView) row.findViewById(R.id.txtStateName);

        row.setTag(holder);
    } else {
        holder = (StatesHolder) row.getTag();
    }

    States states = data.get(position);
    holder.stateName.setText(states.StateName);
    holder.statePic.setImageBitmap(states.StatePic);

    row.setClickable(true);
    row.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(context, ActivityLocalReps.class);
            //intent.putExtras("StateName",  holder.stateName);  ** holder.stateName is not accessible here, I somehow need this info though.
            context.startActivity(intent);
        }
    });

    return row;
}    

如果我能在这次通话中回传一些东西就好了:

Intent intent = new Intent(context, ActivityLocalReps.class(goodStuffINeed));

我要回电的 activity 正是启动这个新的前一个 activity。我需要以某种方式使新的 Activity 构造函数重载点击项信息以不同地流经原始 class,或者找出实际的 picture/value 或 text/value 被点击.那class:

boolean userSelectedCustomState = false;
String userSelectedState = "";
public ActivityLocalReps(String state){
    userSelectedCustomState = true;
    userSelectedState = state;
}
public ActivityLocalReps(){
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_reps);

    String imageName;
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            imageName = null;
        } else {
            imageName = extras.getString("StateName");
        }
    } else {
        imageName = (String) savedInstanceState.getSerializable("StateName");
    }    

我想我在这里遗漏了一些东西,或者没有设置正确的东西来处理最终 class 中的这个点击操作?感谢您的帮助!
**编辑 1:

 public void setStatesAdapter(ArrayList<States> allStates) {
    ChangeStateAdapter adapter = new ChangeStateAdapter(this, R.layout.list_states, allStates);
    ListView statesListView = (ListView) findViewById(R.id.listView_States);

    statesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getApplicationContext(), ActivityLocalReps.class);
            intent.putExtras("StateName", data.get(position).stateName);
            getApplicationContext().startActivity(intent);
        }
    });

    statesListView.setAdapter(adapter);
}    

目前这条线不可用,似乎是我需要得到的:

intent.putExtras("StateName", data.get(position).stateName);

不使用 OnClickListener,而是使用 OnItemClickListener。它对 Adapters 效果更好。例如:

mListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(context, ActivityLocalReps.class);
        intent.putExtras("StateName", data.get(position).stateName);
        context.startActivity(intent);
    }
});

您应该避免尝试直接使用 ViewHolders(StatesHolder,在您的情况下)或适配器的行。而是使用支持视图的数据。这是一个更好的模式。

EDIT 你是对的,你不能在你的 getView() 方法中调用这段代码。它需要在您创建适配器的位置并将其设置在列表视图上(如您在编辑中所示)。要回答有关如何获取数据的问题,您可以调用 Adapter class 的以下方法(您应该重写):

@Override
public States getItem(int position) {
    return data.get(position);
}

确保您也覆盖了 getCount()

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

除非您这样做,否则您的适配器将无法正常工作。