如何在自定义适配器中的2个不同表中设置文本元素?

时间:2016-06-13 11:04:13

标签: android arraylist android-arrayadapter adapter settext

我的班级有自定义适配器 它使用日期和标题。 日期和标题与表格页面和textTag与表格标签

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;
    }
}

我的表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的相同内容

2 个答案:

答案 0 :(得分:0)

创建一个新的Model类

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());

答案 1 :(得分:0)

最后你的方法我已经改变了一些代码。 谢谢你@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);