作为android的新手我试图在我的项目上实现List视图。列表视图有效。但是我只能在列表中看到一行。我希望动态添加尽可能多的行。 我创建了一个单独的XML,它定义了列表视图中单行的结构。 这是我到目前为止编码的内容:
public class Offers extends Activity
{
ListView latesttrans;
ArrayList<String> listItems = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offers);
latesttrans = (ListView) findViewById(R.id.listView1);
latesttrans.setAdapter(new mylist2(this));
}
}
class SingleRow2
{
String title;
String description;
SingleRow2(String title, String description)
{
this.title=title;
this.description=description;
}
}
class mylist2 extends BaseAdapter
{
int i=0;
Boolean flagValue = false;
ArrayList<SingleRow> list;
Context context;
mylist2(Context c)
{
context=c;
list=new ArrayList<SingleRow>();
Resources res = c.getResources();
String[] title = res.getStringArray(R.array.titles);
String[] description = res.getStringArray(R.array.description);
list.add(new SingleRow(title[i],description[i]));
i++;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0)
{
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0)
{
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup viewGroup)
{
// TODO Auto-generated method stub
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row_offers,viewGroup,false);
Uri latesttransuri = Uri.parse("content://sms/inbox");
Cursor latesttranscursor = context.getContentResolver().query(latesttransuri, new String[]{"_id","address","date","body"}, null, null, null);
if(flagValue == false)
{
latesttranscursor.moveToFirst();
flagValue = true;
}
String address = null,body = null;
int i=0;
while(latesttranscursor.moveToNext())
{
address=latesttranscursor.getString(1);
long date = latesttranscursor.getLong(2);
String sdate = millisToDate(date);
body = latesttranscursor.getString(3);
if(body.contains("FREE"))
{
TextView title = (TextView) row.findViewById(R.id.textViewSingleOffer1);
TextView description = (TextView) row.findViewById(R.id.textViewSingleOffer2);
title.setText(address);
description.setText(body);
}
else if(body.contains("unlimited"))
{
TextView title = (TextView) row.findViewById(R.id.textViewSingleOffer1);
TextView description = (TextView) row.findViewById(R.id.textViewSingleOffer2);
title.setText(address);
description.setText(body);
}
}
return row;
}
public String millisToDate(long currentTime)
{
String finalDate;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentTime);
Date date = calendar.getTime();
finalDate = date.toString();
return finalDate;
}
}
答案 0 :(得分:0)
您不会迭代您的商品。在适配器的构造函数中,您只需增加一次,然后停止。这导致有一行。将它更改为for循环,如下所示:
for(int i = 0; i < var; i++) {
list.add(new SingleRow(title[i], description[i]));
}
其中var是您从资源中检索的数组的大小。
有关ListView和适配器使用的更多信息:http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter