简体   繁体   English

在列表视图中动态更改(列表项)TextView的颜色

[英]Dynamically change color of (List item)TextView in Listview

I am using a ListView as shown below in filename browse.xml. 我正在使用ListView,如下文件名browser.xml所示。

<ListView
   android:id="@+id/listView1"
   android:layout_width="250dp"
   android:layout_height="match_parent"
   android:layout_alignParentLeft="true"
   android:layout_below="@+id/relativeLayout1" >

</ListView>

And I am filling this listView inside onCreate() method as: 我在onCreate()方法中将此listView填充为:

files1=new ArrayList<String>();
File sdcard=Environment.getExternalStorageDirectory();
files1 =  getListFiles(new File(sdcard.getAbsolutePath()+File.separatorChar)); 
ArrayAdapter<String> fileList =new ArrayAdapter<String>(this, R.layout.row,files1);

setListAdapter(fileList);

row.xml is shown below as: row.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TextView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/rowtext"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:textSize="20dp" 
   android:textColor="#000000"   
   android:background="#FFFFFF"/>

This whole program is am showing all the files of sdcard at listView and on click of any listitem, am saving that file name into sharedPrefernce..Now I want to change the text color of file name(List items) in the ListView which are there in SharedPrefernce.. 整个程序在listView上显示sdcard的所有文件,并在单击任何listitem时,将该文件名保存到sharedPrefernce ..现在,我想在ListView中更改文件名(列表项)的文本颜色在SharedPrefernce中。

[EDIT]: Here am using ArrayAdapter default constructor to list all the items in a listview [编辑]:这里使用ArrayAdapter默认构造函数列出列表视图中的所有项目

Pls suggest me something.. Thanks.. 请给我一些建议..谢谢..

We can change the textcolor dynamically for list item in the getView() of Adapter. 我们可以在Adapter的getView()中动态更改列表项的文本颜色。

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if (row == null) {
        row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
    }

    TextView listTitle = (TextView) row.findViewById(R.id.rowtext);
    listTitle.setTextColor(Color.parseColor("#405478"));

    return listTitle;
}

FIRST 第一

Take one 拿一个

ArrayList<boolean> saved = new ArrayList<boolean>();

first set All element of saved means 0 to files1.size() to FALSE 首先设置保存的所有元素意味着0到files1.size()为FALSE

SECOND 第二

now when in itemClickListener set TRUE at position clicked in saved like, saved.set(position,TRUE); 现在,当在itemClickListener中将TRUE at position clicked设置为TRUE at position clicked ,保存的文件类似saved.set(position,TRUE);

and call notifyDataSetChanged(); 并调用notifyDataSetChanged(); in that listenher after setting TRUE at that position. 在该位置将TRUE设置为该听众之后。

THIRD 第三

now in getView of Adapter Class 现在在Adapter类的getView中

public View getView(int position, View convertView, ViewGroup parent) { 

    View row = convertView; 
    position = = getItemViewType(position);
    if(row==null){ 
        row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null); 
    } 

    TextView listTitle = (TextView) row.findViewById(R.id.rowtext); 
    if(saved.get(position)==TRUE)
    {
            listTitle .setTextColor(Color.parseColor("#405478")); 
    }
}

and also add this in your adapter class, 并将其添加到您的适配器类中,

@Override
public int getItemViewType(int position) {

return position;
}

@Override
public int getViewTypeCount() {
return files1.size();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM