使用Kumulos填充ListView时遇到了很多困难。通过我的研究,我发现了很多关于使用SQLite或其他数据库的教程和帖子,但没有Kumulos = /。
我需要帮助:
1)实施Kumulos以填充ListView
来源:https://docs.kumulos.com/integration/android/
进行的研究:
Encode and decode bitmap object in base64 string in Android
How to retrieve data from DBHelper by HashMap in Multicolumn 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();
}
});
}
}
CUSTOM ADAPTER:
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得到错误但是= /。
Kumulos错误:{&#34; responseCode&#34;:32,&#34; responseMessage&#34;:&#34;无效请求:&#34;,&#34;有效负载&#34;:null, &#34; sessionToken&#34;:&#34; b29366e44a7cdbb905db18b51995e545daf4f816&#34;&#34; requestedMethod&#34;:&#34; searchPerson&#34;&#34; requestedFormat&#34;:&#34; JSON& #34;&#34;时间戳&#34;:1462147387,&#34; requestReceivedTime&#34;:1462147387,&#34; maxAllowedRequestTime&#34; 40&#34; requestProcessingTime&#34;:0.012163877487183}
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));
答案 0 :(得分:0)
我是来自库穆洛斯的克里斯 - 让我试着指出你正确的方向。
因此,通常,在Web服务的列表视图中显示数据的过程如下所示:
呼叫Web服务并检索数据列表
将这些对象映射到模型类,并保留在内存中或保留到磁盘
创建由手机上的数据支持的适配器
将列表视图的适配器设置为自定义适配器实例
假设您将对象保留在内存中,创建由模型类型的ArrayList
支持的自定义适配器就足够了。这是最简单的入门方式之一。
因此,对于Kumulos,您可以执行以下操作:
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);
}
});
}
});
}
}
如前所述,虽然这是最简单的方法之一,但在加载方向变化或离开活动时,它确实存在一些限制。用回收来实现视图也没有涉及。
有关更全面的列表视图示例,可能需要查看http://developer.android.com/training/material/lists-cards.html和http://www.vogella.com/tutorials/AndroidListView/article.html。
希望这很有用。