[英]How to use CustomAdapter with ListView
I am Trying to Display the IP Address
in a ListView
.I created a ListAdapter
& ArrayAdapter
but it is not displaying properly.So I think i need a CustomAdapter to Display the Addresses.I have already Used TextView
to display it.I need the Adapter to display the values which are displayed in the TextView. 我正在尝试在
ListView
显示IP Address
。我创建了ListAdapter
和ArrayAdapter
但显示不正确。所以我认为我需要一个CustomAdapter来显示地址。我已经使用TextView
来显示它了。我需要适配器以显示在TextView中显示的值。
So Help me in the Right Direction :) 因此,请按正确的方向帮助我:)
Thanks for your Help ... 谢谢你的帮助 ...
connect.java 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 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;
}
}
NOTE: I Commented the Lines which i used to display the values in TextView. 注意:我注释了用于在TextView中显示值的行。
You can pass the arraylist clients and use the same in CustomAdapter. 您可以传递arraylist客户端,并在CustomAdapter中使用它们。
In onPostExecute
of your AsyncTask
在您的
AsyncTask
onPostExecute
中
CustomAdapter cus = new CustomAdapter(ActivityName.this,clients);
lv.setAdapter(cus);
CustomAdapter 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;
}
}
Create row.xml with 3 textviews with id textView1,textView2 and textView3 创建具有ID为textView1,textView2和textView3的3个textview的row.xml
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();
}
});
}
}
You can take idea from this code..i think its pretty simple..tell me if you have any doubt in this code. 您可以从这段代码中汲取灵感。.我认为它非常简单..如果您对此代码有任何疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.