0

我想将自定义 ArrayAdapter 中的 HashMap 或 ArrayList 对象传递给填充 ListView 的活动。我想发送检查的记录 recordId 字段。我是 Android 的新手。任何建议 Plz

4

2 回答 2

2

向适配器传递对活动的引用。然后在activity中提供一个adapter可以调用的方法。

在适配器中:

private MyActivity activity;

public void setActivity(MyActivity activity) {
    this.activity = activity;
}

在活动中:

// create adapter
adapter.setActvity(this);

在适配器中(当您想将参数传递回活动时):

activity.setHashMap(mymap);
于 2012-07-17T09:21:55.060 回答
0

我的 CustomAdapter 类::
类 SearchResultAdapter 扩展 ArrayAdapter{

    Context context;
    int layoutResourceId;   
    private ArrayList<SearchResultModel> results;

    public SearchResultAdapter(Context context, int layoutResourceId, ArrayList<SearchResultModel> results) {
        super(context, layoutResourceId, results);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.results = results;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View row = convertView;
        SearchResultHolder holder=null;
        if(row == null)
        {
            LayoutInflater vi = ((Activity)context).getLayoutInflater();
            row = vi.inflate(R.layout.listitemrow, null);
            holder = new SearchResultHolder();
            holder.jobdetails = (TextView)row.findViewById(R.id.jobdetails);
            row.setTag(holder);
        }
        else
        {
            holder = (SearchResultHolder)row.getTag();
        }
        SearchResultModel model = results.get(position);

        if (model != null)
        {
          row.setId(Integer.parseInt(model.getjob_id()));   
          String result=
                  "<font color=#5D2E8C>"+model.getjob_title()+"</font><br>" +
                  "<font color=#737373>"+model.getpost_date()+"</font><br>"+model.getuser_name()+"<br>" +
                  "<font color=#3C5A98>"+model.getsalary()+" Rs (Per Annum)</font><br>"+
                  "<font color=#97AB40>"+model.getjob_location()+"," +
                  " "+model.getjob_exp_min()+"-"+model.getjob_exp_max()+" year";
          holder.jobdetails.setText(Html.fromHtml(result));

        }
        return row;
    }

    class SearchResultHolder
    {
        TextView jobdetails;
    }
}  

我在 Main 方法中的 ListView 将使用上述 ArrayAdapter。

SearchResultAdapter adapter;
ListView lv;  
adapter=new SearchResultAdapter(this, R.layout.listitemrow, model);  
//Here model is the ArrayList<SearchResultModel> 
lv=getListView();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() 
{
  public void onItemClick(AdapterView<?> arg0, View v,int arg2, long arg3) 
    {  
    }
}  

我的 SearchResultModel 类

package com.cws.model;

public class SearchResultModel {

    private String job_id,userdetail_id,user_name,subuser_id,
                        job_title,search_keyword,job_skill,
                        job_exp_min,job_exp_max,
                        job_description,salary,
                        job_location,education,
                        industry_id,role_id,functional_id,job_type,
                        desire_candiadate_profile,company_profile,
                        website,executive_name,address,email,
                        phone,post_date,is_active;
    private String count,counter;//----count=total no of row found
                                                //---counter=now displaying row nos.

    private String errorflag;

    public void seterrorflag(String errorflag)
    {
        this.errorflag=errorflag;
    }
    public String geterrorflag()
    {
        return this.errorflag;
    }


    public void setcount(String count)
    {
        this.count=count;
    }
    public String getcount()
    {
        return this.count;
    }
    public void setcounter(String counter)
    {
        this.counter=counter;
    }
    public String getcounter()
    {
        return this.counter;
    }
    public void setjob_id(String job_id)
    {
        this.job_id=job_id;
    }
    public String getjob_id()
    {
        return this.job_id;
    }
    public void setuserdetail_id(String userdetail_id){
        this.userdetail_id=userdetail_id;
    }
    public String getuserdetail_id()
    {
        return this.userdetail_id;
    }

    public void setuser_name(String user_name){
        this.user_name=user_name;
    }
    public String getuser_name()
    {
        return this.user_name;
    }

    public void setsubuser_id(String subuser_id){
        this.subuser_id=subuser_id;
    }
    public String getsubuser_id()
    {
        return this.subuser_id;
    }
    public void setjob_title(String job_title){
        this.job_title=job_title;
    }
    public String getjob_title()
    {
        return this.job_title;
    }
    public void setsearch_keyword(String search_keyword){
        this.search_keyword=search_keyword;
    }
    public String getsearch_keyword()
    {
        return this.search_keyword;
    }
    public void setjob_skill(String job_skill){
        this.job_skill=job_skill;
    }
    public String getjob_skill()
    {
        return this.job_skill;
    }
    public void setjob_exp_min(String job_exp_min){
        this.job_exp_min=job_exp_min;
    }
    public String getjob_exp_min()
    {
        return this.job_exp_min;
    }
    public void setjob_exp_max(String job_exp_max){
        this.job_exp_max=job_exp_max;
    }
    public String getjob_exp_max()
    {
        return this.job_exp_max;
    }
    public void setjob_description(String job_description){
        this.job_description=job_description;
    }
    public String getjob_description()
    {
        return this.job_description;
    }
    public void setsalary(String salary){
        this.salary=salary;
    }
    public String getsalary()
    {
        return this.salary;
    }
    public void setjob_location(String job_location){
        this.job_location=job_location;
    }
    public String getjob_location()
    {
        return this.job_location;
    }
    public void seteducation(String education){
        this.education=education;
    }
    public String geteducation()
    {
        return this.education;
    }
    public void setindustry_id(String industry_id){
        this.industry_id=industry_id;
    }
    public String getindustry_id()
    {
        return this.industry_id;
    }
    public void setrole_id(String role_id){
        this.role_id=role_id;
    }
    public String getrole_id()
    {
        return this.role_id;
    }
    public void setfunctional_id(String functional_id){
        this.functional_id=functional_id;
    }
    public String getfunctional_id()
    {
        return this.functional_id;
    }
    public void setjob_type(String job_type){
        this.job_type=job_type;
    }
    public String getjob_type()
    {
        return this.job_type;
    }
    public void setdesire_candiadate_profile(String desire_candiadate_profile){
        this.desire_candiadate_profile=desire_candiadate_profile;
    }
    public String getdesire_candiadate_profile()
    {
        return this.desire_candiadate_profile;
    }
    public void setcompany_profile(String company_profile){
        this.company_profile=company_profile;
    }
    public String getcompany_profile()
    {
        return this.company_profile;
    }
    public void setwebsite(String website){
        this.website=website;
    }
    public String getwebsite()
    {
        return this.website;
    }
    public void setexecutive_name(String executive_name){
        this.executive_name=executive_name;
    }
    public String getexecutive_name()
    {
        return this.executive_name;
    }
    public void setaddress(String address){
        this.address=address;
    }
    public String getaddress()
    {
        return this.address;
    }
    public void setemail(String email){
        this.email=email;
    }
    public String getemail()
    {
        return this.email;
    }
    public void setphone(String phone){
        this.phone=phone;
    }
    public String getphone()
    {
        return this.phone;
    }
    public void setpost_date(String post_date){
        this.post_date=post_date;
    }
    public String getpost_date()
    {
        return this.post_date;
    }
    public void setis_active(String is_active){
        this.is_active=is_active;
    }
    public String getis_active()
    {
        return this.is_active;
    }

}
于 2012-07-17T09:23:14.063 回答