2015-12-11 185 views
-1

我有一個簡單的任務,即從對話框中獲取字符串輸入,然後將其添加到帶有複選框的列表視圖。列表視圖不顯示內容

發生什麼事情是,我通過對話框輸入了測試,但添加後沒有顯示,並且代碼在運行時沒有任何錯誤。

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(); 
} 
} 
+0

更換getCount將= 0就不會顯示任何 – NaserShaikh

+0

您需要修改這麼多的事情,至少研究有關如何顯示'ListView'。 –

+0

是@NigamPatro我知道這就是爲什麼我在這裏發佈我的查詢,你可以請幫忙:/ – Ujjawal

回答

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

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