android 自定义ListView布局中动态加入几个TextView控件, 如何获得每个TextView来单独改变某一个TextView的文字颜色啊 。
public class ListView3333 extends Activity {/**
* @查看我发布的商品
*/
private List<Map<String, Object>> mData;
private ListView mListView;
public static int count = 0;
private MyAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
mListView=(ListView)findViewById(R.id.MyListView3);
adapter = new MyAdapter(this);
mListView.setAdapter(adapter);
mListView.setItemsCanFocus(false);
mListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击列表中栏目发生的事件
Toast.makeText(ListView3333.this, "商品描述:" + position, Toast.LENGTH_LONG).show();
}
});
Button changeButton = (Button)findViewById(R.id.changebutton);
changeButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
if(count == mData.size()){
count = 0;
}
Map<String, Object> map = new HashMap<String, Object>();
//map.put("title", "这是第" + (i + 1) + "题目");
map.put("A", count + "E选项 ");
map.put("B", count +"F选项 ");
map.put("C", count +"G选项 ");
map.put("D", count +"H选项 ");
mData.remove(count);
mData.add(count++,map);
adapter.notifyDataSetChanged();
//能够改变某个TextView的数据
//但是不能单独改变某个TExtView的颜色
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private void init() {
mData=new ArrayList<Map<String, Object>>();
for(int i=0; i<30; i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("A", "A选项 ");
map.put("B", "B选项 ");
map.put("C", "C选项 ");
map.put("D", "D选项 ");
mData.add(map);
}
}
class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
init();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//System.out.println("position: " + position);
convertView = mInflater.inflate(R.layout.my_listitem3, null);
TextView t1 = (TextView) convertView.findViewById(R.id.textView1);
TextView t2 = (TextView) convertView.findViewById(R.id.textView2);
TextView t3 = (TextView) convertView.findViewById(R.id.textView3);
TextView t4 = (TextView) convertView.findViewById(R.id.textView4);
convertView.setTag(position);
t1.setText((String) mData.get(position).get("A"));
t2.setText((String) mData.get(position).get("B"));
t3.setText((String) mData.get(position).get("C"));
t4.setText((String) mData.get(position).get("D"));
t1.setTextColor(Color.GREEN);
t2.setTextColor(Color.BLUE);
t3.setTextColor(Color.RED);
t4.setTextColor(Color.YELLOW);
return convertView;
}
}
main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- 横向 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 竖向1 android:background="#87CEEB"-->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/MyListView3">
</ListView>
</LinearLayout>
<!-- 竖向2 -->
<LinearLayout
android:layout_weight="2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="有区域"
android:textSize="35dip"
android:background="#808000"
/>
<Button
android:id="@+id/changebutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="32dip"
android:text="改 变"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
my_listitem3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dip"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dip"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dip"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dip"
/>
</LinearLayout>
--------------------编程问答-------------------- 自己把问题解决了,其实你根本不用获得那个TextView,只要你的数据改变了,你就可以通知ListView数据改变,这时候ListView就会重新加载适配器,这时你根据条件设置字体的颜色就行了。。
通知ListView数据改变代码adapter.notifyDataSetChanged();
补充:移动开发 , Android