更改适配器中的文本颜色

时间:2018-07-01 03:04:14

标签: android android-adapter

我需要通过单击按钮的开关来更改列表视图中的文本颜色。具有自定义适配器和自定义布局的列表视图。 newsitemlist_layout.xml中的文本视图被适配器正常填充后可以正常工作,但是片段显示错误:

  在空对象引用上

Adaptor.setTextColor(int)'

问题是,如果我在片段中实现了更改文本颜色,但是适配器在主活动中初始化了适配器,而开关或按钮在主活动中初始化了,但在主活动中却没有。有什么办法吗?

MainActivity.java:

NewsAdaptor adaptor;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick ( View view ) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);


aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
   @SuppressLint("ResourceAsColor")
   @Override
   public void onCheckedChanged ( CompoundButton buttonView, boolean   isChecked ) {

           adaptor.setTextColor(Color.GREEN);
            adaptor.notifyDataSetChanged();
           } else {


            adaptor.setTextColor(Color.RED);
            adaptor.notifyDataSetChanged();

        }
   }
});

Fragment.java

NewsAdaptor adapter;
adapter = new NewsAdaptor(Main2Activity.this, newsItemsList);
    lvRss.setAdapter(adapter);

NewsAdapter.java

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



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

    ArrayList <NewsItem> newsList;


    @Override
    public int getCount () {

        return newsList.size();
    }

    @Override
    public Object getItem ( int position ) {

        return newsList.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);

        }
        NewsItem currentNews = newsList.get(position);
        tvtitle = (TextView) convertView.findViewById(R.id.textView1id);

        tvtitle.setText(currentNews.getTitle());
        tvtitle.setTextColor(color);
        return convertView;
    }
    public void setTextColor(int color) {
        this.color = color;
    }
}

NewsItem.java

public class NewsItem implements Serializable {

String title;

public String getTitle () {
    return title;
}

public void setTitle ( String title ) {
    this.title = title;
}


@Override
public String toString() {
    return title;
}

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/textView1id"
        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" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

  

“空对象引用上的Adaptor.setTextColor(int)'”。

这是因为您从未在Activity中创建适配器,而是在尝试对其进行修改。

您需要通过片段访问它,因为适配器位于片段内部。因此,在您的片段中创建一个公共方法,如下所示:

public changeAdapterTextColor(int color) {
  if(adaptor == null) {
     Log.e("FRAGMENT", "please initialize the adaptop!");
     return;
  }

  adaptor.setTextColor(color);
  adaptor.notifyDataSetChanged();
}

然后,您可以通过在Activity中使用以下内容来更新适配器:

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if(isChecked) {
           // Assume the fragment is attached and use mFragment as pointer
           mFragment.changeAdapterTextColor(Color.GREEN);
         } else {
           mFragment.changeAdapterTextColor(Color.RED);
         }
       }
    });