使用包含listviews的标签的问题(Android)

时间:2011-01-19 20:30:41

标签: android listview layout android-tabhost tabview

我尝试使用两个标签创建一个tabhost。每个选项卡包含一个列表视图。 这是java文件的代码

package com.Bussruter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TabHost.OnTabChangeListener;

public class Bussruter extends TabActivity implements OnTabChangeListener {
    /** Alle */
    private static final String LIST1_TAB_TAG = "List1";
    /** Nedlastet */
    private static final String LIST2_TAB_TAG = "List2";

    private StorageHandler sh;
    private Resources res;// Resource object to get Drawables
    private ListView lvNedlastet;
    private ListView lvAlle;
    private TabHost th;

    /*private int[] created = new int[]{
            0,
            20100108,
            0
    };*/
    /** Called when the activity is first created. */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);


      //Checking which pdfs are already downloaded.
      sh = new StorageHandler();
      this.checkAvaliablePdfs();
      th = getTabHost();
      res = getResources();
      th.setOnTabChangedListener(this);
      lvAlle = (ListView) findViewById(R.id.list1);
      lvNedlastet = (ListView) findViewById(R.id.list2);

      lvAlle.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, new String[]{"1"}));//sh.getListNameAlle()));
      lvNedlastet.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_2, new String[]{"1"}));//sh.getListNameNedlastet()));


      OnItemClickListener itemCLAlle = this.createClickListenerAlle();
      OnItemClickListener itemCLNedlastet = this.createClickListenerNedlastet();

      lvAlle.setOnItemClickListener(itemCLAlle);
      lvNedlastet.setOnItemClickListener(itemCLNedlastet);

      OnItemLongClickListener itemLCL = this.createLongClickListener();
      lvAlle.setOnItemLongClickListener(itemLCL);
      lvAlle.setLongClickable(true);
      lvNedlastet.setOnItemLongClickListener(itemLCL);
      lvNedlastet.setLongClickable(true);

      th.addTab(th.newTabSpec(LIST2_TAB_TAG)
                .setIndicator(LIST2_TAB_TAG,
                        res.getDrawable(R.drawable.ic_tab_nedlastet))
                .setContent(new TabContentFactory() {
                    public View createTabContent(String arg0) {
                        return lvNedlastet;
                    }
                }));
      th.addTab(th.newTabSpec(LIST1_TAB_TAG)
                .setIndicator(LIST1_TAB_TAG,
                        res.getDrawable(R.drawable.ic_tab_alle))
                .setContent(new TabContentFactory() {
                    public View createTabContent(String arg0) {
                        return lvAlle;
                    }
                }));

      th.setCurrentTabByTag(LIST1_TAB_TAG);
    }

    private OnItemLongClickListener createLongClickListener() {
        return 
        new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, final View view,
                       final int position, long id) {
                if(isAvailable(position)){
                    try{
                        new AlertDialog.Builder(parent.getContext())
                        .setMessage("Vil du slette bussruten?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                deleteFile(view, position);
                                sh.availability[position] = false;
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        })
                        .show();
                    }catch(Exception e){
                        toastText("ERROR: "+e.getMessage(), Toast.LENGTH_LONG);
                    }
                    return true;
                }else return false;
            }
        };
    }

    private OnItemClickListener createClickListenerAlle(){
        return 
            new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View view,
                       int position, long id){  

                   boolean temp = sh.availability[position];
                   if(temp == true){
                       if(Environment.getExternalStorageState().equals(
                               Environment.MEDIA_MOUNTED)){
                           File file = new File(StorageHandler.sdcardUrl+ StorageHandler.uniqueBussFilename[position]);

                           if (file.exists()) {//open the file
                               Uri path = Uri.fromFile(file);
                               Intent intent = new Intent(Intent.ACTION_VIEW);
                               intent.setDataAndType(path, "application/pdf");
                               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                               try {
                                   startActivity(intent);
                               } 
                               catch (ActivityNotFoundException e) {
                                   toastText("No Application Available for viewing PDF", Toast.LENGTH_LONG);
                               }
                           }else{
                               toastText("The file "+file.getAbsolutePath()+" was not found.", Toast.LENGTH_LONG);
                           }
                       }else{
                           toastText("sdkort er ikke tilgjengelig.", Toast.LENGTH_LONG);
                       }
                   }else{//Have to download the file.
                       connect();   
                       try { 
                            String absUrl = StorageHandler.webUrl[position];
                            InputStream inputStream = OpenHttpConnection(absUrl);
                            File tempFile = new File(StorageHandler.sdcardUrl+StorageHandler.uniqueBussFilename[position]);
                            File tempFile2 = new File(StorageHandler.sdcardUrl);
                            if(!tempFile.exists()){
                                tempFile2.mkdirs();
                                tempFile.createNewFile();
                            }
                            OutputStream output = new FileOutputStream(tempFile);
                            int bytesRead;
                            byte[] buffer = new byte[8 * 1024];
                            while((bytesRead = inputStream.read(buffer)) != -1){
                                output.write(buffer,0,bytesRead);
                            }


                            output.close();
                            inputStream.close();

                            toastText("Bussruten er lastet ned.", Toast.LENGTH_LONG);
                            sh.availability[position] = true;
                        } catch (IOException e) {
                            toastText("ERROR:Kan ikke laste ned bussruten.\nStacktrace:\n"+e.getMessage(), Toast.LENGTH_LONG);
                        }
                   }
               }
            };
    }

    private OnItemClickListener createClickListenerNedlastet(){
        return 
        new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                   int position, long id){  

               boolean temp = sh.availability[position];
               if(temp == true){
                   if(Environment.getExternalStorageState().equals(
                           Environment.MEDIA_MOUNTED)){
                       File file = new File(StorageHandler.sdcardUrl+ StorageHandler.uniqueBussFilename[position]);

                       if (file.exists()) {//open the file
                           Uri path = Uri.fromFile(file);
                           Intent intent = new Intent(Intent.ACTION_VIEW);
                           intent.setDataAndType(path, "application/pdf");
                           intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                           try {
                               startActivity(intent);
                           } 
                           catch (ActivityNotFoundException e) {
                               toastText("No Application Available for viewing PDF", Toast.LENGTH_LONG);
                           }
                       }else{
                           toastText("The file "+file.getAbsolutePath()+" was not found.", Toast.LENGTH_LONG);
                       }
                   }else{
                       toastText("sdkort er ikke tilgjengelig.", Toast.LENGTH_LONG);
                   }
               }
            };
        };
}
    private boolean isConnected(){
        if(sh.isEmulator){
            return true;
        }else{
            ConnectivityManager connMan = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            if (connMan.getActiveNetworkInfo().isConnected()){//getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
                return true;
            }else{
                return false;
            }
        }
    }
    private void toastText(String t, int l){
        final String text = t;
        final int length = l;
        Toast.makeText(getApplicationContext(), text, length).show();               

    }
    private void connect(){
        if(isConnected()){
            sh.isConnected = true;
            toastText("Kobler til...", Toast.LENGTH_SHORT);
        }else{
            sh.isConnected = false;
            toastText("Bussruten har ikke blitt lastet ned.\n" +
                    "Vennligst koble til internet for å laste ned ruten", Toast.LENGTH_LONG);   
        }
    }
    private InputStream OpenHttpConnection(String urlString) 
    throws IOException
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        if (!(conn instanceof HttpURLConnection))                     
            throw new IOException("Not an HTTP connection");

        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect(); 

            response = httpConn.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                 
            }                     
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");            
        }
        return in;     
    }
    /*
    private String urlWithSpaces2Percentage20(String url){
        String output = "";
        for(int i = 0;i<url.length();i++){
            char c = url.charAt(i);
            if(c == ' '){
                output = output + "%20";
            }else{
                output = output + c;
            }
        }
        return output;
    }
    private int getCreatedDateFromFullName(String name){
        return 0;
    }*/
    private boolean isAvailable(int position){
        File f = new File(StorageHandler.sdcardUrl + StorageHandler.uniqueBussFilename[position]);
        if(f.exists()){
            return true;
        }
        return false;
    }
    private void checkAvaliablePdfs(){
        for(int i = 0;i<StorageHandler.uniqueBussFilename.length;i++){
            File f = new File(StorageHandler.sdcardUrl + StorageHandler.uniqueBussFilename[i]);
            if(f.exists()){
                sh.availability[i] = true;
            }
        }
    }
    private void deleteFile(View view, int index){
        try{
            File f =  new File(StorageHandler.sdcardUrl + StorageHandler.uniqueBussFilename[index]);
            f.delete();
        }catch(Exception e){
            toastText("ERROR: "+e.getMessage(), Toast.LENGTH_LONG);
        }
    }
    /*
    private void setTextColor(View view, int id){
        if(availability[id]){
            view.setPressed(true);
        }
    }*/

    /**
     * Implement logic here when a tab is selected
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void onTabChanged(String tabName) {
        if(tabName.equals(LIST2_TAB_TAG)) {
            lvNedlastet.setAdapter(new ArrayAdapter(this, 
                    android.R.layout.simple_list_item_2, 
                    sh.getListNameNedlastet()));
        }
        else if(tabName.equals(LIST1_TAB_TAG)) {
            //do something
        }
    }
}

这是main.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <ListView android:id="@+id/list1" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1">
            </ListView>
            <ListView android:id="@+id/list2" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1">
            </ListView>
        </FrameLayout>
    </LinearLayout>
</TabHost>

这是来自DDMS的错误消息

01-19 21:40:13.198: ERROR/AndroidRuntime(16123): Uncaught handler: thread main exiting due to uncaught exception
01-19 21:40:13.838: ERROR/AndroidRuntime(16123): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.AbsListView.obtainView(AbsListView.java:1274)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.ListView.onMeasure(ListView.java:1060)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.View.measure(View.java:7964)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.View.measure(View.java:7964)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.View.measure(View.java:7964)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.View.measure(View.java:7964)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.View.measure(View.java:7964)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.View.measure(View.java:7964)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.View.measure(View.java:7964)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.os.Looper.loop(Looper.java:123)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at java.lang.reflect.Method.invoke(Method.java:521)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at dalvik.system.NativeStart.main(Native Method)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123): Caused by: java.lang.ClassCastException: android.widget.TwoLineListItem
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
01-19 21:40:13.838: ERROR/AndroidRuntime(16123):     ... 35 more

提前。谢谢! :)

2 个答案:

答案 0 :(得分:0)

这是一个非常简单的错误消息。来自文档:

  

管理ListView的ListAdapter   由任意数组支持   对象。默认情况下,此类需要   提供的资源ID   引用单个TextView。如果你   想要使用更复杂的布局,请使用   构造函数也需要一个   字段ID。该字段ID应该   引用较大的TextView   布局资源。

基本上,在你的onTabChanged方法中,当你构建ArrayAdapter而不是android.R.layout.simple_list_item_2时,它期望是TextView的id,而不是布局的id。您需要使用不同的构造函数来获取资源和TextView ID,因此在这种情况下,您应该能够使用:

lvNedlastet.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_2,
    android.R.id.text1, sh.getListNameNedlastet())); 

如果它比单个TextView更复杂,您应该考虑创建自己的自定义列表适配器。

答案 1 :(得分:0)

我在这里有很多困惑,(我从来没有以这种方式构建ListView ......)但是我得知你得到的错误是因为你在ArrayAdapter期望的时候使用布局资源(第二个参数)构造ArrayAdapter第二个参数是TextView资源。或者如果第二个参数是布局,那个布局必须包含TextView,我不确定android.R.layout.simple_list_item_1是否包含TextView,所以,请尝试这样做;

lvAlle.setAdapter(new ArrayAdapter(this, android.R.layout.android.R.id.text1, new String[]{"1"}));//sh.getListNameAlle()));

也许我不明白你的问题,但希望这有帮助。