用 Kumulos 填充 ListView? [英] Populating a ListView with Kumulos?

查看:25
本文介绍了用 Kumulos 填充 ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Kumulos 填充 ListView 时遇到了很多困难.通过我的研究,我发现了许多关于使用 SQLite 或其他数据库的教程和帖子,但没有使用 Kumulos =/.

I am having a lot of difficulty using Kumulos to populate a ListView. Through my research Ive found numerous tutorials and postings about using SQLite or other databases but nothing with Kumulos =/.

我需要什么帮助:

1) 实现 Kumulos 来填充 ListView

1) Implementation of Kumulos to fill a ListView

来源:https://docs.kumulos.com/integration/android/

进行的研究:

在base64字符串中编码和解码位图对象安卓

HashMap 到 ListView

如何从 DBHelper 检索数据多列ListView中的HashMap

HashMap 到 ListView

主要活动:

public class PersonSearchPop extends Activity {

private ListView personlist;
private CustomListViewAdapter customListViewAdapter;
public static final String YOUR_API_KEY = "HIDDEN";
public static final String YOUR_SECRET_KEY = "HIDDEN";

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

public static Bitmap decodeBase64(String input)
{
    byte[] decodedBytes = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}


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

    Kumulos.initWithAPIKeyAndSecretKey(YOUR_API_KEY, YOUR_SECRET_KEY, this);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int) (width * .4), (int) (height * .6));

   ?????????????????????????????????????????????????????????????????????????????????


    personlist.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            finish();
        }
    });
}

}

自定义适配器:

public class CustomListViewAdapter extends CursorAdapter {

public CustomListViewAdapter(Context context, Cursor c){
    super (context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.personlist_row, parent, false);
    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView dl = (TextView) view.findViewById(R.id.tvdl);
    dl.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7))));

    TextView last = (TextView) view.findViewById(R.id.tvLastName);
    last.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

    TextView first = (TextView) view.findViewById(R.id.tvFirstName);
    first.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));

    TextView middle = (TextView) view.findViewById(R.id.tvMiddleName);
    middle.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));

    TextView ss = (TextView) view.findViewById(R.id.tvSS);
    ss.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(8))));

    //ImageView image = (ImageView) view.findViewById(R.id.idPic);
    //image.setImageDrawable(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(12))));
}

}

输入数据:

public class EnterData extends Activity {

EditText lName;
EditText dl;
EditText ss;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.persons_popup);

    lName = (EditText) findViewById(R.id.etLastName);
    dl = (EditText) findViewById(R.id.etDL);
    ss = (EditText) findViewById(R.id.etSocial);
}

public void onClickSearch (View btnSearch) {

    String personName = lName.getText().toString();
    String personDL = dl.getText().toString();
    String personSS = ss.getText().toString();

    }
}

}

更新:好的,我将我的代码更改为两个类,它似乎可以正常工作.但是,我从 Kumulos 收到错误 =/.

UPDATE: Ok, I changed my code to just two classes and it seems to be working. I am getting an error from Kumulos however =/.

Kumulos 错误:{"responseCode":32,"responseMessage":"无效请求:","payload":null,"sessionToken":"b29366e44a7cdbb905db18b51995e545daf4f816","re​​questedMethod":"searchPerson":"requestedFormatjson","timestamp":1462147387,"requestReceivedTime":1462147387,"maxAllowedRequestTime":40,"requestProcessingTime":0.012163877487183}

Kumulos error: {"responseCode":32,"responseMessage":"Invalid request: ","payload":null,"sessionToken":"b29366e44a7cdbb905db18b51995e545daf4f816","requestedMethod":"searchPerson","requestedFormat":"json","timestamp":1462147387,"requestReceivedTime":1462147387,"maxAllowedRequestTime":40,"requestProcessingTime":0.012163877487183}

PersonSearchPop:

PersonSearchPop:

public class PersonSearchPop extends ListActivity {

public static final String YOUR_API_KEY = "HIDDEN";
public static final String YOUR_SECRET_KEY = "HIDDEN";

static class Person {

    public long personID;
    public String lastName;
    public String middleName;
    public String firstName;
    public String dateOfBirth;
    public String personAddress;
    public int phoneNumber;
    public int driversLicense;
    public int socialSecurity;
    public String personRace;
    public String personSex;
    public String personAge;

    public static Person createFromGenericMap(Map<String, Object> object) {

        Person p = new Person();

        p.personID = (long) object.get("personID");
        p.lastName = (String) object.get("lastName");
        p.middleName = (String) object.get("middleName");
        p.firstName = (String) object.get("firstName");
        p.dateOfBirth = (String) object.get("dob");
        p.personAddress = (String) object.get("address");
        p.phoneNumber = (int) object.get("phone");
        p.driversLicense = (int) object.get("dl");
        p.socialSecurity = (int) object.get("ss");
        p.personRace = (String) object.get("race");
        p.personSex = (String) object.get("sex");
        p.personAge = (String) object.get("age");

        return p;
    }

}

static class PersonAdapter extends BaseAdapter {

    private List<Person> people;

    public PersonAdapter(List<Person> people) {
        this.people = people;
        //inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return people.size();
    }

    @Override
    public Object getItem(int position) {
        return people.get(position);
    }

    @Override
    public long getItemId(int position) {
        Person p = people.get(position);
        return p.personID;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;

        if (convertView == null) {


            TextView dl = (TextView) view.findViewById(R.id.tvdl);
            TextView last = (TextView) view.findViewById(R.id.tvLastName);
            TextView first = (TextView) view.findViewById(R.id.tvFirstName);
            TextView middle = (TextView) view.findViewById(R.id.tvMiddleName);
            TextView ss = (TextView) view.findViewById(R.id.tvSS);

            Person mperson = people.get(position);

            dl.setText(mperson.driversLicense);
            last.setText(mperson.lastName);
            first.setText(mperson.firstName);
            middle.setText(mperson.middleName);
            ss.setText(mperson.socialSecurity);
            //ImageView image = (ImageView) view.findViewById(R.id.idPic);
            //image.setImageDrawable(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(12))));
        }
        return view;
    }
}

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

    Kumulos.initWithAPIKeyAndSecretKey(YOUR_API_KEY, YOUR_SECRET_KEY, this);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int) (width * .4), (int) (height * .6));

    // Call Kumulos
    Map<String, String> params = new HashMap<>();
    Intent intent = getIntent();
    String lastName = intent.getStringExtra("lastName");
    params.put("lastName",String.valueOf(lastName));
    Kumulos.call("searchPerson", params, new ResponseHandler() {

        // Handle result
        @Override
        public void didCompleteWithResult(Object result) {
            super.didCompleteWithResult(result);

            // Cast generic response down to list of maps
            ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String, Object>>) result;

            // Create a list for the models
            ArrayList<Person> people = new ArrayList<>();

            // Map models from generic objects and add to list
            for (Map<String, Object> personObject : objects) {
                Person p = Person.createFromGenericMap(personObject);
                people.add(p);
            }

            // Create adapter with model list
            final PersonAdapter adapter = new PersonAdapter(people);

            // Set adapter on main UI thread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setListAdapter(adapter);
                }
            });
        }
    });
}

}

PersonsPop

 @Override
public void onClick(View v){
    switch(v.getId()){
        case R.id.bCancelPerson:
            finish();
            break;
        case R.id.bSearchPerson:
            EditText lastName = (EditText) findViewById(R.id.etLastName);
            Intent intent = new Intent(PersonsPop.this, PersonSearchPop.class);
            intent.putExtra("lastName", lastName.getText().toString());
            startActivity(new Intent(PersonsPop.this, PersonSearchPop.class));

推荐答案

我是来自 Kumulos 的 Chris —— 让我试着为您指明正确的方向.

I'm Chris from Kumulos -- let me try to point you in the right direction.

因此,一般来说,在列表视图中显示来自 Web 服务的数据的过程如下所示:

So in general, the process of displaying data in a list view from a web service looks like:

  1. 调用网络服务并检索数据列表

  1. Call out to the web service and retrieve the list of data

将这些对象映射到模型类,然后保存在内存中或保存到磁盘

Map those objects down to model classes and either hold in memory or persist to disk

创建一个由手机数据支持的适配器

Create an adapter that is backed by the data on the phone

将列表视图的适配器设置为您的自定义适配器实例

Set the adapter of the list view to be your custom adapter instance

假设您将对象保存在内存中,创建一个由您的模型类型的 ArrayList 支持的自定义适配器就足够了.这是最简单的入门方法之一.

Assuming that you would hold the objects in memory, creating a custom adapter backed by an ArrayList of your model type would be sufficient. This is one of the simplest ways to get started.

因此,对于 Kumulos,您可以执行以下操作:

So with Kumulos, you could do something like the following:

package com.example.cgwyllie.simplelistview;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.kumulos.android.jsonclient.Kumulos;
import com.kumulos.android.jsonclient.ResponseHandler;

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

public class MainActivity extends ListActivity {

    static class Person {

        public long personID;
        public String firstName;
        public String lastName;

        public static Person createFromGenericMap(Map<String, Object> object) {
            Person p = new Person();

            p.personID = (long) object.get("personID");
            p.firstName = (String) object.get("firstName");
            p.lastName = (String) object.get("lastName");

            return p;
        }

    }

    static class PersonAdapter extends BaseAdapter {

        private List<Person> people;

        public PersonAdapter(List<Person> people) {
            this.people = people;
        }

        @Override
        public int getCount() {
            return people.size();
        }

        @Override
        public Object getItem(int position) {
            return people.get(position);
        }

        @Override
        public long getItemId(int position) {
            Person p = people.get(position);
            return p.personID;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO implement your view
            return null;
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Kumulos.initWithAPIKeyAndSecretKey("API_KEY", "SECRET_KEY", this);

        // Call Kumulos
        Map<String,String> params = new HashMap<>();
        Kumulos.call("getPeople", params, new ResponseHandler() {

            // Handle result
            @Override
            public void didCompleteWithResult(Object result) {
                super.didCompleteWithResult(result);

                // Cast generic response down to list of maps
                ArrayList<LinkedHashMap<String, Object>> objects = (ArrayList<LinkedHashMap<String,Object>>) result;

                // Create a list for the models
                ArrayList<Person> people = new ArrayList<>();

                // Map models from generic objects and add to list
                for (Map<String,Object> personObject : objects) {
                    Person p = Person.createFromGenericMap(personObject);
                    people.add(p);
                }

                // Create adapter with model list
                final PersonAdapter adapter = new PersonAdapter(people);

                // Set adapter on main UI thread
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setListAdapter(adapter);
                    }
                });
            }
        });
    }
}

如前所述,虽然这是最简单的方法之一,但在加载发生时处理方向更改或导航离开活动确实存在一些限制.也未涵盖使用回收实现视图.

As mentioned, whilst this is one of the simplest approaches, it does have some limitations around the handling of orientation changes or navigation away from the activity whilst loading is taking place. Implementing the view with recycling is also not covered.

有关更全面的列表视图示例,可能值得查看 http:///developer.android.com/training/material/lists-cards.htmlhttp://www.vogella.com/tutorials/AndroidListView/article.html.

For more comprehensive list view examples, it might be worth checking out http://developer.android.com/training/material/lists-cards.html and http://www.vogella.com/tutorials/AndroidListView/article.html.

希望这有用.

这篇关于用 Kumulos 填充 ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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