简体   繁体   English

如何在AsyncTask中使用ListAdapter?

[英]How to use ListAdapter in AsyncTask?

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 我正在尝试在ListView显示IP Address 。在我的代码中,要在TextView显示Ip地址。现在我正试图设置一个ListAdapter以显示ArrayList的内容。我不知道该在哪里插入清单adapter.I试图在onPostExecute但它返回的一个错误ListAdapter参数

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 ?? 我已经使用setText和append在TextView中显示。但是如何在ListAdapter中显示值?

connect.java 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. 注意:我注释了用于在TextView中显示值的行。

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 但是您的活动不会扩展ListActivity

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

setListAdapter is a method of ListActivity . setListAdapterListActivity的方法。

Replace this by connect.this . 更换thisconnect.this

Also follow naming conventions. 还请遵循命名约定。 Name you class like Connect(C caps) 像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 onPostExecute

ArrayAdapter<ClientScanResult> adapter= new ArrayAdapter<ClientScanResult>(connect.this, android.R.layout.simple_list_item_1, clients)
lv.setAdapter(adapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM