我正在尝试在IP Address
中显示ListView
。我创建了ListAdapter
& ArrayAdapter
但它没有正确显示。所以我认为我需要一个CustomAdapter来显示地址。我已经使用TextView
来显示它。我需要适配器来显示显示的值TextView的。
所以帮助我正确的方向:)
感谢您的帮助......
connect.java
public class connect extends ListActivity implements OnClickListener{
WifiApManager wifiApManager;
TextView tv;
Button ipscan;
ListView lv;
@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);
lv = getListView();
}
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){
ArrayAdapter<ClientScanResult> adapter= new ArrayAdapter<ClientScanResult>(connect.this, android.R.layout.simple_list_item_1, clients);
lv.setAdapter(adapter);
tv.setText("WifiApState: " + wifiApManager.getWifiApState() + "\n\n");
tv.append("Clients: \n");
for (ClientScanResult clientScanResult : clients)
{
//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();
}
}
ClientScanResult.java
public class ClientScanResult {
private String IpAddr;
private String HWAddr;
private String Device;
private boolean isReachable;
public ClientScanResult(String ipAddr, String hWAddr, String device, boolean isReachable) {
super();
IpAddr = ipAddr;
HWAddr = hWAddr;
Device = device;
this.setReachable(isReachable);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.IpAddr.toString();
}
public String getIpAddr() {
return IpAddr;
}
public void setIpAddr(String ipAddr) {
IpAddr = ipAddr;
}
public String getHWAddr() {
return HWAddr;
}
public void setHWAddr(String hWAddr) {
HWAddr = hWAddr;
}
public String getDevice() {
return Device;
}
public void setDevice(String device) {
Device = device;
}
public void setReachable(boolean isReachable) {
this.isReachable = isReachable;
}
public boolean isReachable() {
return isReachable;
}
}
注意: 我评论了用于在TextView中显示值的行。
答案 0 :(得分:1)
您可以传递arraylist客户端并在CustomAdapter中使用它。
onPostExecute
AsyncTask
CustomAdapter cus = new CustomAdapter(ActivityName.this,clients);
lv.setAdapter(cus);
CustomAdapter
public class CustomAarrayAdapter extends ArrayAdapter
{
LayoutInflater mInflater;
List<ClientScanResult> resultList;
public CustomArrayAdapter(Context context, List<ClientScanResult> list)
{
super(context,0,list);
resultList = list;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row,parent,false);
// inflate custom layout called row
holder = new ViewHolder();
holder.tv =(TextView) convertView.findViewById(R.id.textView1);
holder.tv1 =(TextView) convertView.findViewById(R.id.textView2);
holder.tv2 =(TextView) convertView.findViewById(R.id.textView3);
// initialize textview
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
ClientScanResult result = (ClientScanResult)resultList.get(position);
holder.tv.setText(result.getIpAddr());
holder.tv1.setText(result.getDevice());
holder.tv2.setText(result.getHWAddr());
// set the name to the text;
return convertView;
}
static class ViewHolder
{
TextView tv,tv1,tv2;
}
}
使用id textView1,textView2和textView3
创建包含3个textviews的row.xml答案 1 :(得分:0)
import android.annotation.TargetApi;
import android.app.ListActivity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ListView3Activity extends ListActivity {
ListView listView;
static final String[] COUNTRIES = {"Argentina",
"Bahamas",
"Bangladesh",
"China",
"Denmark",
"France",
"Egypt",
"Germany",
"Hong Kong",
"Iceland",
"India",
"Indonesia",
"Iraq",
"Israel",
"Italy",
"Malaysia",
"New Zealand",
"Pakistan",
"Singapore",
"United States"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
//Own row layout
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, COUNTRIES);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_LONG).show();
}
});
}
}
你可以从这段代码中汲取灵感......我认为这很简单..如果你对这段代码有任何疑问,请告诉我。