roid应用程序错误延伸BaseAdapter和利用Asynck

roid应用程序错误延伸BaseAdapter和利用Asynck

本文介绍了在Android应用程序错误延伸BaseAdapter和利用Asyncktask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要使用PHP应用程序的工作完美Android和MySQL数据库之间的连接android应用。仅显示用户操作不起作用这种方法在一些数据的列表显示用户。

谁能帮我解决这个错误?

的附加code的结果是

  04-14 23:17:57.766:D /这从互联网上(2367):usersList.get(位置).getDate()为null

LogCat中错误

  04-14 21:59:22.170:E / AndroidRuntime(1951年):致命异常:主要
04-14 21:59:22.170:E / AndroidRuntime(1951年):显示java.lang.NullPointerException
04-14 21:59:22.170:E / AndroidRuntime(1951年):在com.devleb.loginDemo.CustomAdapter.getView(CustomAdapter.java:60)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.AbsListView.obtainView(AbsListView.java:2177)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.ListView.makeAndAddView(ListView.java:1840)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.ListView.fillDown(ListView.java:675)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.ListView.fillFromTop(ListView.java:736)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.ListView.layoutChildren(ListView.java:1655)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.AbsListView.onLayout(AbsListView.java:2012)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.View.layout(View.java:14289)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.View.layout(View.java:14289)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.View.layout(View.java:14289)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.View.layout(View.java:14289)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.View.layout(View.java:14289)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5481)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.Choreographer.doFrame(Choreographer.java:532)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.os.Handler.handleCallback(Handler.java:730)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.os.Handler.dispatchMessage(Handler.java:92)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.os.Looper.loop(Looper.java:137)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在android.app.ActivityThread.main(ActivityThread.java:5103)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在java.lang.reflect.Method.invokeNative(本机方法)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在java.lang.reflect.Method.invoke(Method.java:525)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-14 21:59:22.170:E / AndroidRuntime(1951年):在dalvik.system.NativeStart.main(本机方法)

list_item.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@绘制/ background_1
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / UID
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:知名度=水涨船高/>    <的TextView
        机器人:ID =@ + ID /名称
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingLeft =6DIP
        机器人:paddingTop =20dip
        机器人:文本=文本视图
        机器人:文字颜色=#FFFFFF
        机器人:TEXTSIZE =17dip
        机器人:文字样式=大胆/>    <的TextView
        机器人:ID =@ + ID / txtCreateDate
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真
        机器人:文字=TextView的
        机器人:文字颜色=#FFFFFF/>< / RelativeLayout的>

User.java

 包com.devleb.loginDemo;进口的java.util.HashMap;// @燮pressWarnings(串行)
公共类用户扩展的HashMap<字符串,字符串> {    / **
     *
     * /
    字符串名称;
    字符串日期;    公共字符串的getName(){
        返回名称;
    }    公共无效setname可以(字符串名称){
        this.name =名称;
    }    公共字符串GETDATE(){
        归期;
    }    公共无效的setDate(字符串日期){
        this.date =日期;
    }}

CustomAdapter.java

 包com.devleb.loginDemo;进口的java.util.ArrayList;
进口java.util.Date;进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;公共类CustomAdapter延伸BaseAdapter {    私有静态的ArrayList<使用者> usersList =新的ArrayList<使用者>();    //私有静态ArrayList的名称,日期;
    LayoutInflater layoutInflater;
    的String []用户名;
    日期[] createdDate;
    上下文语境;    @燮pressWarnings(rawtypes)
    公共CustomAdapter(ArrayList的<使用者>因此,语境三){
        usersList =结果;
        上下文= C;    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回usersList.size();
    }    @覆盖
    公共对象的getItem(INT为arg0){
        // TODO自动生成方法存根
        返回usersList.get(为arg0);
    }    @覆盖
    众长getItemId(INT为arg0){
        // TODO自动生成方法存根
        返回将arg0;
    }    @覆盖
    公共查看getView(INT位置,查看ARG1,父母的ViewGroup){
        // TODO自动生成方法存根        layoutInflater =(LayoutInflater)上下文
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看排= NULL;        行= layoutInflater.inflate(R.layout.list_item,父母,假);
        TextView的txtCreateDate =(TextView中)一行
                .findViewById(R.id.txtCreateDate);        txtCreateDate.setText(usersList.get(位置).getDate());        TextView的txtName的=(TextView中)row.findViewById(R.id.name);        txtName.setText(usersList.get(位置).getName());
        返回行;
    }}

UserListActivity.java

 包com.devleb.loginDemo;进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;进口org.apache.http.NameValuePair;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.app.Activity;
进口android.app.ListActivity;
进口android.content.Intent;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;
进口android.widget.Toast;公共类UserListActivity扩展ListActivity {    JSONParser jsonParser =新JSONParser();    // ArrayList的<&HashMap的LT;字符串,字符串>> usersList;    // ****从Internt的加入************* ///
    @燮pressWarnings(rawtypes)
    ArrayList的<使用者> arrayList_userList;
    ListView的lvUsers;
    // ****从Internt的加入************* ///    私人静态字符串url_display_user =htt​​p://10.0.3.2/android_connect/display_user.php;    // JSON节点名称
    私有静态最后弦乐TAG_SUCCESS =成功;
    私有静态最后弦乐TAG_MESSAGE =消息;    私有静态最后弦乐TAG_ID =ID;    私有静态最后弦乐TAG_USERS =用户列表;    私有静态最后弦乐TAG_USER =用户;
    私有静态最后弦乐TAG_DATE =日期;    //私有静态最后弦乐TAG_NAME =名;    //员工JSONArray
    JSONArray用户= NULL;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_user_list);        // usersList =新的ArrayList<&HashMap的LT;字符串,字符串>>();        新getUserList()执行();        // getListView
        lvUsers =(ListView控件)findViewById(android.R.id.list);        lvUsers.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,观景,INT ARG2,
                    长ARG3){                //字符串ID =((TextView中)
                // view.findViewById(R.id.uid))。gettext的()
                //的ToString();                在=新意图(getBaseContext(),StatusList.class)//意向;
                // in.putExtra(TAG_ID,UID);                // startActivity(在);
            }
        });
    }    类getUserList扩展的AsyncTask<字符串,字符串,字符串> {        / **
         *启动后台线程显示进度对话框之前
         * * /
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();            UserListActivity.this.setProgressBarIndeterminateVisibility(真);
        }        @覆盖
        保护字符串doInBackground(字符串... PARAMS){
            // TODO自动生成方法存根            //大厦参数
            清单<&的NameValuePair GT;参数应用=新的ArrayList<&的NameValuePair GT;();
            // URL从获取JSON字符串
            JSONObject的JSON = jsonParser.makeHtt prequest(url_display_user,
                    GET上,参数);            //检查JSON效应初探日志的猫
            Log.d(所有用户,json.toString());            尝试{
                //检查成功TAG
                INT成功= json.getInt(TAG_SUCCESS);                如果(成功== 1){
                    发现//产品
                    //获取产品阵列
                    用户= json.getJSONArray(TAG_USERS);                    //通过所有用户循环
                    的for(int i = 0; I< users.length();我++){
                        JSONObject的C = users.getJSONObject(I)                        //存储在变量中的每个JSON项目
                        字符串ID = c.getString(TAG_ID);
                        字符串用户= c.getString(TAG_USER);
                        字符串CDATE = c.getString(TAG_DATE)的ToString();                        //创建新的HashMap
                        用户地图=新用户();                        //将每个子节点的HashMap键=>值
                        map.put(TAG_ID,身份证);
                        map.put(TAG_USER,用户);
                        map.put(TAG_DATE,CDATE);                        //添加HashList到ArrayList的                        // usersList.add(图)                        arrayList_userList.add(地图);
                    }
                    // // ***********
                    //从互联网                    // arrayList_userList =新
                    // ArrayList的<使用者>(usersList.value())                    // // **************                    返回json.getString(TAG_MESSAGE);
                }其他{                    返回json.getString(TAG_MESSAGE);                }
            }赶上(JSONException E){
                e.printStackTrace();
            }            返回null;
        }        / **
         *在完成后台任务之后辞退进度对话框
         * ** /
        保护无效onPostExecute(字符串结果){
            //让所有的产品后,关闭该对话框
            如果(结果!= NULL){                UserListActivity.this
                        .setProgressBarIndeterminateVisibility(假);                / **
                 *更新解析JSON数据到ListView控件
                 * * /
                / *
                 * ListAdapter适配器=新
                 * SimpleAdapter(UserListActivity.this,usersList,
                 * R.layout.list_item,新的String [] {TAG_ID,TAG_USER},新
                 * INT [] {R.id.uid,R.id.name}); //更新列表视图
                 * setListAdapter(适配器);
                 * /
                lvUsers.setAdapter(新CustomAdapter(arrayList_userList,
                        getBaseContext()));                Toast.makeText(getBaseContext(),结果,Toast.LENGTH_LONG)
                        。显示();            }
        }    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.user_list,菜单);
        返回true;
    }}


解决方案

您没有的setDate(),所以GETDATE()为null。

  //创建新的HashMap
                    用户地图=新用户();                    //将每个子节点的HashMap键=>值
                    map.put(TAG_ID,身份证);
                    map.put(TAG_USER,用户);
                    map.put(TAG_DATE,CDATE);

我看不到任何使用HashMap的是任何一个code的。但对于测试,只是离开这个位置。

添加
        map.setName(用户);
        map.setDate(CDATE);

这就是它。

i have android application that need connection between android and mysql database using php the application work perfect. only the display user action do not work this method display the user in a list with some data.

can anyone help me to fix this error ??

the result of the added code is

04-14 23:17:57.766: D/this from internet(2367): usersList.get(position).getDate() is null

LogCat Error

   04-14 21:59:22.170: E/AndroidRuntime(1951): FATAL EXCEPTION: main
04-14 21:59:22.170: E/AndroidRuntime(1951): java.lang.NullPointerException
04-14 21:59:22.170: E/AndroidRuntime(1951):     at com.devleb.loginDemo.CustomAdapter.getView(CustomAdapter.java:60)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.ListView.makeAndAddView(ListView.java:1840)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.ListView.fillDown(ListView.java:675)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.ListView.fillFromTop(ListView.java:736)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.ListView.layoutChildren(ListView.java:1655)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.AbsListView.onLayout(AbsListView.java:2012)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.View.layout(View.java:14289)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.View.layout(View.java:14289)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.View.layout(View.java:14289)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.View.layout(View.java:14289)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.View.layout(View.java:14289)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.Choreographer.doFrame(Choreographer.java:532)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.os.Handler.handleCallback(Handler.java:730)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.os.Looper.loop(Looper.java:137)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at java.lang.reflect.Method.invoke(Method.java:525)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-14 21:59:22.170: E/AndroidRuntime(1951):     at dalvik.system.NativeStart.main(Native Method)

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/uid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="20dip"
        android:text="text view"
        android:textColor="#FFFFFF"
        android:textSize="17dip"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtCreateDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView"
        android:textColor="#FFFFFF" />

</RelativeLayout>

User.java

package com.devleb.loginDemo;

import java.util.HashMap;

//@SuppressWarnings("serial")
public class User extends HashMap<String, String> {

    /**
     *
     */
    String name;
    String date;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}

CustomAdapter.java

package com.devleb.loginDemo;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

    private static ArrayList<User> usersList = new ArrayList<User>();

    // private static ArrayList name, date;
    LayoutInflater layoutInflater;
    String[] userName;
    Date[] createdDate;
    Context context;

    @SuppressWarnings("rawtypes")
    public CustomAdapter(ArrayList<User> result, Context c) {
        usersList = result;
        context = c;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return usersList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return usersList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup parent) {
        // TODO Auto-generated method stub

        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = null;

        row = layoutInflater.inflate(R.layout.list_item, parent, false);
        TextView txtCreateDate = (TextView) row
                .findViewById(R.id.txtCreateDate);

        txtCreateDate.setText(usersList.get(position).getDate());

        TextView txtName = (TextView) row.findViewById(R.id.name);

        txtName.setText(usersList.get(position).getName());
        return row;
    }

}

UserListActivity.java

package com.devleb.loginDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class UserListActivity extends ListActivity {

    JSONParser jsonParser = new JSONParser();

    // ArrayList<HashMap<String, String>> usersList;

    // **** added from internt*************///
    @SuppressWarnings("rawtypes")
    ArrayList<User> arrayList_userList;
    ListView lvUsers;
    // **** added from internt*************///

    private static String url_display_user = "http://10.0.3.2/android_connect/display_user.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    private static final String TAG_ID = "id";

    private static final String TAG_USERS = "userlist";

    private static final String TAG_USER = "user";
    private static final String TAG_DATE = "date";

    // private static final String TAG_NAME = "name";

    // employees JSONArray
    JSONArray users = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);

        // usersList = new ArrayList<HashMap<String, String>>();

        new getUserList().execute();

        // getListView
        lvUsers = (ListView) findViewById(android.R.id.list);

        lvUsers.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                    long arg3) {

                // String id = ((TextView)
                // view.findViewById(R.id.uid)).getText()
                // .toString();

                // Intent in = new Intent(getBaseContext(), StatusList.class);
                // in.putExtra(TAG_ID, uid);

                // startActivity(in);
            }
        });
    }

    class getUserList extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            UserListActivity.this.setProgressBarIndeterminateVisibility(true);
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            // Building Parameters
            List<NameValuePair> parametres = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(url_display_user,
                    "GET", parametres);

            // Check your log cat for JSON reponse
            Log.d("All Users: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    users = json.getJSONArray(TAG_USERS);

                    // looping through All Users
                    for (int i = 0; i < users.length(); i++) {
                        JSONObject c = users.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_ID);
                        String user = c.getString(TAG_USER);
                        String cdate = c.getString(TAG_DATE).toString();

                        // creating new HashMap
                        User map = new User();

                        // adding each child node to HashMap key => value
                        map.put(TAG_ID, id);
                        map.put(TAG_USER, user);
                        map.put(TAG_DATE, cdate);

                        // adding HashList to ArrayList

                        // usersList.add(map);

                        arrayList_userList.add(map);
                    }
                    // ***********************************//
                    // From the internet

                    // arrayList_userList = new
                    // ArrayList<User>(usersList.value())

                    // **********************************//

                    return json.getString(TAG_MESSAGE);
                } else {

                    return json.getString(TAG_MESSAGE);

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

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String result) {
            // dismiss the dialog after getting all products
            if (result != null) {

                UserListActivity.this
                        .setProgressBarIndeterminateVisibility(false);

                /**
                 * Updating parsed JSON data into ListView
                 * */
                /*
                 * ListAdapter adapter = new
                 * SimpleAdapter(UserListActivity.this, usersList,
                 * R.layout.list_item, new String[] { TAG_ID, TAG_USER }, new
                 * int[] { R.id.uid, R.id.name }); // updating listview
                 * setListAdapter(adapter);
                 */
                lvUsers.setAdapter(new CustomAdapter(arrayList_userList,
                        getBaseContext()));

                Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG)
                        .show();

            }
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.user_list, menu);
        return true;
    }

}
解决方案

You didn't setDate(), so getDate() is null.

                    // creating new HashMap
                    User map = new User();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_USER, user);
                    map.put(TAG_DATE, cdate);

I don't see any use of HashMap is any of the code. But for testing, just leave this here.

Add map.setName(user); map.setDate(cdate);

That's it.

这篇关于在Android应用程序错误延伸BaseAdapter和利用Asyncktask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:11