开发者

Embedded ListView in Android?

开发者 https://www.devze.com 2023-04-12 17:21 出处:网络
I want to use a listView in my program. However, I also wan开发者_如何学编程t to use some different components(Button, TextEdit etc.) in the same window. How can i do it?You have to use Base Adapter f

I want to use a listView in my program. However, I also wan开发者_如何学编程t to use some different components(Button, TextEdit etc.) in the same window. How can i do it?


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.


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.

0

精彩评论

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

关注公众号