Alexander Ho
Alexander Ho

Reputation: 533

list view error android

this is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ListView listView = (ListView) findViewById(R.id.mylist);

    final Button b=(Button)findViewById(R.id.b);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            while(i<30){
                String stringValue = Integer.toString(i);
            tt[i] = sendPostDataToInternet(stringValue);
            i++;
            }
            //TextView f=(TextView)findViewById(R.id.cc);
            //f.setText(tt[1]+tt[2]+tt[3]);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(),
                    android.R.layout.simple_list_item_1, android.R.id.text1, tt);
            listView.setAdapter(adapter);
        }

    });
       }

I tried to set the data to text and it worked, but when I set it into a listview it gives

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

whats the matter Thanks.

Upvotes: 0

Views: 249

Answers (2)

Barak
Barak

Reputation: 16393

You should use this or activity.this or even getActivity() when you instantiate the adapter.

Since I use fragments a lot, I usually end up using getActivity() so I can reuse a fragment in more than one activity if necessary.

Example:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
    android.R.layout.simple_list_item_1, android.R.id.text1, tt); 

Upvotes: 1

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

in line

ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, tt);

use YOURActivityName.this instead of v.getContext()

Upvotes: 1

Related Questions