列表视图不显示内容

时间:2015-12-11 07:40:40

标签: java android listview

我有一个简单的任务,就是从Dialog获取字符串输入,然后将其添加到带有复选框的listview。

发生了什么,我通过对话框进入测试但是在添加后它没有显示,并且代码在运行时没有任何错误。

MainActivity.java

public class MainActivity extends AppCompatActivity {

LinearLayout linearLayout;
ListView list;
MyCustomAdapter dataAdapter ;
List<SubTasksModel> subTasksModelArrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.heading);

    linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showInputDialog();
        }
    });

    subTasksModelArrayList = new ArrayList<SubTasksModel>();
    list = (ListView) findViewById(R.id.list);
    dataAdapter = new MyCustomAdapter(this , R.layout.child_row_add_subtask , subTasksModelArrayList);
    list.setAdapter(dataAdapter);

}

protected void showInputDialog() {

    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.add_subtask_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
    alertDialogBuilder.setView(promptView);

    final EditText editText = (EditText) promptView.findViewById(R.id.et_task);

    // setup a dialog window
    alertDialogBuilder.setCancelable(false)

            .setPositiveButton("Add Subtask", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    /*subTasksModelArrayList = new ArrayList<SubTasksModel>();*/
                    fillData(editText.getText().toString() , false);
                    Utility.setListViewHeightBasedOnChildren(list);
                }
            })
            .setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

private void fillData(String s, boolean b) {
    final SubTasksModel model = new SubTasksModel();
    model.setTask(s);
    model.setSelected(b);
    subTasksModelArrayList.add(model);
}
}

MyCustomAdapter.java

public class MyCustomAdapter extends BaseAdapter {

public List<SubTasksModel> subTasksModelList;
Context context;
private LayoutInflater mInflater;

public MyCustomAdapter(Context context, int resource, List<SubTasksModel> objects) {
    this.context=context;
    this.subTasksModelList=objects;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

static class ViewHolder {
    LinearLayout linearLayout;
    TextView task;
    CheckBox cb;
}


@Override
public int getCount() {
    return 0;
}

@Override
public SubTasksModel getItem(int position) {
    return subTasksModelList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    ViewHolder holder ;
    Log.v("ConvertView", String.valueOf(position));

    if (convertView == null) {
        holder = new ViewHolder();

        convertView = mInflater.inflate(R.layout.child_row_add_subtask, null);

        holder.task = (TextView) convertView.findViewById(R.id.tv_subtask);
        holder.cb = (CheckBox) convertView.findViewById(R.id.cb_subtask);
        holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.LinearLayout01);
        convertView.setTag(holder);

    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    final int pos = position;

    holder.task.setText(subTasksModelList.get(position).getTask());
    holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            subTasksModelList.get(pos).setSelected(isChecked);
        }
    });

        /*Log.d("ArrayList"," : "+ subTasksModelList);
    SubTasksModel subTasksModel = subTasksModelList.get(position);
    holder.task.setText(subTasksModel.getTask());
    holder.cb.setChecked(subTasksModel.isSelected());
    holder.cb.setTag(subTasksModel);*/
    return convertView;
}
}

SubTaskModel.java

public class SubTasksModel {

String task = "";
boolean selected = false;


public String getTask() {
    return task;
}
public void setTask(String task) {
    this.task = task;
}

public boolean isSelected() {
    return selected;
}
public void setSelected(boolean selected) {
    this.selected = selected;
}
}

//实用程序类只是在scrollview中设置listview的高度。

public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    Log.d("Params are : " , " : "+ params);
    listView.requestLayout();
}
}

1 个答案:

答案 0 :(得分:1)

@Override
public int getCount() {
    return 0;
}

替换为

@Override
public int getCount() {
    return subTasksModelList.size();
}