如何在Android中更改ListView点击事件上的TextView颜色和ImageView图像?
我想更改listview单击事件上的Textview颜色和Imageview图像&我还想创建自定义listview。我想在单击listview项目以更改图像和文本颜色并转到其他活动时,但当我返回列表活动并单击其他列表项以更改textcolor和image以及更改first click item color和image时。我的代码如下所示:如何在Android中更改ListView点击事件上的TextView颜色和ImageView图像?,android,android-listview,Android,Android Listview,我想更改listview单击事件上的Textview颜色和Imageview图像&我还想创建自定义listview。我想在单击listview项目以更改图像和文本颜色并转到其他活动时,但当我返回列表活动并单击其他列表项以更改textcolor和image以及更改first click item color和image时。我的代码如下所示: SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), cha
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
channel_listView, R.layout.listview_layout1, from, to);
listView.setDivider(null);
listView.setAdapter(adapter);
listView.setCacheColorHint(0);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
long c_name = listView.getItemIdAtPosition(position);
Log.i("c_name", "" + c_name);
((TextView) arg1.findViewById(R.id.txt))
.setTextColor(Color.YELLOW);
((ImageView) arg1.findViewById(R.id.flag))
.setBackgroundResource(R.drawable.yellowmusicicon);
String ch_name = (String) ((TextView) arg1
.findViewById(R.id.txt)).getText();
Log.i("txt_value", "" + ch_name);
Intent intent = new Intent(ChannelList.this,
FMActivity.class);
intent.putExtra("id", c_name);
intent.putExtra("c_name", ch_name);
startActivity(intent);
}
});
simpledapter adapter=new simpledapter(getBaseContext(),
通道\列表视图,R.layout.listView\布局1,从,到);
setDivider(null);
setAdapter(适配器);
setCacheColorHint(0);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、,
整数位置,长arg3){
//TODO自动生成的方法存根
长c_name=listView.getItemIdAtPosition(位置);
Log.i(“c_名称”,“c_名称”);
((TextView)arg1.findViewById(R.id.txt))
.setTextColor(颜色为黄色);
((ImageView)arg1.findViewById(R.id.flag))
.setBackgroundResource(R.drawable.YellowMusicon);
字符串Chu name=(字符串)((文本视图)arg1
.findviewbyd(R.id.txt)).getText();
Log.i(“txt_值”,“Chu名称”);
意向意向=新意向(ChannelList.this,
fmc(活动类);
意向。额外(“id”,c_名称);
意图。putExtra(“c_名称”,ch_名称);
星触觉(意向);
}
});
只需将以下内容添加到您的清单(此活动中):
或:
你的活动不会再发生了。它将被保存。更多信息:声明:
SharedPrefernces mPrefs;
在“创建时”中:
mPrefs = getSharedPreferences("some name", 0);
要使用值:
int value = mPrefs.getInt("value name",0);
要保存价值,请执行以下操作:
mPrefs.edit().putInt("value name",value);
mPrefs.edit().commit();
当您从ChannelList活动返回时,会再次创建主活动(调用onCreate),列表也是如此。因此,它将显示您在xml中设置为默认值的初始颜色和图像。您需要存储新值(例如使用SharedReferences),并在getView()方法中设置它们,但是如何使用SharedReferences。请举个例子,谢谢
int value = mPrefs.getInt("value name",0);
mPrefs.edit().putInt("value name",value);
mPrefs.edit().commit();