我有一个asynctask,它从服务器访问数据并填充主 - 详细信息流。填写左侧的列表。我可以看到进度对话框正常工作。当我选择该项目时,我会调用另一个异步任务来获取该方面的更多信息。当我从左侧列表中选择项目时,进度对话框不会显示。它弹出一瞬间,然后显示数据。
如何创建一个进度对话框,显示第二个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 :(得分:0)
你不能从AsyncTask获取getActivity(),你应该在构造函数中传递它:
首先添加构造函数
public ConnectToServer(Activity activity) {
super();
this.activity = activity;
}
然后使用对话框中的活动
PD = ProgressDialog.show(activity,"title","massage", true);
PD.setCancelable(true);
PD.show();