ListView,setOnItemClickListener

时间:2014-01-10 20:58:58

标签: android-listview

提前感谢任何观看并帮助解决我的问题的人。我有一个简单的应用程序,在webview中显示html页面。我从strings.xml中的字符串数组生成一个列表。每个项目按照它们在数组中列出的顺序显示在ListView中,没有任何问题。 setOnItemClickListener和loadUrl工作正常,直到你滚动并选择。一旦滚动然后选择,加载了错误的html文件(一个不同于所选的)有时它不会加载任何东西并且将崩溃或恢复到主加载活动。我已经在众多尺寸和样式模拟器以及nexus 7平板电脑上进行了测试。

这是显示列表并将选定的html文件加载到WebView中的类:

    public class MKI extends Activity 
    {
   ListView listView;
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_style);
    listView = (ListView) findViewById(R.id.list);
    String[] list = getResources().getStringArray(R.array.maintkits_array);
        listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list));  

    listView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
           public void onItemClick(AdapterView<?> parent, View view, int p,long id) 
        {
              ListView lv = (ListView) parent;
              TextView tv = (TextView) lv.getChildAt(p);
              String s = tv.getText().toString();
              setContentView(R.layout.html_view); 
              WebView mbrowser = (WebView) findViewById(R.id.viewHTML); 
              mbrowser.loadUrl("file:///android_asset/ki/"+ s +".html");

        }
    });
}
  }

1 个答案:

答案 0 :(得分:0)

将此分为两个活动可能会更好。一个处理URL选择显示,另一个处理显示网页。

类似的东西:

public class MKI extends Activity {

   private ListView listView;
   private ArrayAdapter<String> adapter;

   @Override
   protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_style);

    String[] list = getResources().getStringArray(R.array.maintkits_array);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list); 

    listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);  
    listView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
           public void onItemClick(AdapterView<?> parent, View view, int p,long id) 
        {
              String url = adapter.getItem(p);
              Intent viewPage = new Intent(MKI.this, WebActivity.class);
              viewPage.putExtra(EXTRA_URL, url);
              startActivity(viewPage);
        }
    });
}
  }

public class WebActivity extends Activity {

    public static final String EXTRA_URL = "EXTRA_URL";

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         

        if(!getIntent().hasExtra(EXTRA_URL)){
            throw new IllegalArgumentException("Must provide url to" this.getClass().getSimpleName());
        } else {
                     setContentView(R.layout.store_activity);
             webview = (WebView)findViewById(R.id.web_view);

                     String url = getIntent().getStringExtra(EXTRA_URL);
                     mbrowser.loadUrl("file:///android_asset/ki/"+ url +".html");
                }



         .....


        }