为什么我的CustomAdapter没有被使用?

时间:2013-05-14 00:07:54

标签: android listview listadapter

我创建了一个名为FTPSite的类,其中包含名称,地址,用户名,密码,端口,被动模式的getter和setter。我正在尝试通过使用自定义适配器来显示站点名称来创建站点管理器,但我只是将包名称和数字作为列表项。

public class SiteManager extends Activity {
    DBAdapter myDb;
    public FTPClient mFTPClient = null;

    public EditText sitename;
    public EditText address;
    public EditText username;
    public EditText password;
    public EditText port;
    public CheckBox passive;
    public ListView site_list;
    public TextView tview;
    public Button clr;
    public Button test;
    public Button savesite;
    public Button close;
    public Button connect;

    String _name;
    String _address;
    String _username;
    String _password;
    int _port;
    boolean _passive;
    List<FTPSite> model = new ArrayList<FTPSite>();
    SiteAdapter adapter;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.site_manager);

        site_list = (ListView) findViewById(R.id.siteList);

        adapter = new SiteAdapter();
        site_list.setAdapter(adapter);
        sitename = (EditText) findViewById(R.id.dialogsitename);
        address = (EditText) findViewById(R.id.dialogaddress);
        username = (EditText) findViewById(R.id.dialogusername);
        password = (EditText) findViewById(R.id.dialogpassword);
        port = (EditText) findViewById(R.id.dialogport);
        passive = (CheckBox) findViewById(R.id.dialogpassive);
        close = (Button) findViewById(R.id.closeBtn);
        connect = (Button) findViewById(R.id.connectBtn);
        clr = (Button) findViewById(R.id.clrBtn);
        test = (Button) findViewById(R.id.testBtn);

        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                siteManager();
            }
        });
        savesite = (Button) findViewById(R.id.saveSite);
        savesite.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                siteManager();
            }
        });
        openDb();
        displayRecords();
    }

    private void openDb() {
        myDb = new DBAdapter(this);
        myDb.open();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        closeDb();
    }

    private void closeDb() {
        myDb.close();
    }

    public void siteManager() {

        connect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent returnResult = new Intent();
                returnResult.putExtra("ftpname", _name);
                returnResult.putExtra("ftpaddress", _address);
                returnResult.putExtra("ftpusername", _username);
                returnResult.putExtra("ftppassword", _password);
                returnResult.putExtra("ftpport", _port);
                setResult(RESULT_OK, returnResult);
                finish();

            }
        });

        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });

        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                _name = sitename.getText().toString();
                _address = address.getText().toString();
                _username = username.getText().toString();
                _password = password.getText().toString();
                _port = Integer.parseInt(port.getText().toString());
                _passive = false;
                if (passive.isChecked()) {
                    _passive = true;
                }

                boolean status = ftpConnect(_address, _username, _password,
                        _port);
                ftpDisconnect();

                if (status == true) {
                    Toast.makeText(SiteManager.this, "Connection Succesful",
                            Toast.LENGTH_LONG).show();
                    savesite.setVisibility(0);
                } else {
                    Toast.makeText(SiteManager.this,
                            "Connection Failed:" + status, Toast.LENGTH_LONG)
                            .show();

                }
            }
        });

        savesite.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                _name = sitename.getText().toString();
                _address = address.getText().toString();
                _username = username.getText().toString();
                _password = password.getText().toString();
                _port = Integer.parseInt(port.getText().toString());
                long newId = myDb.insertRow(_name, _address, _username,
                        _password, 21);
                FTPSite tmpSite = new FTPSite();
                tmpSite.setName(_name);
                tmpSite.setAddress(_address);
                tmpSite.setUsername(_username);
                tmpSite.setPassword(_password);
                tmpSite.setPort(_port);
                model.add(tmpSite);
                adapter.notifyDataSetChanged();
                Toast.makeText(SiteManager.this, tmpSite.getName(),
                        Toast.LENGTH_SHORT).show(); // Shows the correct name
                // displayRecords();
            }
        });

        clr.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                clearAll();
            }
        });

    }

    public void displayRecords() {

        Cursor cursor = myDb.getAllRows();
        // displayRecordSet(cursor);
    }

    protected void displayRecordSet(Cursor c) {
        String msg = "";

        if (c.moveToFirst()) {
            do {
                int id = c.getInt(0);
                _name = c.getString(1);
                _address = c.getString(2);
                _username = c.getString(3);
                _password = c.getString(4);
                _port = c.getInt(5);

                msg += "id=" + id + "\n";
                msg += ", name=" + _name + "\n";
                msg += ", address=" + _address + "\n";
                msg += ", username=" + _username + "\n";
                msg += ", password=" + _password + "\n";
                msg += ", port=" + _port + "\n";

            } while (c.moveToNext());
        }
        c.close();
    }

    public void clearAll() {
        myDb.deleteAll();
    }

    public boolean ftpConnect(String host, String username, String password,
            int port) {
        try {
            mFTPClient = new FTPClient();
            // connecting to the host
            mFTPClient.connect(host, port);

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                mFTPClient.enterLocalPassiveMode();
                return status;
            }
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not connect to host " + host );
        }

        return false;
    }

    public boolean ftpDisconnect() {
        try {
            mFTPClient.logout();
            mFTPClient.disconnect();
            return true;
        } catch (Exception e) {
            // Log.d(TAG,
            // "Error occurred while disconnecting from ftp server.");
        }

        return false;
    }



class SiteAdapter extends ArrayAdapter<FTPSite> {
            public SiteAdapter(Context context, int resource, int textViewResourceId, List<T> objects)    {
    super(context,resource,textViewResourceId,objects);  
}
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.siterow, null);
            }
            FTPSite s = model.get(position);
            ((TextView) row.findViewById(R.id.title)).setText(s.getName());

            return (row);
        }
    }


  FTPSite.java


    public class FTPSite {
        private String name = "";
        private String address = "";
        private String username = "";
        private String password = "";
        private int port = 21;
        private boolean passive = false;

        public FTPSite() {
            // TODO Auto-generated constructor stub
        }

        public String getName() {
            return (name);
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return (address);
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getUsername() {
            return (username);
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return (password);
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public int getPort() {
            return (port);
        }

        public void setPort(int port) {
            this.port = port;
        }

        public boolean isPassive() {
            return (passive);
        }

        public void setPassive(boolean passive) {
            this.passive = passive;
        }

    }

我的siterow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:minHeight="?android:attr/listPreferredItemHeight"
    />

修改

List<FTPSite> model = new ArrayList<FTPSite>();
    ArrayAdapter<FTPSite> adapter;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.site_manager);

        site_list = (ListView) findViewById(R.id.siteList);

        adapter = new SiteAdapter(SiteManager.this, R.layout.siterow,
                R.id.title, model);
        site_list.setAdapter(adapter);
        sitename = (EditText) findViewById(R.id.dialogsitename);
        address = (EditText) findViewById(R.id.dialogaddress);
        username = (EditText) findViewById(R.id.dialogusername);
        password = (EditText) findViewById(R.id.dialogpassword);
        port = (EditText) findViewById(R.id.dialogport);
        passive = (CheckBox) findViewById(R.id.dialogpassive);
        close = (Button) findViewById(R.id.closeBtn);
        connect = (Button) findViewById(R.id.connectBtn);
        clr = (Button) findViewById(R.id.clrBtn);
        test = (Button) findViewById(R.id.testBtn);

        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                siteManager();
            }
        });
        savesite = (Button) findViewById(R.id.saveSite);
        savesite.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                siteManager();
            }
        });
        openDb();
        displayRecords();
    }

    private void openDb() {
        myDb = new DBAdapter(this);
        myDb.open();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        closeDb();
    }

    private void closeDb() {
        myDb.close();
    }

    public void siteManager() {

        connect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent returnResult = new Intent();
                returnResult.putExtra("ftpname", _name);
                returnResult.putExtra("ftpaddress", _address);
                returnResult.putExtra("ftpusername", _username);
                returnResult.putExtra("ftppassword", _password);
                returnResult.putExtra("ftpport", _port);
                setResult(RESULT_OK, returnResult);
                finish();

            }
        });

        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });

        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                _name = sitename.getText().toString();
                _address = address.getText().toString();
                _username = username.getText().toString();
                _password = password.getText().toString();
                _port = Integer.parseInt(port.getText().toString());
                _passive = false;
                if (passive.isChecked()) {
                    _passive = true;
                }

                boolean status = ftpConnect(_address, _username, _password,
                        _port);
                ftpDisconnect();

                if (status == true) {
                    Toast.makeText(SiteManager.this, "Connection Succesful",
                            Toast.LENGTH_LONG).show();
                    savesite.setVisibility(0);
                } else {
                    Toast.makeText(SiteManager.this,
                            "Connection Failed:" + status, Toast.LENGTH_LONG)
                            .show();

                }
            }
        });

        savesite.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                _name = sitename.getText().toString();
                _address = address.getText().toString();
                _username = username.getText().toString();
                _password = password.getText().toString();
                _port = Integer.parseInt(port.getText().toString());
                long newId = myDb.insertRow(_name, _address, _username,
                        _password, 21);
                FTPSite tmpSite = new FTPSite();
                tmpSite.setName(_name);
                tmpSite.setAddress(_address);
                tmpSite.setUsername(_username);
                tmpSite.setPassword(_password);
                tmpSite.setPort(_port);
                model.add(tmpSite);
                adapter.notifyDataSetChanged();
                Toast.makeText(SiteManager.this, tmpSite.getName(),
                        Toast.LENGTH_SHORT).show();
                // displayRecords();
            }
        });

        clr.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                clearAll();
            }
        });

    }

    class SiteAdapter extends ArrayAdapter<FTPSite> {
        public SiteAdapter(Context context, int resource,
                int textViewResourceId, List<FTPSite> objects) {
            super(context, resource, textViewResourceId, objects);
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.siterow, null);
        }
        FTPSite s = model.get(position);
        ((TextView) row.findViewById(R.id.title)).setText(s.getName());

        return (row);
    }

1 个答案:

答案 0 :(得分:1)

替换SiteAdapter adapter;

ArrayAdapter<FTPSite> adapter

然后在你的适配器类中,你需要创建一个这样的构造函数

public SiteAdapter(Context context, int resource, int textViewResourceId, List<T> objects)    {
    super(context,resource,textViewResourceId,objects);  
}

请参阅documentation

编辑:

当你声明需要做的时候

adapter = new SiteAdapter(this,r.layout.your_layout,new int[] {r.id.title},model);