Question

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.

So Help me in the Right Direction :)

Thanks for your Help ...

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; 
} 
}

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

Was it helpful?

Solution

You can pass the arraylist clients and use the same in CustomAdapter.

In onPostExecute of your 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;
} 
}

Create row.xml with 3 textviews with id textView1,textView2 and textView3

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top