ListView适配器和onClickListener

时间:2015-05-13 18:44:31

标签: android listview

如何为ListView中的每个元素设置onClickListener for button? 当我做这样的事情时:

   public View getView(final int position, View convertView, ViewGroup parent){
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.element, parent, false);
        }

        mButton = (Button) convertView.findViewById(R.id.button);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mButton.setText("Clicked");
            }
        });

        return convertView;
    }

当我按下按钮时,我得到了这个错误:

android.content.res.Resources$NotFoundException: String resource ID #0x4c
            at android.content.res.Resources.getText(Resources.java:286)
            at android.widget.TextView.setText(TextView.java:4148)
            at the.vizimir.onezone.adapters.PostListViewAdapter$1.onClick(ListViewAdapter.java:119)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19858)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

更新: 我没有得到错误但是在点击按钮文本之后不会在我按下的元素中进行操作,而是在其他情况下进行操作。

public class PostListViewAdapter extends ArrayAdapter<Post> {
    private List<Post> mPostList;
    private Context mContext;

    Button mPlusImageButton;
    TextView mTextTextView;


    public PostListViewAdapter(List<Post> postList, Context context){
        super(context, R.layout.post, postList);
        mPostList = postList;
        mContext = context;

    }

    public View getView(final int position, View convertView, ViewGroup parent){
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.post, parent, false);
        }
        mPlusImageButton = (Button) convertView.findViewById(R.id.plusImageButton);
        mTextTextView = (TextView) convertView.findViewById(R.id.textTextView);

        String mText = mPostList.get(position).getText();
        mTextTextView.setText(mText);

        int a = position;
        mPlusImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlusImageButton.setText("A");
            }
        });

        return convertView;
    }

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

3 个答案:

答案 0 :(得分:2)

也许你应该尝试使用:

Button thisButton = (Button) v; 
如果你完全确定问题是onClick()

,那么

而不是直接使用它

所以,我找到了一个时间,并做了一个简单的例子,它有效: MainActivity.class:

public class MainActivity extends Activity {

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

        ListView listView = (ListView) findViewById(R.id.listView);

        ArrayList<String> items = new ArrayList<String>();
        items.add("1");
        items.add("2");
        items.add("3");
        TestAdapter adapter = new TestAdapter(this, R.layout.list_item, R.id.title, items);
        listView.setAdapter(adapter);
    }

}

TestAdapter.class(我知道我没有使用ViewHolder模式等等,我只是根据你的例子创建了一个类):

public class TestAdapter extends ArrayAdapter<String>{

    private List<String> items;
    private Context mContext;

    public TestAdapter(Context context, int resource, int textViewResourceId,
            List<String> items) {
        super(context, resource, textViewResourceId, items);
        this.items = items;
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_item, parent, false);
            }

            final Button mButton = (Button) convertView.findViewById(R.id.button);
            TextView mTitle = (TextView) convertView.findViewById(R.id.title);

            mTitle.setText(items.get(position));

            mButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    mButton.setText("clicked");
                }

            });

            return convertView;
    }

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

}

enter image description here

所以我需要你的所有代码才能看到发生了什么

编辑: 当你在错误的Button上获得文本时,你需要在getView中创建它们(就像在我的帖子中一样),而不是在你的适配器类中创建它。

答案 1 :(得分:1)

您不需要使用按钮。 对于列表视图,使用onListItemClick。它是ListFragment内置的一种方法。否则使用listview.setOnItemClickListener();希望有所帮助。它们采用参数ListView l,View v,int position,long id

答案 2 :(得分:0)

mButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        mButton.setText("clicked");
        notifyDataSetChanged();
    }
});