【问题标题】:Adapter,progress dialog doesn't work in fragment适配器,进度对话框在片段中不起作用
【发布时间】:2012-06-14 15:53:18
【问题描述】:

我有扩展片段的类

public class  oUnit  extends Fragment {

我想填充列表并将其添加到线性布局,所以我使用以下代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                 ProgressDialog connectionProgressDialog = new 

ProgressDialog( getActivity());
          connectionProgressDialog.setCancelable(false);
          connectionProgressDialog.setCanceledOnTouchOutside(false);
          connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          connectionProgressDialog.setMessage("Uploading Leads...");
          connectionProgressDialog.show();

         View view = inflater.inflate(
                    R.layout.chemounit,
                    container,
                    false);


        //Intialize the record Grid
         LinearLayout formLayout = (LinearLayout)view.findViewById(R.id.UnitGrid);
        formLayout.removeAllViews();

        MainGrid = new ListView(getActivity().getApplicationContext());              
        MainGrid.setVisibility(ListView.VISIBLE);
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
         params.gravity = Gravity.RIGHT;
         MainGrid.setLayoutParams(params);


         //2. Call the web Service 



         GlobalVariables appState = (GlobalVariables) getActivity().getApplication();

         Log.d(" in Chemo unit"," the array is has "+  appState.encounters.size());

         MainGrid.setAdapter(new Encounteradapter(view.getContext(),R.id.ChemoUnitGrid ,  appState.encounters));
        // Finally add it 

         formLayout.addView(MainGrid);


       connectionProgressDialog.dismiss();

     return view;
    }

我确定全局变量列表不为空并且包含数据(我用log.d显示它的长度),Adapter类就像

public class Encounteradapter   extends ArrayAdapter<Encounter> {

    private final Context context;

    TextView textViewTime ;
    TextView textViewPatientName;
    Button ButottonArrive ;
    Button ButottonEncounter;
    Button ButottonExit; 
    ArrayList<Encounter> Encounters ; 

    public Encounteradapter(Context context, int  ResourceId,
            ArrayList<Encounter> items)
    {


        super(context, ResourceId, items);
        this.context = context;
         this.Encounters = items ; 
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.chemounitgrid, parent, false);




        Encounter EncounterObject = Encounters.get(position);

         textViewTime = (TextView) rowView.findViewById(R.id.Time);
        textViewTime.setText(EncounterObject.bookingDate);

        textViewPatientName = (TextView) rowView.findViewById(R.id.Time);
        textViewPatientName.setText(EncounterObject.Name);

        ButottonArrive = (Button) rowView.findViewById(R.id.test1);
        ButottonEncounter = (Button) rowView.findViewById(R.id.test2);
        ButottonExit = (Button) rowView.findViewById(R.id.test3);

        rowView.setTag(position);




        return rowView;
    }
}

但没有出现列表没有出现,我也尝试删除我设置的属性部分,但也没有

这是我尝试在片段 OnCreate() 上显示进度的第二个问题,但它没有出现我使用以下代码

  connectionProgressDialog = new ProgressDialog(getActivity());
  connectionProgressDialog.setCancelable(false);
  connectionProgressDialog.setCanceledOnTouchOutside(false);
  connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  connectionProgressDialog.setMessage("Uploading Leads...");
  connectionProgressDialog.show(); 

知道如何解决这个问题,我是否错过了片段中的任何内容

【问题讨论】:

  • ProgressDialog 应该可以工作。你在使用ActionBarSherlock 库吗?
  • 我不知道是什么导致ProgressDialog 没有出现。我已经测试了您的 ProgressDialog 代码,它可以正常工作。
  • 是的,我在FragmentonCreate 方法和FragmentonCreateView 方法中都试过了。在这两种情况下,ProgressDialog 都会出现。

标签: android android-listview android-fragments android-adapter android-progressbar


【解决方案1】:

在 onCreateView 中试试这个

View view=inflater.inflate(R.layout.chemounit, container, false);
  ProgressDialog connectionProgressDialog = new ProgressDialog(getActivity());
  connectionProgressDialog.setCancelable(false);
  connectionProgressDialog.setCanceledOnTouchOutside(false);
  connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  connectionProgressDialog.setMessage("Uploading Leads...");
 connectionProgressDialog.show();
 return view;

【讨论】:

    【解决方案2】:

    屏幕上没有任何内容是正常的,因为在onCreateView 方法中,您会膨胀布局并设置ListView,但是当需要返回视图时(该片段的布局带有@ 987654323@你把数据放在哪里)你做的:

    return inflater.inflate(R.layout.chemounit, container, false);
    

    这将使用空的ListView 再次膨胀布局文件(因此您之前所做的所有设置都无用,因为您丢弃了第一个膨胀的View)。相反,您应该返回:

    return view;
    

    【讨论】:

    • 进度对话框怎么样,还有更多关于你的解释的细节
    • @AMH 您声明的ProgressDialog 应该可以正常工作。也许您的代码特别有问题,因此您应该为尝试使用它的片段添加完整代码。关于丢失的ListView,我不知道要给你什么额外的解释。只需返回第一个膨胀的 View 而不是您当前返回的内容,因为这将提供另一个空视图,而您不希望这样。
    • 返回视图,工作会并显示列表,但是没有出现进度,看我更新的代码
    • @Luksprog ,当我在做 ProgressDialog connectionProgressDialog = new ProgressDialog(getActivity()) 时,在片段中它显示在整个 Activity 中,而不是在特定片段中。
    • @TusharPandey 这是正常的对话行为。如果您只想显示该片段的进度,请使用内联 ProgressBar 小部件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多