在单击按钮时更改baseadapter getview中的文本颜色

时间:2018-06-23 16:11:07

标签: android

大家好 通过单击活动中的按钮,是否可以更改基本适配器夸大的行中的Textview的文本颜色。我需要在单击按钮时更改文本颜色。

MainActivity.class

Button button1=findViewById(R.id.button);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick ( View v ) {
            text.setTextColor(Color.GREEN);            }
    });

Button button2=findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick ( View v ) {
            text.setTextColor(Color.RED);            }
    });

Adapter.class

public class Adaptor extends BaseAdapter {
TextView tvtitle;
Context context;
private int textSize;



public NewsAdaptor ( Context context, ArrayList <Item> List ) {
    this.context = context;
    this.newsList = List;
}

ArrayList <Item> List;


@Override
public int getCount () {

    returnList.size();
}

@Override
public Object getItem ( int position ) {

    return List.get(position);
}

@Override
public long getItemId ( int position ) {
    return 0;
}

@SuppressLint("ResourceAsColor")
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
    if (convertView == null) {
        convertView = View.inflate(context, R.layout.newsitemlist_layout, null);
    }
    Item currentNews = List.get(position);
    tvtitle = (TextView) convertView.findViewById(R.id.textView1id);
    tvtitle.setText(currentNews.getTitle());
    return convertView;
}

newsitemlist_layout.xml

RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">

<TextView
    android:id="@+id/text"
    android:layout_width="237dp"
    android:layout_height="83dp"
    android:layout_alignEnd="@+id/pubDateid"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/pubDateid"
    android:ellipsize="end"
    android:gravity="right"
    android:text="News TITLE"
    android:textStyle="bold" />

2 个答案:

答案 0 :(得分:1)

在点击按钮并调用adpater.notifyDataSetChanged()时为对象分配颜色 并在适配器的getView()方法中设置该对象的textcolor。

答案 1 :(得分:1)

您需要在适配器中为每一行设置颜色,然后如果单击按钮会更改适配器中的颜色并调用notifyDataSetChanged()。它将更改后的颜色应用于所有行。

我修改了您的代码

Button button1=findViewById(R.id.button);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick ( View v ) {
            adapter.setTextColor(Color.GREEN)
            adapter.notifyDataSetChanged()            }
    });

Button button2=findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick ( View v ) {
            adapter.setTextColor(Color.RED)
            adapter.notifyDataSetChanged()           }
    });

并在您的适配器中

public class Adaptor extends BaseAdapter {
TextView tvtitle;
Context context;
private int textSize;
private int color;



public NewsAdaptor ( Context context, ArrayList <Item> List ) {
    this.context = context;
    this.newsList = List;
    this.color = Color.RED   //default color
}

ArrayList <Item> List;


@Override
public int getCount () {

    returnList.size();
}

@Override
public Object getItem ( int position ) {

    return List.get(position);
}

@Override
public long getItemId ( int position ) {
    return 0;
}

@SuppressLint("ResourceAsColor")
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
    if (convertView == null) {
        convertView = View.inflate(context, R.layout.newsitemlist_layout, null);
    }
    Item currentNews = List.get(position);
    tvtitle = (TextView) convertView.findViewById(R.id.textView1id);
    tvtitle.setText(currentNews.getTitle());
    tvtitle.setColor(color);
    return convertView;
}

public void setTextColor(int color){
    this.color = color;
}