【问题标题】:i need to change my listview to extend baseAdapter using the ArrayList<HashMap<String, String>>我需要更改我的列表视图以使用 ArrayList<HashMap<String, String>> 扩展 baseAdapter
【发布时间】:2014-05-27 00:08:39
【问题描述】:

我有一个使用列表视图并在从 mysql 数据库获取应用程序后显示值的活动,应用程序工作正常,但现在我想将列表活动更改为扩展 BaseAdapter,因为我需要在第 2 行文本视图中添加。

谁能帮我做出这个改变????

这是我的代码

用户.java

package com.devleb.loginDemo;

import java.util.Date;

public class User {

    String name;

    public String getName() {
        return name;
    }

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

    public Date getDate() {
        return date;
    }

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

    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;

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

    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().toString());

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

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

}

现在如何将此自定义适配器添加到我的 UserListActivity ???

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;

    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 = "create_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
        ListView lv = getListView();

        lv.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);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

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

                        // adding HashList to ArrayList
                        usersList.add(map);

                    }
                    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);

                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;
    }

}

我知道必须在 OnPostExecute() 中进行更改

【问题讨论】:

  • 如果你想改变你看到的东西,应该在getView中完成改变......

标签: android mysql json android-listview arraylist


【解决方案1】:

这是你的问题吗?

 public class CustomAdapter extends BaseAdapter {

        private static ArrayList<User> usersList;

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

        public CustomAdapter(ArrayList<HashMap<String,String> 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) {
            //Get the HashMap
            HashMap<String,String> map = userList.get(position);

            //do what ever you want with the hashmap
            return row;
        }

    }

【讨论】:

  • 不,正如您在我的 BaseAdapter 代码中看到的那样,我正在初始化一个 private static ArrayList usersList; 并且此 customAdapter 的构造函数作为参数 arrayList 在我的UserListActivity 我需要和 hashMap (ArrayList> usersList;) 因为 arrayList 不处理 HashMap 所以如何修复我所要求的这个错误。
  • 所以你问的是如何在 BaseAdapter 中处理 hashmap?您只需从 getView 中的 hashmap 读取数据。 BaseAdapter 不需要处理哈希图,因为您有一个 ArrayList。其中的项目是一个哈希图。所以你可以在 getview 中读取每一个 hashmap。并启动一个 UI 线程并创建新的 listView/Activity 以查看结果,因为您处于异步任务中
  • 但我的 CustomAdapter 的构造函数将有一个 ArrayList 作为参数,我需要有一个 ArrayList> 来处理 JSON 节点作为键 和 字符串作为值 你明白我的意思吗??
  • 是的。我没有看到问题。更改构造函数。给 ArrayList> 甚至两者都在里面。我编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2015-05-30
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 2022-01-09
相关资源
最近更新 更多