2014-07-22 91 views
0

我有一个asynctask,它从服务器访问数据并填充主 - 细节流。填充左侧的列表。我可以看到进度对话框正常工作。当我选择该项目时,然后我会调用另一个异步任务以获取该方面的更多信息。当我从左侧列表中选择项目时,进度对话框不显示。它像一瞬间弹出,然后显示数据。进度对话框master-detail不显示android

如何创建一个进度对话框,显示第二个asynctask获取右手片段数据的持续时间?

左侧列表中的项目:

public class ItemListFragment extends ListFragment 
{ 

private ObjectOutputStream output; 
private ObjectInputStream input; 
private String message = ""; 
private Socket client; 
private XMLCreator x = new XMLCreator(); 


private static final String STATE_ACTIVATED_POSITION = "activated_position"; 

private Callbacks mCallbacks = sDummyCallbacks; 

private int mActivatedPosition = ListView.INVALID_POSITION; 

public interface Callbacks 
{ 

    public void onItemSelected(int id, String value); 
} 


private static Callbacks sDummyCallbacks = new Callbacks() 
{ 
    @Override 
    public void onItemSelected(int id, String value) 
    { 
    } 
}; 

public class ConnectToServer extends AsyncTask<Integer, String, ArrayList<String>> 
{ 

    ProgressDialog PD; 

    @Override 
    protected void onPreExecute() 
    { 
     //Works fine and shows up perfectly 
     super.onPreExecute(); 
     PD = new ProgressDialog(ItemListFragment.this.getActivity()); 
     PD.setTitle("Please Wait.."); 
     PD.setMessage("Loading..."); 
     PD.setCancelable(true); 
     PD.setIndeterminate(true); 
     PD.show(); 
     } 

    @Override 
    protected ArrayList<String> doInBackground(Integer... params) 
    { 
     Socket client; 
     DataInputStream input; 
     DataOutputStream output; 
     String serverResult = "nothing"; 
     XMLCreator x = new XMLCreator(); 
     ArrayList<String> lists = new ArrayList<String>();; 

     try 
     { 
      Log.i("CLIENT", "Client started"); 

      // Step 1: Create a Socket to make connection. 
      // client = new Socket(InetAddress.getLocalHost(), 500); 
      client = new Socket("10.0.0.5", 5001); 
      Log.i("CLIENT", "Connected to: " 
        + client.getInetAddress().getHostName()); 
      publishProgress("Connecting to server..."); 

      // Step 2: Get the input and output streams. 
      input = new DataInputStream(client.getInputStream()); 
      output = new DataOutputStream(client.getOutputStream()); 
      output.flush(); 
      Log.i("CLIENT", "Got I/O Streams"); 
      publishProgress("Obtained Streams..."); 

       // Step 3: Process connection. 

      // Step 4: Close connection. 
      Log.i("CLIENT", "Transmission complete. " 
        + "Closing connection."); 
      output.writeUTF("TERMINATE"); 
      client.close(); 
      publishProgress("Closed connection..."); 

     } catch (IOException e) { 
      e.printStackTrace(); 
      String s = e.getMessage(); 
      String b = s; 
     } 
     return lists; 

    } 

    @Override 
    protected void onPostExecute(ArrayList<String> lists) 
    { 
     super.onPostExecute(lists); 
     PD.dismiss(); 
     List<DummyItem> ITEMS = new ArrayList<DummyItem>(); 
     for (String s : lists) 
     { 
      int count = 1; 
      ITEMS.add(new DummyItem("",s)); 
     } 
     setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(), 
       android.R.layout.simple_list_item_activated_1, android.R.id.text1, ITEMS)); 

    } 

    @Override 
    protected void onProgressUpdate(String... values) 
    { 
     PD.setMessage(values[0]); 
     super.onProgressUpdate(values); 
    } 
} 


public ItemListFragment() 
{ 
} 

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

    ConnectToServer task = new ConnectToServer(); 
    task.execute(); 

} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) 
{ 
    super.onViewCreated(view, savedInstanceState); 

    // Restore the previously serialized activated item position. 
    if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) { 
     setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION)); 
    } 
} 

@Override 
public void onAttach(Activity activity) 
{ 
    super.onAttach(activity); 

    // Activities containing this fragment must implement its callbacks. 
    if (!(activity instanceof Callbacks)) 
    { 
     throw new IllegalStateException("Activity must implement fragment's callbacks."); 
    } 

    mCallbacks = (Callbacks) activity; 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 

    // Reset the active callbacks interface to the dummy implementation. 
    mCallbacks = sDummyCallbacks; 
} 

@Override 
public void onListItemClick(ListView listView, View view, int position, long id) 
{ 
    super.onListItemClick(listView, view, position, id); 

    Object item = listView.getItemAtPosition(position); 

    String myitem = item.toString(); 

    mCallbacks.onItemSelected(position, myitem); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    if (mActivatedPosition != ListView.INVALID_POSITION) 
    { 

     outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); 
    } 
} 

public void setActivateOnItemClick(boolean activateOnItemClick) 
{ 

    getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE); 
} 

private void setActivatedPosition(int position) 
{ 
    if (position == ListView.INVALID_POSITION) 
    { 
     getListView().setItemChecked(mActivatedPosition, false); 
    } else 
    { 
     getListView().setItemChecked(position, true); 
    } 

    mActivatedPosition = position; 
    } 
} 

右手细节片段:

public class CountingFragment extends Fragment 
{ 
int mNum; 
String value; 
List<Session> lists; 
String[] simpleArray; 

public class ConnectToServer extends AsyncTask<String, String, List<Session>> 
{ 

    ProgressDialog PD; 

    @Override 
    protected void onPreExecute() 
    { 
     //Doesn't show up at all ---> Want to show the dialog here 
     PD = new ProgressDialog(getActivity()); 
     PD.setTitle("Please Wait.."); 
     PD.setMessage("Loading..."); 
     PD.setCancelable(true); 
     PD.setIndeterminate(true); 
     PD.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected List<Session> doInBackground(String... params) 
    { 
     Socket client; 
     DataInputStream input; 
     DataOutputStream output; 
     String results = ""; 
     XMLCreator x = new XMLCreator(); 
     List<Session> items = new ArrayList<Session>(); 
     try 
     { 
      Log.i("CLIENT", "Client started"); 

      // Step 1: Create a Socket to make connection. 
      // client = new Socket(InetAddress.getLocalHost(), 500); 
      client = new Socket("10.0.0.5", 5001); 
      Log.i("CLIENT", "Connected to: " 
        + client.getInetAddress().getHostName()); 
      publishProgress("Connected..."); 

      // Step 2: Get the input and output streams. 
      input = new DataInputStream(client.getInputStream()); 
      output = new DataOutputStream(client.getOutputStream()); 
      Log.i("CLIENT", "Got I/O Streams"); 
      publishProgress("Obtained Streams..."); 

       // Step 3: Process connection. 
      //Read in the value that the user selected from the bundle 
      String Day = params[0].toString(); 

      // Step 4: Close connection. 
      Log.i("CLIENT", "Transmission complete. " 
        + "Closing connection."); 
      output.writeUTF("TERMINATE"); 
      client.close(); 
      publishProgress("Closed connection..."); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return items; 

    } 

    @Override 
    protected void onPostExecute(List<Session> lists) 
    { 
     super.onPostExecute(lists); 
     PD.dismiss(); 
    } 

    @Override 
    protected void onProgressUpdate(String... values) 
    { 
     PD.setMessage(values[0]); 
     super.onProgressUpdate(values); 

    } 
} 

/** 
* Create a new instance of CountingFragment, providing "num" as an argument. 
*/ 
static CountingFragment newInstance(int num, String value) 
{ 
    CountingFragment f = new CountingFragment(); 

    // Supply num input as an argument. 
    Bundle args = new Bundle(); 
    args.putInt("num", num); 
    args.putString("value", value); 
    f.setArguments(args); 
    return f; 
} 

/** 
* When creating, retrieve this instance's number from its arguments. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 


    mNum = getArguments() != null ? getArguments().getInt("num") : 1; 
    value = getArguments() != null ? getArguments().getString("value") : ""; 

    ConnectToServer task = new ConnectToServer(); 
    try 
    { 
     lists = task.execute(value).get(); 


    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

/** 
* The Fragment's UI is just a simple text view showing its instance number. 
*/ 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.main_world, container, false); 
    ListView lv = (ListView) view.findViewById(R.id.list_sessions); 
    final SessionAdapter adapter = new SessionAdapter(getActivity(), lists); 
    lv.setAdapter(adapter); 
    //ArrayAdapter<String> adapter = new ArrayAdapter<String>( 
      // inflater.getContext(), android.R.layout.simple_list_item_1, 
      // lists);    
    return view; 
    } 
} 

有什么特别的原因,为什么我的进度对话框不显示在选择的了列表项???我的代码在哪里出错?

+0

典型

public ConnectToServer(Activity activity) { super(); this.activity = activity; } 

然后使用活动原因是你没有在UIThread上这么做,因此它不会发生,或者代码在doInBackground()内执行得太快而没有显示可视助手。 –

回答

0

你不能getActivity()从的AsyncTask,你应该通过它在构造函数中:

首先添加的构造函数在对话框

PD = ProgressDialog.show(activity,"title","massage", true); 
PD.setCancelable(true); 
PD.show(); 
+0

然后在我的实例中,我会打电话吗? ConnectToServer任务=新的ConnectToServer(getActivity()); – user1397978

+0

是的,exaclty。祝你好运 – itzhar