Domanda

Hellow this is my first question in stack. Im creating a dynamic grid of this tutorial http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/comment-page-1/

Now it's working pretty good. My layout is composed by a gridView and under this gridView i have a TextView.

The problem is that i want to change the TextView to display different information on each id when the focus changes (on the grid elements). I've tried to use OnFocusChangeListener inside ButtonAdapter, but when trying to get a reference to the textView, it says that findViewById is not implemented.

I wonder how to make a reference in my main activity that allows me to handle my dynamic grid elements. I have the following in onCreate();

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ButtonAdapter(this));

So i want to handle my grid elements from here, any ideas? Thanks

Edit: I've been trying to change different things, but im receiving a NullPointerException from my getView method. I can't find a way to make it work, i'll apreciate any help guys, this is my code:

    public View getView(int position, View convertView, ViewGroup parent) {
    final Button btn;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        btn = new Button(mContext);
        btn.setLayoutParams(new GridView.LayoutParams(100, 55));
        btn.setPadding(8, 8, 8, 8);
    } else {
        btn = (Button) convertView;
    }

    btn.setText(filenames[position]);
    // filenames is an array of strings
    btn.setTextColor(Color.WHITE);
    btn.setId(position);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            TextView vt = (TextView) btn.findViewById(R.id.textView1);
            vt.setText("Button Pressed");
        }
    });

Thanks.

È stato utile?

Soluzione

I think you are on the right track, but I think it would make most sense to take care of it in your adapter. So findViewById() isn't working for you, but it will work if you change it to convertView.findViewById() (or whatever view is returned in your getView method of your Adapter). From there you will be able to manipulate the TextView.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top