0

My main.xml has two parts. In the left is a listview and in the right is a webView. How can I display the String

String[] myList = new String[]{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

in the list view

<ListView 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:id="@+id/mylist"/>

I don't want to use ListActivity. My code is like

package com.android.WebViewChart;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WebViewChartActivity extends Activity {
    /** Called when the activity is first created. */
    String[] myList = new String[]{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView wv = (WebView) findViewById(R.id.WebViewChart);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl("file:///android_asset/1.html");
        ListView list = (ListView) findViewById(R.id.mylist);
        list.setAdapter(new ArrayAdapter<String>(this, R.id.mylist, myList));
    }
}

This deesn't work. Could someone tell me why? Thanks!

SPG
  • 6,109
  • 14
  • 48
  • 79

2 Answers2

0

use android.R.layout.simple_list_item_1 instead of R.id.mylist. This parameter is reserved for the id of layout that will be used to inflate each list item, not for id of a list view

list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList));

alternatively you could use your custom layout id, but it must cosist solely of TextView with id attribute set to android:id="@android:id/text"

wjeshak
  • 1,064
  • 9
  • 6
  • acctually, how about if I use ListActivity? It will be easiler? My goal is put that string inside the listviw which id is "mylist". I tried several times, but failed. – SPG Jul 04 '11 at 14:09
  • still doesn't work... Does it mean that the code doesn't compile or application throws an error or that wasn't what you intended to do? List view is used to present users many items, particularly simple text view filled with strings provided by the adapter, I'd you to clearly explain what you want to do in you question... – wjeshak Jul 04 '11 at 14:22
  • there is no mistake in Ecilpse, but when i run it. ----crashed!! – SPG Jul 04 '11 at 14:31
  • crash could be connected to anything in your application, if that's the issue, please attach logs from the device/emulator from ddms or adb logcat to your question, try to disable web view related logic until we find what's going on with list view – wjeshak Jul 04 '11 at 14:41
0

Go through the following link:

How can I implement a ListView without ListActivity? (use only Activity)

Community
  • 1
  • 1
Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60