如何使用onPostExecute和ListView正确构建包含数据的表

时间:2014-02-26 17:06:11

标签: java android

我是初学Android开发者。我有下一个问题: 我不想从服务器解析数据,而是像屏幕一样在屏幕上显示这些数据。我选择像表一样使用 ListView 。在方法 onDateSet 中,我启动将ListView传递给 Asyncr 对象并开始解析我的数据。我对解析数据没有问题,而不是 onPostExecute 我做了一个列表我的数据。比我创建 ArrayAdapter 的对象,并将此数据传递给此对象,但在我的屏幕上,我没有数据表。 有任何想法吗? 谢谢大家。

活动

的代码
package com.example.earchive;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class InboxActivity extends Activity implements View.OnClickListener {




    DateFormat format = DateFormat.getInstance();
    Calendar calendar = Calendar.getInstance();
    Button filter_bt;
    public String propertyNumber,sess_id;




    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inbox);
        filter_bt = (Button)findViewById(R.id.filter_button);
        filter_bt.setOnClickListener(this);

        TableLayout emailsTable;
        //emailsTable = (TableLayout)findViewById(R.id.emailsTable);

        TableRow row;
        this.selectdate();

    }




    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH,monthOfYear);

            int Syear  = calendar.get(Calendar.YEAR);
            int Smonth = calendar.get(Calendar.MONTH);
            Smonth = Smonth + 1;
            InboxActivity i = new InboxActivity();


            String user_id = getIntent().getExtras().getString(LoginActivity.SESSION_ID);           

            FetchTask fetch = new FetchTask();
            fetch.Selectedmonth = Smonth;
            fetch.Selectedyear = Syear;
            fetch.page = 0;
            fetch.sess_id = user_id;
            **ListView ll = (ListView)findViewById(R.id.mailList);
            fetch.ll = ll;**
            fetch.execute();
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login, menu);
        return true;    
    }


    public void setDate()
    {
        new DatePickerDialog(InboxActivity.this,d,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
    }


    @Override
    public void onClick(View v) {
        this.setDate();
    }    


    public void selectdate()
    {

    }


public class FetchTask extends AsyncTask<Void, Void, JSONArray> {

        public JSONArray result_arr;
        public String result_str,email,password,test;
        public int Selectedyear;
        public int Selectedmonth;
        public int page;
        public String sess_id;
        public ListView ll;

        @Override

        protected JSONArray doInBackground(Void... params) {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("MY HOST");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);




                nameValuePairs.add(new BasicNameValuePair("act", "psots"));
                nameValuePairs.add(new BasicNameValuePair("debug", "1"));
                nameValuePairs.add(new BasicNameValuePair("t", "0"));
                nameValuePairs.add(new BasicNameValuePair("m", Integer.toString(this.Selectedmonth)));
                nameValuePairs.add(new BasicNameValuePair("y", Integer.toString(this.Selectedyear)));
                nameValuePairs.add(new BasicNameValuePair("st", Integer.toString(this.page)));
                nameValuePairs.add(new BasicNameValuePair("sess_id", this.sess_id));


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                sb.append(reader.readLine());
                String line = "0";
                while ((line = reader.readLine()) != null) 
                {
                    sb.append(line);
                }
                reader.close();
                String result11 = sb.toString();
                System.out.println(result11);
                this.result_str = result11;
                // parsing data
                JSONArray arr = new JSONArray(result11);





                return new JSONArray(result11);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(JSONArray result)
        {

            if (result != null) 
            {
                List<String> subjects = new ArrayList<String>();
                List<String> emails = new ArrayList<String>();

                for(int i = 0; i < result.length(); i++)
                {
                    try 
                    {
                        JSONObject json_data = result.getJSONObject(i);
                        emails.add(json_data.getString("mittente"));
                        subjects.add(json_data.getString("oggetto"));


                    } 
                    catch (JSONException e) 
                    {
                        e.printStackTrace();
                    }

                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    InboxActivity.this,
                    R.layout.da_item,
                    emails
                );  
            } 
            else 
            {
                System.out.println("Messages not found");

            }        
        }
    }   
}

我的 activityInbox.xml 文件

<RelativeLayout 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:background="@drawable/bkg_2"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LoginActivity" >

    <TextView
        android:id="@+id/errorMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="29dp"
        android:textColor="@color/red" />

    <Button
        android:id="@+id/filter_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/errorMessage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="128dp"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="19dp"
        android:background="@color/red"
        android:text="Filter by date"
        android:textColor="@color/white" />

    <ListView
        android:id="@+id/mailList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/filter_button"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="61dp" >

    </ListView>

</RelativeLayout>

我的 da_item.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


</TextView>

我没有错误,但我的屏幕上没有桌子。

1 个答案:

答案 0 :(得分:0)

您遗漏了onPostExecute中的以下行:

ll.setAdapter(adapter);

您可以构建适配器,但是您需要告诉列表视图才能使用它。