尽管自定义列表适配器包含值,但列表片段中不显示任何项

时间:2014-12-26 23:22:57

标签: android android-fragments

下面的代码片段是我的列表片段的代码: -

public class XYZ extends ListFragment
{
    private ListViewItemAdapter mAdapter = null;

    private ArrayList<ListViewItem> values = new ArrayList<ListViewItem>();

    private ListView listView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

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

        listView = (ListView)view.findViewById(android.R.id.list);

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);

        outState.putParcelableArrayList("Values", values);  
     }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);

        Bundle bundle = this.getArguments();

        if(bundle != null)
        {
            Parcelable[] parceables = bundle.getParcelableArray("LIST_ITEMS");

            for(int i = 0, size = parceables == null ? 0 : parceables.length; i < size; i++)
            {
                values.add((ListViewItem)parceables[i]);
            }

            mAdapter = new ListViewItemAdapter(getActivity(), values);

            listView.setAdapter(mAdapter);

            mAdapter.notifyDataSetChanged();
        }
    }
}

我的自定义数组适配器的代码段: -

public class ListViewItemAdapter extends ArrayAdapter<ListViewItem>
{

    private Context context = null;

    private List<ListViewItem> listItems = null;


    public ListViewItemAdapter(Context context, List<ListViewItem> values) {

        super(context, 0, values);

        this.context = context;

        if(values != null)
        {
            listItems = values;
        }
    }

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

        if(convertView == null)
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.composite_control_for_list_view, parent, false);

        EditText textView = (EditText) convertView.findViewById(R.id.composite_control_text_box);

        Resources androidResources = XYZParameters.getApplicationContext().getResources();

        LayoutParams params = textView.getLayoutParams();

        params.height = (int)androidResources.getDimension(R.dimen.composite_control_textview_height_compressed);

        params.width = XYZParameters.getParentLinearLayoutWidth();//(int)androidResources.getDimension(R.dimen.composite_control_textview_compressed_width);

        textView.setLayoutParams(params);

        ImageButton importanceImageButton = (ImageButton) convertView.findViewById(R.id.composite_control_importance_button);

        LayoutParams importanceButtonParams = importanceImageButton.getLayoutParams();

        importanceImageButton.setVisibility(View.INVISIBLE);

        importanceButtonParams.height =  params.height;

        importanceButtonParams.width = (int)androidResources.getDimension(R.dimen.composite_control_importance_button_compressed_width);

        //(int)androidResources.getDimension(R.dimen.composite_control_image_button_height_compressed);

        importanceImageButton.setLayoutParams(importanceButtonParams);

        ImageButton alarmImageButton = (ImageButton)convertView.findViewById(R.id.composite_control_alarm_button);

        alarmImageButton.setVisibility(View.INVISIBLE);

        LayoutParams alarmButtonParams = alarmImageButton.getLayoutParams();

        alarmButtonParams.height = params.height;

        alarmButtonParams.width = (int)androidResources.getDimension(R.dimen.composite_control_alarm_button_compressed_width);

        //(int)androidResources.getDimension(R.dimen.composite_control_image_button_height_compressed);

        alarmImageButton.setLayoutParams(alarmButtonParams);

        if(listItems != null)
        {
            textView.setText(listItems.get(position).getTextViewData());
        }

        textView.setEnabled(false);

        LayoutParams layoutParamsForConvertView = convertView.getLayoutParams();

        layoutParamsForConvertView.height = params.height;

        layoutParamsForConvertView.width = (int)((params.width + importanceButtonParams.width + alarmButtonParams.width)+5);

        convertView.setLayoutParams(layoutParamsForConvertView);

        return convertView;
      }

}

以下是我创建此片段的活动的代码段: -

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {return;}

        Fragment fragment = new XYZ();

        Bundle bundle = new Bundle();

        Parcelable[] values = data.getParcelableArrayExtra("LIST_ITEMS");

        if(values != null)
        {
            bundle.putParcelableArray("LIST_ITEMS", values);

            fragment.setArguments(bundle);

            FrameLayout frameLayout = new FrameLayout(this);

            frameLayout.setId(ProfessionalPAParameters.getId());

            System.out.println("adding fragment ->");

            fragmentManager.beginTransaction().add(frameLayout.getId(), fragment).commit();

            System.out.println("fragment added <- return");

            addFrameLayoutToActivtyView(frameLayout);

            updateActivityView();
        }
    }

    private void addFrameLayoutToActivtyView(FrameLayout frameLayout) 
    {
        childFrames.add(0, frameLayout);
    }

    private void updateActivityView() 
    {
        for(int i = 0; i < numberOfLinearLayouts; i++)
        {
            System.out.println("updateActivityView -> i="+i);

            LinearLayout linearLayout = linearLayouts.get(i);

            linearLayout.removeAllViews();

            int index = 0;

            for(int j = i; j < childFrames.size(); j = j+numberOfLinearLayouts)
            {
                FrameLayout frameLayout = childFrames.get(j);

                frameLayout.setLayoutParams(frameLayoutParams);

                linearLayout.addView(frameLayout, index);

                index++;
            }
        }

    }

输出: - 当从活动创建XYZ列表片段时,线性布局仅包含空白字段列表,并且所有数据都将丢失。虽然通过应用日志,我发现在XYZ片段中,这些行中存在值: -

Parcelable[] parceables = bundle.getParcelableArray("LIST_ITEMS");

for(int i = 0, size = parceables == null ? 0 : parceables.length; i < size; i++)
{
    values.add((ListViewItem)parceables[i]);
}

mAdapter = new ListViewItemAdapter(getActivity(), values);

listView.setAdapter(mAdapter);

mAdapter.notifyDataSetChanged();  

在上面的行中,Parceable [] parceables包含正确的信息。

同样在ListViewItemAdapter中这行“textView.setText(listItems.get(position).getTextViewData());”在textView对象中设置正确的值。

问题: - framelayout中显示的列表片段不包含所需数据。显示的项目列表为空白。请查看屏幕截图以获取更多详细信息。

Screen shot showing blank list

XML文件composite_control_for_list_view.xml: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/composite_control_height"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_marginLeft="8dip"
    android:layout_marginTop="3dip"
    android:layout_marginBottom="3dip"
    android:id="@+id/composite_control_layout"
    tools:context="com.gp.app.professionalpa.layout.ListItemLayout" >

    <EditText
        android:id="@+id/composite_control_text_box"
        android:layout_width="fill_parent"
        android:layout_height= "@dimen/composite_control_height"
        android:hint="@string/add_note" />
    <ImageButton
        android:id="@+id/composite_control_importance_button"
        android:layout_width= "35dip"
        android:layout_height="@dimen/composite_control_height"
        android:layout_toRightOf="@id/composite_control_text_box"
        android:contentDescription="@string/set_importance"/> 

    <ImageButton
        android:id="@+id/composite_control_alarm_button"
        android:layout_width="35dip"
        android:layout_height="@dimen/composite_control_height"
        android:layout_toRightOf="@id/composite_control_importance_button"
        android:contentDescription="@string/set_alarm"/>   
</RelativeLayout>

0 个答案:

没有答案