我的作业需要一些帮助.. 我的应用程序使用google map api v2并将位置和一些注释存储到realm中。我想使用来自领域的数据制作listview。
LocationData.java
import io.realm.RealmObject;
public class LocationData extends RealmObject {
private double latitude;
private double longitude;
private String title;
private String detailInfo;
public String getUriString() {
return uriString;
}
private String uriString;
public String getDetailInfo() {
return detailInfo;
}
public void setDetailInfo(String detailInfo) {
this.detailInfo = detailInfo;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setUriString(String uriString){ this.uriString = uriString; }
}
ListViewActivity.java *这只是从示例代码中复制的
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ListViewActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
ListView list = (ListView)findViewById(R.id.listView1);
ArrayList<String> labelList = new ArrayList<String>();
for(int i=1; i<=20; i++){
labelList.add("List Item "+i);
}
CustomAdapter mAdapter = new CustomAdapter(this, 0, labelList);
list.setAdapter(mAdapter);
list.setDivider(null);
}
}
CustomAdapter.java *这也是复制的示例代码
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ListViewActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
ListView list = (ListView)findViewById(R.id.listView1);
ArrayList<String> labelList = new ArrayList<String>();
for(int i=1; i<=20; i++){
labelList.add("List Item "+i);
}
CustomAdapter mAdapter = new CustomAdapter(this, 0, labelList);
list.setAdapter(mAdapter);
list.setDivider(null);
}
}
我想知道如何从域中加载数据并为listview设置数据。
答案 0 :(得分:0)
你发布了ListViewActvity.java的代码两次,而不是发布CustomeAdapter.java,所以我要去GUESS它扩展了ListAdapter对象....
我将为您提供您需要做的简短版本以及一个非常简单的数组适配器的示例代码。
以下是使用简单股票报价应用的文本视图的arrayadapter示例。
class StockArrayAdapter extends ArrayAdapter {
Context myContext;
ArrayList<Stock> stocks;
int myLayout;
LayoutInflater inflator;
public StockArrayAdapter(Context context, int resource) {
super(context, resource);
ArrayList<Stock> stocks = new ArrayList<Stock>();
initialize(context, resource, stocks);
}
public StockArrayAdapter(Context context, int resource, ArrayList<Stock> stocks) {
super(context, resource, stocks);
initialize(context, resource, stocks);
}
private void initialize(Context context, int resource, ArrayList<Stock> objects) {
myContext = context;
myLayout = resource;
stocks = objects;
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = inflator.inflate(myLayout, parent, false);
TextView nameTextView = (TextView) rowView.findViewById (R.id.nameTextView);
TextView lastTextView = (TextView) rowView.findViewById(R.id.lastTextView);
TextView changeTextView = (TextView) rowView.findViewById(R.id.changeTextView);
nameTextView.setText(stocks.get(position).CompanyName);
lastTextView.setText(stocks.get(position).LastTrade);
changeTextView.setText((stocks.get(position).Change));
return rowView;
}
}