1

我是android新手,如果有人像这样愚蠢的问题,我很抱歉

我有一个扩展 Listactivty 的活动。Listview 首次加载时显示数据正常。它显示来自服务器的所有文件和文件夹。但是,当我单击列表视图时,会调用 onListItemClick 事件,它会再次从服务器获取数据,但它会将数据附加到以前的数据中。我想要的是 ListView 只显示最新的数据而没有附加数据。

我的 ListView 活动代码:

public class MainActivity extends ListActivity {
    private String urlString="http://xxxxxxxxserver PathXXXXX";
    private List<String> item = null;
    private List<String> path = null;
    private String root="http://xxxxxxxxserver PathXXXXX";
    private String result;
    private TextView myPath;
    static private String pos;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View v= findViewById(R.id.rowtext);
        myPath = (TextView)findViewById(R.id.path);
        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(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;
                    while ((line=in.readLine())!=null)
                    {
                        result=result+"\n"+line;
                    }
                    in.close();
                    con.disconnect();
                    root="AndroidMapper";
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            getDir(urlString);
                        }
                    });
             }
            }

 private void getDir(String dirPath)
    {


        String\[\] r=result.split("/");
        myPath.setText("Location: " + urlString);
        item = new ArrayList<String>();
        path = new ArrayList<String>();

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

            }
            else
            {
                item.add(r\[k\]+"/");
            }
        }
        ArrayAdapter<String> fileList =
                new ArrayAdapter<String>(MainActivity.this, R.layout.row, item);
        setListAdapter(fileList);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub

        String pos=new String(item.get(position));

        if (pos.contains("."))
        {
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
        else
        {

   urlString=urlString+pos;



            Http_connection f=new Http_connection();
            f.execute("");

        }

http://i.stack.imgur.com/HIEL7.png

4

1 回答 1

2

由于这一行,您需要重新初始化结果变量:

result=result+"\n"+line;

这种方式可能:

String line;
result =  "";
while ((line=in.readLine())!=null)
  {
    result=result+"\n"+line;
  }
于 2013-08-07T07:55:44.380 回答