I am using a ListView as shown below in filename browse.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:
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:
<?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..
[EDIT]: Here am using ArrayAdapter default constructor to list all the items in a listview
Pls suggest me something.. Thanks..
We can change the textcolor dynamically for list item in the getView() of Adapter.
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
SECOND
now when in itemClickListener
set TRUE at position clicked
in saved
like, saved.set(position,TRUE);
and call notifyDataSetChanged();
in that listenher after setting TRUE at that position.
THIRD
now in getView of Adapter Class
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With