我有一个表(sql table 2008),在那个表中我有两列(名称和日期),我的注意力是获取数据并将这些数据显示到listview中。我早期提到有两个独立的列,之后做一些研究我了解到我需要将我的列表视图表自定义为两个不同的列,其中的显示数据是我的自定义列表视图文件
public class testlistviewAdapter extends ArrayAdapter {
public testlistviewAdapter(Context context, String[] foods) {
super(context, R.layout.testlistview,foods);
}
@Override
public View getView(int position,View convertView,ViewGroup parent ){
LayoutInflater joyinflater=LayoutInflater.from(getContext());
View customView=joyinflater.inflate(R.layout.listview_adapter_file,parent,false);
String singleFoodItem=getItem(position);
TextView firstrow=(TextView)customView.findViewById(R.id.tx);
TextView secondrow=(TextView)customView.findViewById(R.id.datetime);
firstrow.setText(singleFoodItem);
secondrow.setText(singleFoodItem);
return customView;
}
Mainactivity文件的代码如下
ArrayList<String> data=new ArrayList<String>();
while(rs.next()){
String id=rs.getString("tm");
data.add(id);
}
String[] foods=data.toArray(new String[0]);
ArrayAdapter joyAdapter=new testlistviewAdapter(this,foods);
myOne.setAdapter(joyAdapter);
我做错了什么?如果我想为两列创建标题,我必须做出什么改变?请指教。
答案 0 :(得分:0)
您必须使用您的数据添加自定义适配器(以下示例中的项目对象)。
ListView yourListView = (ListView) findViewById(R.id.itemListView);
// data from the table
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, List<yourItem>);
yourListView .setAdapter(customAdapter);
您的自定义适配器:
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
Item p = getItem(position);
if (p != null) {
TextView firstrow=(TextView)customView.findViewById(R.id.tx);
TextView secondrow (TextView)customView.findViewById(R.id.datetime);
firstrow.setText(singleFoodItem);
secondrow.setText(singleFoodItem);
}
return v;
}}