Question

I am Trying to Display IP Address in a ListView.In my code,iam displaying the Ip addresses in a TextView.Now i am trying to set a ListAdapter to display the Contents of the ArrayList.I don't know where can i insert the List adapter.I tried in the onPostExecute But it returned a Error on the ListAdapter Parameter

setListAdapter(new ArrayAdapter<ClientScanResult>(this, android.R.layout.simple_list_item_1, clients));

I already used setText & append to display in TextView.But how to Display the Values in ListAdapter ??

connect.java

public class connect extends Activity implements OnClickListener{
WifiApManager wifiApManager;
TextView tv;
Button ipscan;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connect);
    tv =(TextView) findViewById(R.id.iptv);
    ipscan=(Button) findViewById(R.id.scan);
    ipscan.setOnClickListener(this);  
    ListView lv = (ListView) findViewById(R.id.list);


}
class scan extends AsyncTask<Void, Void, ArrayList<ClientScanResult>>{ 
    public Context context;
    ArrayList<ClientScanResult> clients= new  ArrayList<ClientScanResult>();
    public scan(Context c)  // constructor to take Context
    {
        context = c;   // Initialize your Context variable
    }

    protected ArrayList<ClientScanResult> doInBackground(Void... params) {
        wifiApManager = new WifiApManager(context);  // use the variable here
        return wifiApManager.getClientList(false);

    }

protected void onPostExecute(ArrayList<ClientScanResult> clients){

//tv.setText("WifiApState: " + wifiApManager.getWifiApState() + "\n\n");
    //tv.append("Clients: \n");
for (ClientScanResult clientScanResult : clients) //showin error in clients
    {
        //tv.append("####################\n");
       // tv.append("IpAddr: " + clientScanResult.getIpAddr() + "\n");
        // tv.append("Device: " + clientScanResult.getDevice() + "\n");
       // tv.append("HWAddr: " + clientScanResult.getHWAddr() + "\n");
       // tv.append("isReachable: " + clientScanResult.isReachable()+ "\n");
    }
}
}
@Override
public void onClick(View v) {
    // TODO Click Action...
    scan myScan = new scan(this); // pass the context to the constructor
    myScan.execute();
}
}

NOTE: I Commented the Lines which i used to display the values in TextView.

Was it helpful?

Solution

I tried in the onPostExecute But it returned a Error on the ListAdapter Parameter

Then

setListAdapter(new ArrayAdapter<ClientScanResult>(this, android.R.layout.simple_list_item_1, clients));

But your Activity does not extend ListActivity

http://developer.android.com/reference/android/app/ListActivity.html

setListAdapter is a method of ListActivity.

Replace this by connect.this.

Also follow naming conventions. Name you class like Connect(C caps)

ListView lv; // declare as isntance variable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connect);
lv = (ListView) findViewById(R.id.list); // initialize

In onPostExecute

ArrayAdapter<ClientScanResult> adapter= new ArrayAdapter<ClientScanResult>(connect.this, android.R.layout.simple_list_item_1, clients)
lv.setAdapter(adapter);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top