开发者

getting source not found on setContentView

I am getting an source not found on setContentView in this line:

  setContentView(R.layout.webview);

On onItemClick action. I have 2 xml files. One is an list items. its look like that:

list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="15sp" >
</TextView>

I am trying to do when the user click on the item on the list it will just load it. my xml of it is: webview.xml

<?xml version="1.0" encoding="utf-8"?>
<Web开发者_如何学JAVAView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>   

here the full code of it.its all works great till setContentView:

public class NewsActivity  extends ListActivity {
WebView mWebView;
 public ReadXML ReadXML=new ReadXML();
 public  ArrayList<String> ynetList =new ArrayList<String>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

for(int i=0;i<ReadXML.hadashotListItems.size();i++)
   ynetList.add(ReadXML.hadashotListItems.get(i).title+"\n"+ReadXML.hadashotListItems.get(i).pubDate);

      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, ynetList));

      View v=getListView() ;

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
          // When clicked, show a toast with the TextView text
        String s=   ReadXML.hadashotListItems.get(position).link;

        setContentView(R.layout.webview);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(s);



        }
      }
      );
    }}


A ListActivity needs a ListView with the ID android.R.id.list. As you create your activity, everything is fine because you're not specifying a content view of your own, and so a default content view containing just a list is being used.

Now, in your setContentView call, what your code is really saying is "Now I want the entire screen to be a big webview." In doing this, you're violating the ListActivity requirement of always having a ListView.

You could create a layout file that contains a WebView and a ListView with the ID android.R.id.list, and use onclick to toggle their visibility, and perhaps listen to the hardware back button to toggle back. But I think it would be a neater approach to simply have the click listener launch a new activity, containing only the WebView:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

       // When clicked, show a toast with the TextView text
       String s = ReadXML.hadashotListItems.get(position).link;

       Intent intent = new Intent(NewsActivity.this, WebActivity.class);
       intent.putExtra("url", s);

       startActivity(intent);

    }
}

You could then let your new WebActivity class set R.layout.webview as its content view. Pressing the hardware back button will automatically finish that activity, and bring you back to the list, where you were.


don't you want to create a new activity that loads your url more than changing the whole layout of your current activity ? It seems ackward.

Regards, Stéphane

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜