Listview没有显示第一个条目

时间:2013-08-21 13:28:04

标签: android-listview

我在listview中显示来自服务器的所有文件。

服务器上的文件如下: binary.txt / erpTestBench / muneem.php / oglPrahova / permitCore / workOrderTestBench /

Listview显示除binary.txt文件以外的所有文件。

我的listview xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
        >


    <TextView
            android:id="@+id/path"
            android:layout_width="fill_parent"
            android:textSize="12dp"
            android:layout_height="wrap_content" android:background="#2377ff"/>
    <ListView
            android:id="@android:id/list"
            android:cacheColorHint="#00000000"
            android:layout_width="fill_parent"
            android:layout_height="70dp"
            android:layout_weight="0.85"
            android:clickable="false"
            android:textSize="30dp"
            android:drawSelectorOnTop="false" android:background="#2f3fc8" android:layout_gravity="center"
            android:dividerHeight="15dp"/>
    <TextView
            android:id="@android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="No Data"
            android:layout_weight="0.75"
            android:background="#2f3fc8"/>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:paddingTop="6dp" android:paddingBottom="6dp" android:gravity="center|center_vertical"
            android:id="@+id/linearLayout" android:background="#346684">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1"
                android:src="@drawable/power"
                android:onClick="bt_Quit" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
                android:layout_gravity="center"/>
        <ImageView
                android:layout_width="80dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageView4"
                android:src="@drawable/back"
                android:onClick="back"
                android:layout_marginLeft="10dp" android:layout_marginRight="10dp"/>
        <ImageView
                android:layout_width="71dp"
                android:layout_height="fill_parent"
                android:id="@+id/imageView2"
                android:src="@drawable/home"

                android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:onClick="home"/>
    </LinearLayout>




</LinearLayout>

我的java文件如下:

public class serv extends ListActivity {
    private String m_urlString="XXXXX";
//    private String result;

    private List<String> m_item = null;
    private List<String> m_path = null;
    private String m_root="XXXX";
    private String m_result;
    private TextView m_myPath;
    static private String m_pos;
    private String m_backposition;
    private String m_fileURL;


    int m_downloadedSize = 0;
    int m_totalSize = 0;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file_chooser);

        View v= findViewById(R.id.rowtext);


        m_myPath = (TextView)findViewById(R.id.path);
        m_fileURL="http://192.168.1.30/muneem/";
        Http_connection f=new Http_connection();
        f.execute("");


    }

    class Http_connection extends AsyncTask<String, Void, Void> {

        private Exception exception;

        protected Void  doInBackground(String... urls)
        {

            try
            {
                URL url= new URL(m_urlString);
                HttpURLConnection con=(HttpURLConnection)url.openConnection();
                con.setRequestMethod("GET");
                con.connect();
                int statusCode=con.getResponseCode();
                if (statusCode==HttpURLConnection.HTTP_OK){
                    BufferedReader in= new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String line;
                    m_result="";
                    while ((line=in.readLine())!=null)
                    {
                        m_result=m_result+"\n"+line;
                    }
                    in.close();
                    con.disconnect();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            getDir(m_urlString);


                        }
                    });

                }
            }
            catch (MalformedURLException e)
            {
                //            bundle.putString("Error","Problem with URL");

            }
            catch (IOException e)
            {
                //              bundle.putString("Error","Problem with connection");

            }


            return null;
        }
    }
    private void getDir(String dirPath)
    {


        String[] r=m_result.split("/");
        m_myPath.setText("Location: " + dirPath);
        m_item = new ArrayList<String>();
        m_path = new ArrayList<String>();

        for (int k=0;k<r.length;k++)
        {
            if (r[k].contains("."))
            {
                m_item.add(r[k]);

            }
            else
            {
                m_item.add(r[k]+"/");
            }
        }


        ArrayAdapter<String> fileList =
                new ArrayAdapter<String>(serv.this, R.layout.row, m_item);
        setListAdapter(fileList);

    }

1 个答案:

答案 0 :(得分:0)

您的数据在进入ListView之前会经历几个步骤,因此问题的根源可能是几个不同的步骤之一。如果您调试代码并在m_result开头检查getDir()的值,则可以确定问题的来源。如果值是预期的,则getDir()中可能存在问题,而如果字符串出现问题,则问题可能在doInBackground()(如果您知道结果有什么问题,则很容易发现,事实证明。)

相关问题