使用菜单项上的适配器更改片段列表视图中的文本颜色

时间:2018-06-29 16:01:13

标签: android

所有人的祝福,在此应用程序中,需要更改菜单项单击上的文本颜色。 Textview是在自定义行布局中,由适配器和在两个片段Tab布局中设置的适配器进行膨胀。在菜单项上,单击需要更改listview整个listview的文本颜色。在正常应用程序上,它可以正常工作,但在片段上,如果有人可以提供帮助,它将在错误“空对象引用上给出Adaptor.setTextColor(int)'”。谢谢 问题是适配器未在片段中初始化的主要活动中初始化。每个片段中的SO都需要像

这样的思想
public void setColor(int color) 
 adapter.setTextColor(color); 
adapter.notifyDataSetChanged(); }

它是“主要活动”选项菜单

Fragment fragment=new Fragment(); 
fragment.setColor(Color.BLACK); –

MainActivity.java

    private SectionsPagerAdapter mSectionsPagerAdapter;
 private ViewPager mViewPager;
   NewsAdaptor adaptor;

protected void onCreate ( Bundle savedInstanceState ) {
    super.onCreate(savedInstanceState);
    Context mContext = getApplicationContext();
    setContentView(R.layout.activity_main);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(10);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}


  @Override
public boolean onCreateOptionsMenu ( Menu menu ) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@SuppressLint("ResourceType")
@Override
public boolean onOptionsItemSelected ( MenuItem item ) {

    int id = item.getItemId();
    if (id == R.id.chagecolor) {
        Fragment fragment=new Fragment(); 
         fragment.setColor(Color.BLACK); –

    }
    return super.onOptionsItemSelected(item);
}

newsApapter.java

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

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

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

}

}

现在在实现了whare适配器的片段中

交易.java

public class TRADE extends Fragment {


public TRADE () {
    // Required empty public constructor
}
ArrayList<NewsItem> newsItemsList;

ListView ali;
NewsAdaptor adapter;

@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState ) {
    View view= inflater.inflate(R.layout.fragment_trade, container, false);
      newsItemsList = new ArrayList<>();
      ali = (ListView)view. findViewById(R.id.listtrade);
       RequestQueue requestQueue = Volley.newRequestQueue(getContext());
      StringRequest stringRequest = new StringRequest("https://urdu.geo.tv/rss/1/3", new Response.Listener <String>() {
        @Override
        public void onResponse ( String response ) {
            Document document = Jsoup.parse(response);
            Elements itemElements = document.getElementsByTag("item");
            for (int i = 0; i < itemElements.size(); i++) {
                Element item = itemElements.get(i);
                String title = rem(item.child(0).text());

                NewsItem news = new NewsItem();
                news.title = title;
                newsItemsList.add(news);
            }
            adapter = new NewsAdaptor(getContext(), newsItemsList);
            ali.setAdapter(adapter);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse ( VolleyError error ) {


        }
    });
    requestQueue.add(stringRequest);
  public void setColor(int color) 
  adapter.setTextColor(color); 
  adapter.notifyDataSetChanged(); }

SPORT.java

public class TRADE extends Fragment {


public SPORT() {
    // Required empty public constructor
}
ArrayList<NewsItem> newsItemsList;

ListView ali;
NewsAdaptor adapter;

@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState ) {
    View view= inflater.inflate(R.layout.fragment_trade, container, false);
      newsItemsList = new ArrayList<>();
      ali = (ListView)view. findViewById(R.id.listtrade);
       RequestQueue requestQueue = Volley.newRequestQueue(getContext());
      StringRequest stringRequest = new StringRequest("https://urdu.geo.tv/rss/1/3", new Response.Listener <String>() {
        @Override
        public void onResponse ( String response ) {
            Document document = Jsoup.parse(response);
            Elements itemElements = document.getElementsByTag("item");
            for (int i = 0; i < itemElements.size(); i++) {
                Element item = itemElements.get(i);
                String title = rem(item.child(0).text());

                NewsItem news = new NewsItem();
                news.title = title;
                newsItemsList.add(news);
            }
            adapter = new NewsAdaptor(getContext(), newsItemsList);
            ali.setAdapter(adapter);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse ( VolleyError error ) {


        }
    });
    requestQueue.add(stringRequest);
    public void setColor(int color) 
    adapter.setTextColor(color); 
    adapter.notifyDataSetChanged(); }

newsitemlist_layout.xml

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

<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:textSize="18sp"
    android:textStyle="bold" />

    </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

我认为您不是在对适配器说您做了更改。

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

希望有帮助。

答案 1 :(得分:0)

在MainActivity中:

@SuppressLint("ResourceType")
@Override
public boolean onOptionsItemSelected ( MenuItem item ) {

    int id = item.getItemId();
    if (id == R.id.chagecolor) {
        adaptor.setTextColor(Color.GREEN);
        adaptor.notifyDataSetChanged();
    }
    return super.onOptionsItemSelected(item);
}

调用adaptor.setTextColor(Color.GREEN);时,adaptor字段为null,因为您从未设置过它。您必须在MainActivity的某个位置执行adaptor=...