简体   繁体   中英

Embedded ListView in Android?

I want to use a listView in my program. However, I also want to use some different components(Button, TextEdit etc.) in the same window. How can i do it?

Instead of extending ListActivity, just use a regular activity and instantiate your list view like this:

listView.setAdapter( new CustomAdapter( this, R.layout.list_item, responses ) );
    listView.setTextFilterEnabled( true );
    listView.setOnItemClickListener( new OnItemClickListener()
    {
        public void onItemClick( AdapterView<?> parent, View view,
            int position, long id ) {
            try
            {
                Intent i = new Intent().setClass( getParent(), GuideActivity.class );
                i.putExtra( "action", "nextstep" );
                i.putExtra( "session", currentStep.getSession() );
                i.putExtra( "step", currentStep.getStep() );
                i.putExtra( "response", currentStep.getResponse( position ).getId() );
                TabGroupActivity parentActivity = ( TabGroupActivity ) getParent();
                parentActivity.startChildActivity( currentStep.getStep(), i );
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    });

You will of course have to do some customization since this is taken directly from my own application.

I made my own adapter for this application. You should probably use an ArrayAdapter instead.

You have to use Base Adapter for that. It is simple. There are four methods Inside this 1.getCount 2.getItem 3.getItemId 4.getView

class CustomListAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return count;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arr[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = view;

        if(rowView == null)
        {
            LayoutInflater layoutInflater = LayoutInflater.from(QuickSearchingWithAndroid.this);
            rowView = layoutInflater.inflate(R.layout.custom_list_view,parent,false);
        }

        WebView webView1 = (WebView)rowView.findViewById(R.id.webview1);

        webView1.loadUrl("http://www.google.co.in");


        return rowView;
    }

}

Then we have to create the object of the class and set That Object as: CustomListAdapter customListAdapter = new CustomListAdapter(); listView.setAdapter(customListAdapter);

Above two lines you have to do inside the onStart or onCreate method. I hope this will solve your problem...just check how the layout file has to create because their is two layout file has to create.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM