我需要在一个标签片段中使用listView,因此我实现了此示例中的代码:http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-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;
}
}
答案 0 :(得分:1)
设置listview适配器后尝试此操作
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:1)
您可以在onItemClick
内添加updateUI()
ListFragment行的监听器:
private void updateUI() {
mAdapter = new MyCustomBaseAdapter(getActivity(), searchResults);
setListAdapter(mAdapter);
ListView listview = FragmentLogbook.this.getListView(); //<< initialize ListView
// set onItemClick here
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterview, View view, int pos,
long id) {
// do your work here...
}
});
}
答案 2 :(得分:1)
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
/// do your code here
}
答案 3 :(得分:1)
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
String requiredvalue= ((TextView) arg1.findViewById(R.id.viewid)).getText().toString();
Log.i("requiredvalue", requiredvalue);
}
OR
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
searchResults.get(position);
Log.i("requiredvalue", ""+searchResults.get(position));
}