我想要更改列表项的背景色,具体取决于是否单击该项。我怎么能做到这一点!我做了以下尝试:
articleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current article that was clicked on
Article currentArticle = mAdapter.getItem(position);
if (currentArticle.getUrl() != null) {
TextView article_TV = (TextView) findViewById(R.id.post_title);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
article_TV.setBackgroundColor(getApplicationContext().getColor(R.color.colorItemClicked));
}
else
article_TV.setBackgroundColor(getResources().getColor(R.color.colorItemClicked));
}
}
});更新:-这是个愚蠢的错误。按照ak sacha的建议,TextView article_TV = (TextView) view.findViewById(R.id.post_title);
发布于 2017-02-18 12:06:12
正如As所建议的,我们可以通过使用TextView article_TV = (TextView) view.findviewbyid (R.id.post_title)来实现它;这是因为我们在适配器中使用它,所以我们需要在listview row.Hence中找到使用view.findviewbyid的视图。
https://stackoverflow.com/questions/42314691
复制相似问题