如何建立正确使用onPostExecute和ListView数据表 [英] How to correctly build table with data using onPostExecute and ListView

查看:106
本文介绍了如何建立正确使用onPostExecute和ListView数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者Android开发者。我有一个问题:
我wan't从服务器解析数据,并比类似表屏幕显示此数据。我我选择使用的ListView 类似的表。在该方法中的 onDateSet 我启动传递的ListView为 Asyncr 对象,并开始解析我的数据。我有一个解析的数据没有问题,比 onPostExecute 我做一列表我的数据即可。比我创建的对象的 ArrayAdapter 并考绩我对这个对象我的数据,但我的屏幕上我有没有数据表。
有任何想法吗?
感谢所有。

I am beginner android developer. And i have next problem: I wan't to parse data from server and than display this data at the screen like a table. I I chose to use ListView like a table. In the method onDateSet i launch pass the ListView to Asyncr object and start to parse my data. I have no problem with parse data than in the onPostExecute i do one List of my data. Than i create object of ArrayAdapter and i pas to this object my data, but on my screen i have no table with data. Any ideas? Thanks to all.

$ C $的ç活动

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>

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

I have no errors, but i have no table on my screen .

推荐答案

您中缺少以下行 onPostExecute

ll.setAdapter(adapter);

您构建一个适配器的所有权利,但你需要告诉你的列表视图中使用它。

You construct an adapter all right, but then you need to tell your list view to use it.

这篇关于如何建立正确使用onPostExecute和ListView数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆