开发者

Listview not showing results

开发者 https://www.devze.com 2023-02-27 05:49 出处:网络
I am struggling with showing results in a listview. So here is my application in short: First screen -> Different activities to perform and search is one of them. Once User chooses Search, next scre

I am struggling with showing results in a listview. So here is my application in short:

First screen -> Different activities to perform and search is one of them. Once User chooses Search, next screen has an input box and two buttons. Based on input keyword, I search for results and want to show results on the next screen, but my next screen appears ,but the results don't appear. Here is some code from my app

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchcriteria);

        txtSearch = (EditText)findViewById(R.id.searchstring);
        btnCurrentLoc = (Button)findViewById(R.id.currentlocation);

        btnCurrentLoc.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View view){
                setContentView(R.layout.searchresults);
                String keyword = txtSearch.getText().toString();
                performSearch(keyword);                         
            }
        });
    }

private void performSearch(String searchterm){
        progressDialog = ProgressDialog.show(Search.this, "Please wait......", "Retrieving data ....",true,true);
        PerformSearchTask task = new PerformSearchTask();
        task.execute(searchterm);
        progressDialog.setOnCancelListener(new CancelTaskOnCancelListener(task));       
    }

private class PerformSearchTask extends AsyncTask<String, Void, List<Event>>{       

        @Override
        protected List<Event> doInBackground(String... arg0) {
            String query = arg0[0];
            return eventGetter.find(query);
        } 

        protected void onPostExecute(final List<Event> result){
            runOnUiThread(new Runnable(){

                public void run() {
                    if(progressDialog != null){
                        progressDialog.dismiss();
                        progressDialog = null;
                    }
                    if(result != null){
                        Vector<String> ve = new Vector<String>();
                        for(Event event: result){
                            ve.add(event.title + "\n" + event.eventlocation + "\t" + event.eventdate);
                        }
                        String[] eventArray = new String[ve.size()];
                        for(int i = 0; i < ve.size(); i++){
                            eventArray[i] = ve.get(i);
                        }

                        ListView lv = (ListView)findViewById(R.id.list);
                        lv.setAdapter(new ArrayAdapter<String>(Search.this,R.layout.searchresults,eventArray));

                    }

                }

            });
        }
    }

Searchresults' screen layout is as below

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/resultsofsearch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<RelativeLayout
android:id="@+id/relativelayout_1"
android:layout_width="320px"
android:layout_height="332px"
xmlns:andro开发者_开发百科id="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
</Button>
<Button
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
>
</Button>

<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1.0"
android:layout_below="@+id/back"
android:layout_centerHorizontal="true"
>
</ListView>
</RelativeLayout>
</ScrollView>


I think u should implement each button click in separate Activity. it should solve ur problem Try this...Let me know if any difficulties occur

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号