Grizz
Grizz

Reputation: 1

Error setListAdapter in simple program

I'm new to android programming and I still have a few things that I don't understand. For example, I'm with a program that when you click on a button, it tries to find any devices near. The problem is that I don't know how to solve a mistake I seem to have made from the beggining. This is what I have

This is the main activity:

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void buscar(View view){
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    List<String> s = new ArrayList<String>();
    for(BluetoothDevice bt : pairedDevices)
       s.add(bt.getName());     
    setListAdapter(new ArrayAdapter<String>(this, R.id.list, s));
}
}

And here is the xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/botonvinculo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:onClick="buscar"
    android:text="Buscar dispositivos" />

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/botonvinculo"
    android:layout_below="@+id/botonvinculo" >
</ListView>

</RelativeLayout>

The thing is that I cannot make the call to setListAdapter unless my main activity extends from ListActivity, and even trying so, it returns an error saying "You must have a ListView whose attribute is "android.R.id.list".

I'd appreciate any help. Thank you very much.

Upvotes: 0

Views: 113

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

Your class extends Activity. setListAdapter is a method of ListActivity

ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 lv = (ListView)findViewById(R.id.list);
}  

Then

public void buscar(View view){
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    List<String> s = new ArrayList<String>();
    for(BluetoothDevice bt : pairedDevices)
       s.add(bt.getName());     
     ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this,android.R.layout.simple.list_item_1,s);
     lv.setAdapter(adapter);
}

Upvotes: 0

Apoorv
Apoorv

Reputation: 13520

If you use ListActivity then you should use android:id="@android:id/list" in your XML instead of android:id="@+id/list" or else you can get an object of ListView in your XML like

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

and then use list.setAdapter

Upvotes: 2

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

setListAdapter will work only when your activity extends from ListActivity. use this in your case

listview.setAdapter();

if you want to use setListAdapter() only make change these things

public class MainActivity extends Activity

to

public class MainActivity extends ListActivity 

and android:id="@android:id/list" for your list view in xml

Upvotes: 0

Related Questions