我创建了一个名为Listing的类:
public class Listing {
@Key private int id;
@Key private String name;
@Key private float lastname;
public String getName() {
return this.name;
}
public void setName( String name ) {
this.name = name;
}
public String getLastName() {
return this.lastname;
}
public void setLastName( String lastname ) {
this.lastname = lastname;
}
}
我可以创建一个列表数组,我从服务器提取并将其存储在变量中。
然后我基本上可以调用variable[1].getLastName
并获得我需要的所有值。
然而,我对将它放在listview中完全感到困惑。有人能指出我正确的方向吗?我发现此页面非常有用http://www.vogella.com/articles/AndroidListView/article.html,但我不确定如何将其与我已有的相关联?
感谢任何和所有帮助!
目前我的设置方式如下。这是我的适配器,主要来自教程:
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
public boolean hasStableIds() {
return true;
}
String[] values = new String[] {
"1", "2"
};
//the stuff I've commented out in this next section give me nullpointer exception, where listinginfo is the varible I have saved my call to.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// thisListing = listinginfo[position];
View rowView = inflater.inflate(R.layout.main_cell, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.firstline);
TextView smallerTextView = (TextView) rowView.findViewById(R.id.secondLine);
//String currentadd = listinginfo[position].getName();
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
//textView.setText(currentadd);
//smallerTextView.setText(listinginfo[position].getLastName);
return rowView;
}
}
我可以在请求成功的同一方法中调用listinginfo [position] .getLastName。
我也可以在onCreate方法中创建listview,但这只是在我之前生成假数据时:
//generate fake data for listview
final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] {
"jon", "jim", "mary"
};
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0;i < values.length;++i){
list.add(values[i]);
}
答案 0 :(得分:0)
在您提到的教程中,请参阅第3.2节。而不是
String[] values;
在您的适配器中,您将拥有类似的内容
Listing[] listings;
然后,在适配器的getView()方法中,您将在TextViews中填入适当的值。类似的东西:
textView.setText(listings[position].getLastName());
试试这个:
public class ListingsAdapter extends BaseAdapter
{
List<Listing> listings;
Context context;
LayoutInflater inflater;
public ListingsAdapter(Context context, List<Listing> listings)
{
this.context = context;
this.listings = listings;
inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = inflater.inflate(R.layout.main_cell, parent, false);
}
TextView text1 = (TextView) convertView.findViewById(R.id.firstline);
TextView text2 = (TextView) convertView.findViewById(R.id.secondLine);;
Listing listing = listings.get(position);
text1.setText(listing.getName());
text2.setText(listing.getLastName());
return convertView;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return listings.size();
}
@Override
public Object getItem(int arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0)
{
// TODO Auto-generated method stub
return 0;
}
}
在您的活动中执行此操作:
final ListView listview = (ListView) findViewById(R.id.listview);
ListingsAdapter listingsAdapter = new ListingsAdapter(this, myListings);
listview.setAdapter(listingsAdapter);