Android更改基本适配器中的文本颜色

时间:2015-01-08 11:05:36

标签: android listview baseadapter

我有列表视图,在基本适配器上设置如下:

                FQAdapter fa=new FQAdapter(FQActivity2.this, al);
                lvfq.setAdapter(fa);
                lvfq.setDivider(null);

在我的基本适配器中,inflater xml文件有两个字段,文本和图像:我希望当我点击图像时,无论文本是什么,文本都会改变它们的颜色如下:

       @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;             //trying to reuse a recycled view
        ViewHolder holder = null;
        if (vi == null) 
        { 
            holder=new ViewHolder();
            LayoutInflater lif=( LayoutInflater ) ctx.getSystemService ( ctx.LAYOUT_INFLATER_SERVICE ) ;
            vi=lif.inflate(R.layout.item_fq, null);
            holder.tvMsg=(TextView) vi.findViewById(R.id.tv_item_username);
            holder.ivMinuse=(ImageView) vi.findViewById(R.id.iv_item_un_minuse);
            vi.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)vi.getTag();
        }
        final FAQ jou=al.get(position);

        holder.tvMsg.setText(jou.getFqTitle());
        holder.ivMinuse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                // here i want to set text color whatever image click
                // like holder.setivMsg.setTextColor(Color.BLUE); but it effect on only last text of list
                new GetAns().execute("url"+jou.getFqId());
                //Log.v("dssfs", jou.getFqAns());
            }
        });

        //date.roll(Calendar.DAY_OF_YEAR, true);

        return vi ;
    }

当我写代码注释时,我想在点击holder时更改文本颜色holder.tvMsg特定位置.ivMinuse image;

请帮我这样做

编辑: 我的整个java文件:

     public class FQActivity extends Activity{
ImageView ivChcklistUnsel;
ImageView ivJourUnsel;
ImageView ivResourUnsel;
ImageView ivRecipUnsel;
ImageView ivSettingUnsel;
ImageView ivFaqUnsel;

ImageView ivChcklistsel;
ImageView ivJoursel;
ImageView ivResoursel;
ImageView ivRecipsel;
ImageView ivSettingsel;
ImageView ivFaqsel;

ListView lvfq;
ArrayList<FAQ> al;
TextView tvAns;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fq_layout);
    initImageview();
    tvAns=(TextView) findViewById(R.id.tv_ans);
    tvAns.setMovementMethod(new ScrollingMovementMethod());
    lvfq=(ListView) findViewById(R.id.lv_fqlistview);
    new GetJournal().execute(Appconstants.SERVICE_URL1+"faq/getFaqData");
}
public class GetJournal extends AsyncTask<String, Void, String>
{
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd=new ProgressDialog(FQActivity.this);
        pd.setMessage(Html.fromHtml("<b>Please Wait</b><br/>loading..."));
        pd.setIndeterminate(false);
        pd.setCancelable(false);
        pd.show();
    }
    @Override
    protected String doInBackground(String... params) {
        return new DownloadAdapter(FQActivity.this).getJSONData(params[0]);
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        pd.cancel();
        try 
        {
            JSONObject obj=new JSONObject(result);
            Log.v("dd", result);
            if(obj.getString("status").equals("success"))
            {
                JSONArray arr=obj.getJSONArray("response");
                al=new ArrayList<FAQ>();
                FAQ fq;
                for(int i=0;i<arr.length();i++)
                {
                    obj=arr.getJSONObject(i);
                    fq=new FAQ();
                    Log.v("dd", obj.getString("faq_title"));
                    fq.setFqId(obj.getString("faq_id"));
                    fq.setFqTitle(obj.getString("faq_title"));
                    //fq.setFqQue(obj.getString("faq_qus"));
                    //fq.setFqAns(obj.getString("faq_ans"));
                    al.add(fq);
                }

                FQAdapter fa=new FQAdapter(FQActivity.this, al);
                lvfq.setAdapter(fa);
                lvfq.setDivider(null);
            }
            else
            {
                Toast.makeText(getApplicationContext(), obj.getString("response"), Toast.LENGTH_SHORT).show();
            }
        } 
        catch (JSONException e) 
        {
            e.printStackTrace();
        }
    }
}
public class GetAns extends AsyncTask<String, Void, String>
{
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd=new ProgressDialog(FQActivity.this);
        pd.setMessage(Html.fromHtml("<b>Please Wait</b><br/>loading..."));
        pd.setIndeterminate(false);
        pd.setCancelable(false);
        pd.show();
    }
    @Override
    protected String doInBackground(String... params) {
        return new DownloadAdapter(FQActivity.this).getJSONData(params[0]);
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        pd.cancel();
        try 
        {
            JSONObject obj=new JSONObject(result);
            Log.v("dd", result);
            if(obj.getString("status").equals("success"))
            {
                JSONArray arr=obj.getJSONArray("response");

                    obj=arr.getJSONObject(0);
                    tvAns.setText(obj.getString("faq_ans"));

            }
            else
            {
                Toast.makeText(getApplicationContext(), obj.getString("response"), Toast.LENGTH_SHORT).show();
            }
        } 
        catch (JSONException e) 
        {
            e.printStackTrace();
        }
    }
}
private void initImageview()
{
    ivChcklistsel= (ImageView) findViewById(R.id.iv_checklist_sel);
    ivChcklistsel.setVisibility(View.GONE);
    ivJoursel= (ImageView) findViewById(R.id.iv_jour_sel);
    ivResoursel= (ImageView) findViewById(R.id.iv_resour_sel);
    ivRecipsel= (ImageView) findViewById(R.id.iv_recipes_sel);
    ivFaqsel= (ImageView) findViewById(R.id.iv_faq_sel);
    ivFaqsel.setVisibility(View.VISIBLE);
    ivSettingsel= (ImageView) findViewById(R.id.iv_setting_sel);

    ivChcklistUnsel= (ImageView) findViewById(R.id.iv_checklist_unsel);
    ivChcklistUnsel.setVisibility(View.VISIBLE);
    ivJourUnsel= (ImageView) findViewById(R.id.iv_jour_unsel);
    ivResourUnsel= (ImageView) findViewById(R.id.iv_resour_unsel);
    ivRecipUnsel= (ImageView) findViewById(R.id.iv_recipes_unsel);
    ivFaqUnsel= (ImageView) findViewById(R.id.iv_faq_unsel);
    ivFaqUnsel.setVisibility(View.GONE);
    ivSettingUnsel= (ImageView) findViewById(R.id.iv_setting_unsel);

    ivChcklistUnsel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //startActivity(new Intent(getApplicationContext(), DailyCheckListActivity.class));
            finish();
        }
    });
    ivResourUnsel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), ResourceActivity.class));
            finish();
        }
    });
    ivRecipUnsel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), RecipesActivity.class));
            finish();
        }
    });
    ivJourUnsel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), DailyJournalActivity.class));
            finish();
        }
    });
    ivSettingUnsel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), AppSettingActivity.class));
            finish();
        }
    });
}

public class FQAdapter extends BaseAdapter{

    Context ctx;
    ArrayList<FAQ> al;

    int i1=0;
    public FQAdapter(Context cxt,ArrayList<FAQ> al)
    {
        this.ctx=cxt;
        this.al=al;  
     }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return al.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;             //trying to reuse a recycled view
        ViewHolder holder = null;
        if (vi == null) 
        { 
            holder=new ViewHolder();
            LayoutInflater lif=( LayoutInflater ) ctx.getSystemService ( ctx.LAYOUT_INFLATER_SERVICE ) ;
            vi=lif.inflate(R.layout.item_fq, null);
            holder.tvMsg=(TextView) vi.findViewById(R.id.tv_item_username);
            holder.ivMinuse=(ImageView) vi.findViewById(R.id.iv_item_un_minuse);
            vi.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)vi.getTag();
        }
        final FAQ jou=al.get(position);

        holder.tvMsg.setText(jou.getFqTitle());
        holder.ivMinuse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                // i try here all your answeres code but not working
                new GetAns().execute("url"+jou.getFqId());
                //Log.v("dssfs", jou.getFqAns());
            }
        });

        //date.roll(Calendar.DAY_OF_YEAR, true);

        return vi ;
    }
    class ViewHolder{
        TextView tvMsg;
        ImageView ivMinuse;
    }
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    //super.onBackPressed();
}

}

holder.tvMsg给出了创建final的错误,所以我也尝试使用全局声明。 没有什么可以帮助我改变颜色..所有工作都在最后一个位置文本ot图像。

3 个答案:

答案 0 :(得分:0)

    if (convertView == null) {
        holder= new ViewHolder();
        convertView = inflater.inflate(R.layout.your_row_xml, null);
     holder.ivMinuse=  convertView.findViewbyId(R.id.YourImageId);
      holder.tvMsg=  convertView.findViewbyId(R.id.YourTextId);
     convertView.setTag(ViewHolder);
      } else {

     holder= (ViewHolder) convertView.getTag();
     }
       holder.ivMinuse.settag(position);
        holder.ivMinuse.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

                if(Integer.ParseInt(v.getTag.toString)==position)
                {
                   holder.tvMsg.setTextColor(YOUR_COLOR_HERE);
                }

            new GetAns().execute("url"+jou.getFqId());
            //Log.v("dssfs", jou.getFqAns());
        }
    });

但最好在获取视图之外编写点击侦听器

          class  ViewHolder {
                TextView  tvMsg;
                 ImageView ivMinuse;
             }

答案 1 :(得分:0)

您可以通过两种方式实现这一目标:

1. 跟踪点击的项目

Integer clickPosition =-1;

 public View getView(final int position, View convertView, ViewGroup parent) {
        if( clickPosition ==  position)
        {
            holder.setivMsg.setTextColor(Color.BLUE);
        }
        else{
            // default color
            holder.setivMsg.setTextColor(Color.BLACK);
        }

        holder.ivMinuse.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            // set the position 
            clickPosition = position;

            new GetAns().execute("url"+jou.getFqId());
            //Log.v("dssfs", jou.getFqAns());
        }
    });
  }

2. 在TextView中将位置设置为标记

 public View getView(final int position, View convertView, ViewGroup parent) {

       // Set Tag
       holder.setivMsg.setTag(position);

       holder.ivMinuse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

               TextView txtListItem =(TextView) v.findViewById(R.id.yourTextViewID);
               // get Tag
               if (position == (Integer)txtListItem.getTag())
                {
                    holder.setivMsg.setTextColor(Color.BLUE);
                }
                else{
                    // default color
                    holder.setivMsg.setTextColor(Color.BLACK);
                }
             }
        });
  }

希望它有助于ツ

答案 2 :(得分:0)

更改此代码,

 private int mSelectedPos = -1;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;             //trying to reuse a recycled view
        ViewHolder holder = null;
        if (vi == null) 
        { 
            holder=new ViewHolder();
            LayoutInflater lif=( LayoutInflater ) ctx.getSystemService ( ctx.LAYOUT_INFLATER_SERVICE ) ;
            vi=lif.inflate(R.layout.item_fq, null);
            holder.tvMsg=(TextView) vi.findViewById(R.id.tv_item_username);
            holder.ivMinuse=(ImageView) vi.findViewById(R.id.iv_item_un_minuse);
            vi.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)vi.getTag();
        }
        final FAQ jou=al.get(position);
        if(mSelectedPos == position){
          holder.tvMsg.setColor(Color.RED);
        }else{
          holder.tvMsg.setColor(yourDefaultColor);
         }
        holder.ivMinuse.setTag(position);
        holder.tvMsg.setText(jou.getFqTitle());
        holder.ivMinuse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                mSelectedPos = (Integer) v.getTag();
                new GetAns().execute("url"+jou.getFqId());
                notifyDataSetChanged();
            }
        });

        //date.roll(Calendar.DAY_OF_YEAR, true);

        return vi ;
    }