Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何实施";“装载”;Android应用程序中的指示器_Java_Php_Android_Listview - Fatal编程技术网

Java 如何实施";“装载”;Android应用程序中的指示器

Java 如何实施";“装载”;Android应用程序中的指示器,java,php,android,listview,Java,Php,Android,Listview,好的,现在我有一个ListView,它通过PHP脚本填充信息。现在,列表一次加载一个。我的意思是,用户可以看到加载每个列表的时间(例如,他们看到一个项目,一秒钟后看到第二个项目,等等)。我要做的是等待所有项目被检索,然后立即显示它们。当这种情况发生时,要有某种类型的“加载”指示器(可能是旋转循环类型的交易)。我有什么办法可以实现这一点吗?这是我的密码: public class MainActivity extends ActionBarActivity { ArrayList<

好的,现在我有一个
ListView
,它通过
PHP脚本填充信息。
现在,列表一次加载一个。我的意思是,用户可以看到加载每个列表的时间(例如,他们看到一个项目,一秒钟后看到第二个项目,等等)。我要做的是等待所有项目被检索,然后立即显示它们。当这种情况发生时,要有某种类型的“加载”指示器(可能是旋转循环类型的交易)。我有什么办法可以实现这一点吗?这是我的密码:

public class MainActivity extends ActionBarActivity {

    ArrayList<Location> arrayOfLocations;
    LocationAdapter adapter;
    Button refresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        // Construct the data source
        arrayOfLocations = new ArrayList<Location>();

        // Create the adapter to convert the array to views
        adapter = new LocationAdapter(this, arrayOfLocations);

        getData();

        // Attach the adapter to a ListView
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(adapter);
    }
}
公共类MainActivity扩展了ActionBarActivity{
ArrayList ArrayFlocations;
位置适配器;
按钮刷新;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.activity_main);
//构建数据源
arrayOfLocations=新的ArrayList();
//创建适配器以将阵列转换为视图
适配器=新位置适配器(此为ArrayFlocations);
getData();
//将适配器连接到ListView
ListView ListView=(ListView)findViewById(R.id.listView1);
setAdapter(适配器);
}
}
因此,我的getData()方法将每个位置添加到适配器中,然后 然后,我的适配器类将数据放入ListView:

public class LocationAdapter extends ArrayAdapter<Location> {
    public LocationAdapter(Context context, ArrayList<Location> locations) {
        super(context, R.layout.item_location, locations);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Location location = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.item_location, parent, false);
        }

        // Lookup view for data population
        TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
        TextView tvDetails = (TextView) convertView
                .findViewById(R.id.tvDetails);
        TextView tvDistance = (TextView) convertView
                .findViewById(R.id.tvDistance);
        TextView tvHours = (TextView) convertView.findViewById(R.id.tvHours);
        ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imgIcon);

        // Populate the data into the template view using the data object
        tvName.setText(location.name);
        tvDetails.setText(location.details);
        tvDistance.setText(location.distance);
        tvHours.setText(location.hours);
        ivIcon.setImageBitmap(location.icon);
        // Return the completed view to render on screen
        return convertView;
    }
}
公共类LocationAdapter扩展了ArrayAdapter{
公共位置适配器(上下文、ArrayList位置){
超级(上下文、右布局、项目位置、位置);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取此职位的数据项
位置=获取项目(位置);
//检查是否正在重用现有视图,否则会膨胀视图
if(convertView==null){
convertView=LayoutFlater.from(getContext())。充气(
R.layout.item_位置,父项,false);
}
//数据填充的查找视图
TextView-tvName=(TextView)convertView.findViewById(R.id.tvName);
TextView tvDetails=(TextView)convertView
.findviewbyd(R.id.tvDetails);
TextView tVDestance=(TextView)convertView
.findViewById(R.id.tvDistance);
TextView tvHours=(TextView)convertView.findViewById(R.id.tvHours);
ImageView ivIcon=(ImageView)convertView.findViewById(R.id.imgIcon);
//使用数据对象将数据填充到模板视图中
tvName.setText(location.name);
tvDetails.setText(location.details);
tvdestance.setText(位置距离);
tvHours.setText(location.hours);
ivIcon.setImageBitmap(location.icon);
//返回要在屏幕上渲染的已完成视图
返回视图;
}
}
此外,我还有一个简单加载指示器的代码:

public class MainActivity extends Activity {

    private ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progress = new ProgressDialog(this);
    }

    public void open(View view) {
        progress.setMessage("Loading...Please Wait");
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setIndeterminate(true);
        progress.show();

        final int totalProgressTime = 100;

        final Thread t = new Thread() {

            @Override
            public void run() {

                int jumpTime = 0;
                while (jumpTime < totalProgressTime) {
                    try {
                        sleep(200);
                        jumpTime += 5;
                        progress.setProgress(jumpTime);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }
        };
        t.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
公共类MainActivity扩展活动{
私人进展对话进展;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
进度=新建进度对话框(此对话框);
}
公共作废打开(视图){
progress.setMessage(“正在加载…请稍候”);
progress.setProgressStyle(ProgressDialog.STYLE\u微调器);
progress.setUndeterminate(true);
progress.show();
最终整数totalProgressTime=100;
最终螺纹t=新螺纹(){
@凌驾
公开募捐{
int jumpTime=0;
while(跳跃时间<总进度时间){
试一试{
睡眠(200);
跳跃时间+=5;
progress.setProgress(jumpTime);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
};
t、 start();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
所以基本上,我想做的是找出如何做到这一点: 1.活动开始时,显示“加载”指示器 2.将所有项目加载到ListView中 3.列出所有项目后,移除“加载”指示器并显示ListView


有什么想法吗?谢谢。

要做到这一点,您需要一个扩展AsyncTask的类。在这个类中,在方法doInBackground中,您需要完成所有“繁重”的工作。在您的情况下,填充ListView。如果希望显示进度,可以在每次迭代结束时调用publishProgress方法。最后,在onPostExecute方法中,您可以通知用户流程已完成。这里有一个简单的例子

 public class ExampleAsync extends AsyncTask <Void, Integer, Void> {

     private ProgressDialog progressBar; //to show a little modal with a progress Bar
 private Context context;  //needed to create the progress bar

     public ExampleAsync(Context context){
           this.context = context;
     }

     //this is called BEFORE you start doing anything 
     @Override 
     protected void onPreExecute(){
         progressBar = new ProgressDialog(context);
         progressBar.setCancelable(false);
         progressBar.setMessage("I am starting to look for stuff");
         progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         progressBar.setIndeterminate(true);
         progressBar.show();
     }

     //every time you call publishProgress this method is executed, in this case receives an Integer
     @Override
 protected void onProgressUpdate(Integer ... option){
         progressBar.setMessage("I have found :" + option[0]);      
}  

     @Override
 protected void onPostExecute(Void unused){     
      progressBar.dismiss(); //hides the progress bar
          //do whatever you want now
}



     //in here is where you execute your php script or whatever "heavy" stuff you need
     @Override
 protected Void doInBackground(Void... unused) {          
        for (int i = 0; i < someLimit; i++){
             //do something
             publishProgress(i); //to show the progress
        }
     } 




 }
显然,这是一个简单的例子。你可以在onProgressUpdate方法中做很多事情,你需要用这个方法来更新进度条


希望它有帮助

实现这一点的另一个简单方法是在视图中显示ProgressBar,并在处理完成后将其隐藏:

<RelativeLayout
    android:id="@+id/app_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <FrameLayout
        android:id="@+id/loading_progress_container"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_centerInParent="true">
        <ProgressBar
            android:id="@+id/list_progress_indicator"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

    <com.example.MyListView
        android:id="@+id/my_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

</RelativeLayout>

显然,您需要对上述代码的容器视图和活动进行引用。

我将使用AsynchTask进行此操作。我使用AsyncTask加载每个ListView项。您打算如何使用AsyncTask检查列表是否已满?您需要一个进度对话框,以及AsyncTask中的回调。然后,在异步任务中,确保将上下文作为常量的参数传递
<RelativeLayout
    android:id="@+id/app_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <FrameLayout
        android:id="@+id/loading_progress_container"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_centerInParent="true">
        <ProgressBar
            android:id="@+id/list_progress_indicator"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

    <com.example.MyListView
        android:id="@+id/my_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

</RelativeLayout>
final View progressView = containerView.findViewById(R.id.loading_progress_container);
final View myListView = containerView.findViewById(R.id.my_list_view);
activity.runOnUiThread(new Runnable() {
    progressView.setVisibility(View.GONE);
    myListView.setVisibility(View.VISIBLE);
});