如何在自定义适配器中设置来自 2 个不同 table 的文本元素?
How set text elements from 2 differents table in custom adapter?
我的 class 带有自定义适配器
它正在处理日期和标题。
日期和标题与 table 页面和文本标签与 Table 标签
public class MyAdapter extends ArrayAdapter <PageTag> {
Context context;
int ressource;
ArrayList<Page> data ;
TextView tvTitreList;
TextView tvDateList;
TextView tvTagList;
Page page;
public MyAdapter(Context context, ArrayList <PageTag> data ) {
super(context, 0, data);
}
我的 getView 方法。
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_list_perso, parent, false);
}
tvTitreList = (TextView) convertView.findViewById(R.id.tvTitre);
tvDateList = (TextView) convertView.findViewById(R.id.tvDate);
tvTagList = (TextView) convertView.findViewById(R.id.tvTag);
tvTitreList.setText(getItem(position).getPage().titrePage);
tvDateList.setText(getItem(position).getPage().datePage.toString());
tvTagList.setText(getItem(position).getTag().texteTag);
return convertView;
}
}
我的table PageTag
public class PageTag {
Page page ;
Tag tag ;
public PageTag ( Page page, Tag tag){
this.page = page;
this.tag = tag;
}
// with getter and setter
我的 ListPageActivity
pageMgr = new PageMgr(this);
lvList = (ListView) findViewById(R.id.listPage);
ArrayList<Page> listPage = pageMgr.getAllPageArray();
ArrayList<Tag> listTag = pageMgr.getAllTagArray();
ArrayList<PageTag> pageTag = new ArrayList<>();
for(int i = 0 ;i < listPage.size() ;i++){
Page page = listPage.get(i);
Tag tag = listTag.get(i);
(cannot resolve method) ==> pageTag.setPage(page);
==> pageTag.setTag(tag);
}
MyAdapter myAdapt = new MyAdapter(this,pageTag);
lvList.setAdapter(myAdapt);
getAllpageArray 方法
public ArrayList<Page> getAllPageArray(){
ArrayList <Page> listPageArray = new ArrayList <Page>();
String rqPage = " SELECT * FROM " + BaseSQLITE.TABLE_PAGE
+ " JOIN " + BaseSQLITE.TABLE_LIENS_TAG_PAGE
+ " ON " + BaseSQLITE.TABLE_PAGE+ "." + BaseSQLITE.COL_ID_PAGE + " = " + BaseSQLITE.TABLE_LIENS_TAG_PAGE+ "." + BaseSQLITE.COL_ID_PAGE_LTP
+ " JOIN " + BaseSQLITE.TABLE_TAG
+ " ON "+ BaseSQLITE.TABLE_TAG+ "." + BaseSQLITE.COL_ID_TAG + " = " + BaseSQLITE.TABLE_LIENS_TAG_PAGE+ "." + BaseSQLITE.COL_ID_TAG_LTP ;
Cursor cPage;
this.open();
cPage = bdd.rawQuery(rqPage,null);
Date date;
long l;
String titre;
while (cPage.moveToNext()){
String titrePage ;
Date datePage ;
l = cPage.getLong(2);
titrePage = cPage.getString(1);
datePage = new Date(l);
listPageArray.add(new Page(titrePage,datePage));
}
this.close();
return listPageArray;
}
getAllTagArray 也是如此
创建新模型class
class Result{
Page page;
Tag tag;
//getters and setters
}
现在循环你的结果并将其添加到 Result
的列表中
ArrayList<Result> result = new ArrayList<>();
for(int i=0 ;i < listPage.size() ;i++){
Page page = listPage.get(i);
Tag tag = tagList.get(i);
result.setPage(page);
result.setTag(tag);
}
在您的适配器中将 Page
更改为 Result
tvTitreList.setText(getItem(position).getPage().getTitrePage());
tvDateList.setText(getItem(position).getPage().getDatePage().toString());
tvTagList.setText(getItem(position).getTag().getTextTag());
终于可以用你的方法了我已经更改了一些代码。
谢谢你@rajan ks
pageMgr = new PageMgr(this);
lvList = (ListView) findViewById(R.id.listPage);
ArrayList<Page> listPage = pageMgr.getAllPageArray();
ArrayList<Tag> listTag = pageMgr.getAllTagArray();
ArrayList<PageTag> pageTag = new ArrayList<>();
for(int i = 0 ;i < listPage.size() ;i++) {
Page page = listPage.get(i);
Tag tag = listTag.get(i);
PageTag pageT = new PageTag(page, tag);
pageTag.add(pageT);
}
MyAdapter myAdapt = new MyAdapter(this,pageTag);
lvList.setAdapter(myAdapt);
我的 class 带有自定义适配器 它正在处理日期和标题。 日期和标题与 table 页面和文本标签与 Table 标签
public class MyAdapter extends ArrayAdapter <PageTag> {
Context context;
int ressource;
ArrayList<Page> data ;
TextView tvTitreList;
TextView tvDateList;
TextView tvTagList;
Page page;
public MyAdapter(Context context, ArrayList <PageTag> data ) {
super(context, 0, data);
}
我的 getView 方法。
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_list_perso, parent, false);
}
tvTitreList = (TextView) convertView.findViewById(R.id.tvTitre);
tvDateList = (TextView) convertView.findViewById(R.id.tvDate);
tvTagList = (TextView) convertView.findViewById(R.id.tvTag);
tvTitreList.setText(getItem(position).getPage().titrePage);
tvDateList.setText(getItem(position).getPage().datePage.toString());
tvTagList.setText(getItem(position).getTag().texteTag);
return convertView;
}
}
我的table PageTag
public class PageTag {
Page page ;
Tag tag ;
public PageTag ( Page page, Tag tag){
this.page = page;
this.tag = tag;
}
// with getter and setter
我的 ListPageActivity
pageMgr = new PageMgr(this);
lvList = (ListView) findViewById(R.id.listPage);
ArrayList<Page> listPage = pageMgr.getAllPageArray();
ArrayList<Tag> listTag = pageMgr.getAllTagArray();
ArrayList<PageTag> pageTag = new ArrayList<>();
for(int i = 0 ;i < listPage.size() ;i++){
Page page = listPage.get(i);
Tag tag = listTag.get(i);
(cannot resolve method) ==> pageTag.setPage(page);
==> pageTag.setTag(tag);
}
MyAdapter myAdapt = new MyAdapter(this,pageTag);
lvList.setAdapter(myAdapt);
getAllpageArray 方法
public ArrayList<Page> getAllPageArray(){
ArrayList <Page> listPageArray = new ArrayList <Page>();
String rqPage = " SELECT * FROM " + BaseSQLITE.TABLE_PAGE
+ " JOIN " + BaseSQLITE.TABLE_LIENS_TAG_PAGE
+ " ON " + BaseSQLITE.TABLE_PAGE+ "." + BaseSQLITE.COL_ID_PAGE + " = " + BaseSQLITE.TABLE_LIENS_TAG_PAGE+ "." + BaseSQLITE.COL_ID_PAGE_LTP
+ " JOIN " + BaseSQLITE.TABLE_TAG
+ " ON "+ BaseSQLITE.TABLE_TAG+ "." + BaseSQLITE.COL_ID_TAG + " = " + BaseSQLITE.TABLE_LIENS_TAG_PAGE+ "." + BaseSQLITE.COL_ID_TAG_LTP ;
Cursor cPage;
this.open();
cPage = bdd.rawQuery(rqPage,null);
Date date;
long l;
String titre;
while (cPage.moveToNext()){
String titrePage ;
Date datePage ;
l = cPage.getLong(2);
titrePage = cPage.getString(1);
datePage = new Date(l);
listPageArray.add(new Page(titrePage,datePage));
}
this.close();
return listPageArray;
}
getAllTagArray 也是如此
创建新模型class
class Result{
Page page;
Tag tag;
//getters and setters
}
现在循环你的结果并将其添加到 Result
ArrayList<Result> result = new ArrayList<>();
for(int i=0 ;i < listPage.size() ;i++){
Page page = listPage.get(i);
Tag tag = tagList.get(i);
result.setPage(page);
result.setTag(tag);
}
在您的适配器中将 Page
更改为 Result
tvTitreList.setText(getItem(position).getPage().getTitrePage());
tvDateList.setText(getItem(position).getPage().getDatePage().toString());
tvTagList.setText(getItem(position).getTag().getTextTag());
终于可以用你的方法了我已经更改了一些代码。 谢谢你@rajan ks
pageMgr = new PageMgr(this);
lvList = (ListView) findViewById(R.id.listPage);
ArrayList<Page> listPage = pageMgr.getAllPageArray();
ArrayList<Tag> listTag = pageMgr.getAllTagArray();
ArrayList<PageTag> pageTag = new ArrayList<>();
for(int i = 0 ;i < listPage.size() ;i++) {
Page page = listPage.get(i);
Tag tag = listTag.get(i);
PageTag pageT = new PageTag(page, tag);
pageTag.add(pageT);
}
MyAdapter myAdapt = new MyAdapter(this,pageTag);
lvList.setAdapter(myAdapt);