在Custom ArrayAdapter上更新ListView

时间:2015-03-09 16:12:02

标签: java android listview android-listview android-arrayadapter

我很抱歉,如果这与其他线程类似,但我还没有找到一个很好的解决方案,如何在我的自定义ArrayAdapter中更新ListView。

我正在扩展FragmentActivity,以便创建一个自定义Dialog弹出窗口,用于添加/更新新的List条目(工作正常),但我似乎无法在更改后更新ServerActivityArrayAdapter中的ListView。 / p>

显然,adapter.notifyDataSetChanged();虽然在ServersActivity类中是不够的,但在ServerDialog弹出窗口中单击“保存”按钮后,我对如何使ListView更新感到困惑。任何帮助将不胜感激!

public class ServersActivity extends FragmentActivity {

private List<Server> server;
private ListView lv;
private ServerActivityArrayAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.server_list);

    server = getServers();

    lv = (ListView) findViewById(android.R.id.list);
    adapter = new ServerActivityArrayAdapter(this, R.layout.server_list_item, server);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            showServerDialog(position);
        }
    });
}

...

private void showServerDialog(int position) {
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    ServerDialog serverDialog = new ServerDialog(position);
    ServerDialog.newInstance();
    serverDialog.show(fragmentManager, "server_dialog");
}

用于添加新列表条目的ServerDialog类

public class ServerDialog extends DialogFragment {

...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.settings_fragment, container);

    context = getActivity().getApplicationContext();
    dbh = new DbHandler(context);

    etName = (EditText) view.findViewById(R.id.etName);
    etAddress = (EditText) view.findViewById(R.id.etAddress);
    etPort = (EditText) view.findViewById(R.id.etPort);
    swtchNumDoors = (Switch) view.findViewById(R.id.swtchNumDoors);
    Button btnSave = (Button) view.findViewById(R.id.btnSave);

    loadData();
    String title = (!newServer) ? "Edit Server" : "Add A New Server " + ID;
    getDialog().setTitle(title);

    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            server[ID] = String.valueOf(ID);
            server[SERVER_NAME] = etName.getText().toString();
            server[ADDRESS] = etAddress.getText().toString();
            server[PORT] = etPort.getText().toString();

            if (server[ADDRESS].isEmpty() || server[PORT].isEmpty()) {
                Toast.makeText(context, "Invalid Entry", Toast.LENGTH_LONG).show();
            } else {
                saveData();
                // TODO - notify ServersActivity of change
            }
        }
    });

    return view;
}

public class ServerActivityArrayAdapter extends ArrayAdapter<Server> {

private int layout;
private String[][] values;
private int defServer;

private static LayoutInflater inflater;

ServerActivityArrayAdapter类,用于显示ListView

public ServerActivityArrayAdapter(Context context, int layout, List<Server> servers) {
    super(context, layout, servers);

    this.layout = layout;

    int i = 0;
    values = new String[servers.size()][5];

    for (Server server : servers) {
        values[i][0] = Integer.toString(server.id);
        values[i][1] = server.name;
        values[i][2] = server.address;
        values[i][3] = Integer.toString(server.port);
        values[i][4] = Integer.toString(server.num_doors);
        defServer = (server.default_server) ? i : 0;
        i++;
    }

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

private static class ViewHolder {
    private TextView tvServerName;
    private CheckBox cbDefault;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    View v = convertView;

    try {
        if(v == null) {
            v = inflater.inflate(layout, parent, false);
            holder = new ViewHolder();
            holder.tvServerName = (TextView) v.findViewById(R.id.tvServerName);
            holder.cbDefault = (CheckBox) v.findViewById(R.id.cbDefault);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.tvServerName.setText(values[position][1]);
        holder.cbDefault.setChecked(defServer==position);

    } catch (Exception e){
        System.out.println(e);
    }

    return v;
}

2 个答案:

答案 0 :(得分:0)

您可以使用一个接口并在其中包含一个名为updateListView()的方法。

在FragmentActivity中实现此接口。在实现的方法中,添加代码以更新listView。

在对话框的按钮代码中,调用((Interfacename)getActivity).updateListView()

答案 1 :(得分:0)

您的问题在于适配器的实现。您正在使用ArrayAdapter对象(超级调用)实例化List<Server>,然后创建自己的二维数组values以包含数据。您对values方法使用getView()数组,但没有提及或通过代码显示您如何实际修改该数据以更新适配器。

您应该回调适配器进行更新。 EG,调用add()remove()等。您只需提及更新与更新适配器无关的数据库。它无论如何都与DB无关。即使您在调用适配器上调用前面提到的mutate方法,除非您专门override它们,否则它们将无法工作。默认ArrayAdapter实施将修改List<Server>数据,而不是您创建的values双暗阵列。

但是如果你发现自己override那些支持你的values二维数组的变异方法......那么继承ArrayAdapter就没有意义了。 ArrayAdapter的部分要点是为您处理所有数据管理。在这种情况下,您应该改为继承BaseAdapter