我希望有人能回答这个问题。我有一个名为main.xml的视图,该视图正在使用另一个视图list_item.xml,就像将一些信息传递给main.xml,其中有一些列表等等。现在,在list_item.xml中我有TextView我想要随着内容的变化改变颜色...所以我很好地获得了textview的内容,但是我无法访问那个textview来改变颜色..一切都发生在一个活动中..这是代码的一部分活性:
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(getBaseContext(), "Zadaci se učitavaju...", Toast.LENGTH_LONG).show();
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//PROVERA KONEKCIJE
CheckConnectivity check = new CheckConnectivity();
Boolean conn = check.checkNow(this.getApplicationContext());
if(conn == true){
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_OBAV);
// looping through all item nodes <item>
TextView tvv = (TextView)findViewById(R.id.prioritet);
int num = 1;
String str = String.valueOf(num);
for (int i = 0; i < 5; i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_PROF, parser.getValue(e, KEY_PROF));
map.put(KEY_PRE, parser.getValue(e, KEY_PRE));
map.put(KEY_TXT, parser.getValue(e, KEY_TXT));
map.put(KEY_VRE, parser.getValue(e, KEY_VRE));
map.put(KEY_PRI, parser.getValue(e, KEY_PRI));
map.put(KEY_ATA, parser.getValue(e, KEY_ATA));
menuItems.add(map);
if((parser.getValue(e, KEY_PRI)).equalsIgnoreCase(str)){
tvv.setBackgroundResource(R.color.somecolor);
System.out.println((parser.getValue(e, KEY_PRI)).toString());
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_PROF, KEY_PRE, KEY_TXT, KEY_VRE, KEY_PRI, KEY_ATA}, new int[] {
R.id.profesor,R.id.vreme, R.id.tekst, R.id.predmet, R.id.prioritet, R.id.atachment });
setListAdapter(adapter);
}
这是list_item.xml,我需要访问名为“prioritet”的textview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/bordo">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="@+id/prioritet"
android:layout_width="210dp"
android:layout_height="15dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="10dp"
android:background="@color/bordot"
android:gravity="left"
android:textColor="#acacac"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-20dp"
android:background="@color/bordot"
android:gravity="left"
android:text="Prioritet:"
android:textColor="@color/bela"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/bordot"
android:gravity="left"
android:text="Atachment:"
android:textColor="@color/bela"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/atachment"
android:layout_width="210dp"
android:layout_height="15dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="-15dp"
android:background="@color/bordot"
android:gravity="left"
android:textColor="#acacac"
android:textStyle="bold" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:layout_marginTop="10dp"
android:background="@color/crvena"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
所以,我想要的只是,当textview“prioritet”中的数字1为该textview的一种颜色时,何时是2号等等... 我知道我不能只是因为视图而访问,这是主要的...我尝试了膨胀......没有工作..任何帮助表示赞赏。
答案 0 :(得分:0)
如果prioritet在list_item.xml中,则应该将findViewById调用到该ViewGroup引用,而不是针对相同的活动(它可能只是在main.xml中查找该视图)。
稍后,如果每个项目都有“优先级”视图,那么只查找一次就没有意义了。当您在onCreate上并且尚未填充列表时,您必须对每个项目进行更改,这就是您应该实现自己的ListAdapter版本的原因:
扩展ArrayAdapter并覆盖
getView(int position, View convertView, ViewGroup parent)
例如。
您可以执行以下操作:
TextView tvv = null;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
tvv = (TextView) v.findViewById(R.id.prioritet);
}
// do what you want with tvv..
// If we have to decide following KEY_PRI..
Map<String,String> currentMap = getItem(position);
/* remember that in this adapter you will only have the List of maps..
nothing else so you must check only against the current map.. */
if (currentMap.get(KEY_PRI).equals(someNum)) {
// now do what you want
}
如果您对如何为所有这些做出自己的ArrayAdapter扩展有疑问,可以执行以下操作:
public class MyMapListViewAdapter extends ArrayAdapter<Map<String,String>> {
public RoomListViewAdapter(Context context, int textViewResourceId, List<Map<String,String>> items) {
super(context, textViewResourceId, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
//... here comes the previously mentioned code
}
}
您可以将其用作..
ListView mapsList = (ListView) findViewById(R.id.your_list_view_id_in_main);
MyMapListViewAdapter listAdapter = new MyMapListViewAdapter(this, R.layout.list_item, hotel.getRooms());
//...
mapsList.setAdapter(listAdapter);