我需要在一个选项卡片段中使用 listView,因此我实现了此示例中的代码:http: //geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-绑定到 an.aspx。该列表显示,当 MainActivity 中的某些操作完成时,但我不知道如何点击它。我试过这个例子中显示的,但它不起作用,我认为问题是因为我有片段,这个例子在 MainActivity 中。请帮忙!
public class FragmentLogbook extends ListFragment {
//handler for passing data through thread
private Handler handler;
//progress dialog
private ProgressDialog progressDialog;
private int progressBarStatus;
SingletonLogbookPack mSingletonLogbookPack = SingletonLogbookPack.getInstance();
ArrayList<SearchResults> searchResults = GetSearchResults();
MyCustomBaseAdapter mAdapter;
// Create an empty adapter we will use to display the loaded data.
//CustomArrayAdapter mAdapter = new CustomArrayAdapter(getActivity());
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
handler = new Handler();
startDownloadingLogbooks();
}
private void startDownloadingLogbooks() {
progressDialog = new ProgressDialog(getActivity());
//progressDialog = ProgressDialog.show(getActivity(),"Loading Logbooks", "Please wait...");
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading Logbooks ...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
progressDialog.setMax(100);
progressDialog.show();
//reset progress bar status
progressBarStatus = 0;
// start the time consuming task in a new thread
Thread thread = new Thread() {
public void run () {
boolean p = ((MainActivity)getActivity()).mGpsColibri.mBinary.getLogbook();
while ( ((MainActivity)getActivity()).logbookFinished != true ) {
progressBarStatus = (int) ((MainActivity)getActivity()).GetProgressValue();
progressDialog.setProgress(progressBarStatus);
//in this time Progress dialog will be on screen
}
//set logbookFinished variable from MainActivity to false
((MainActivity)getActivity()).logbookFinished = false;
// this will handle the post task.
// it will run when the time consuming task get finished
handler.post(new Runnable() {
@Override
public void run() {
// Update your UI or
// do any Post job after the time consuming task
// remember to dismiss the progress dialog here.
updateUI();
progressDialog.dismiss();
}
});
}
};
thread.start();
}
private void updateUI() {
mAdapter = new MyCustomBaseAdapter(getActivity(), searchResults);
setListAdapter(mAdapter);
ListView listview = FragmentLogbook.this.getListView();
// set onItemClick here
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterview, View view, int pos,
long id) {
// do your work here...
Toast.makeText(getActivity(), "" + pos, Toast.LENGTH_SHORT).show();
}
});
}
private ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr1 = new SearchResults();
sr1.setName("John Smith");
sr1.setCityState("Dallas, TX");
sr1.setPhone("214-555-1234");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Jane Doe");
sr1.setCityState("Atlanta, GA");
sr1.setPhone("469-555-2587");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Steve Young");
sr1.setCityState("Miami, FL");
sr1.setPhone("305-555-7895");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Fred Jones");
sr1.setCityState("Las Vegas, NV");
sr1.setPhone("612-555-1234");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Bob Marsh");
sr1.setCityState("New York, NY");
sr1.setPhone("612-555-5678");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Harold Funk");
sr1.setCityState("Chicago, IL");
sr1.setPhone("612-555-8765");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Scott Dorf");
sr1.setCityState("Winslow, AZ");
sr1.setPhone("612-555-5432");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Mike Hail");
sr1.setCityState("Seattle, WA");
sr1.setPhone("612-555-0961");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Scott Dorf");
sr1.setCityState("Winslow, AZ");
sr1.setPhone("612-555-5432");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Mike Hail");
sr1.setCityState("Seattle, WA");
sr1.setPhone("612-555-0961");
results.add(sr1);
return results;
}
}