In the attached picture of a fragment layout from google's blog I have a listview in my code. My question here is to set the first two items(Henry IV and Henry V) with background colors RED and others remain as defaul backgroudn values. Is this possible?
Any help would be really appreciated.
Thanks, Sana.
The code that I use to get the ListView is
Log.i(TAG, "Selected item position = "+getListView().getCount()+" Child count = "+getListView().ge开发者_C百科tChildCount());
for (int i=0; i<getListView().getCount();i++) {
Log.i(TAG, "Item id = "+getListView().getItemIdAtPosition(i));
View v = getActivity().findViewById((int) getListView().getItemIdAtPosition(i));
View v1 = getListView().getChildAt(i);
Log.i(TAG, "Condition = "+(v == null)); //Here I get the display as true
Log.i(TAG, "Condition1 = "+(v1 == null)); //Here I get again a true, so basically both are none!
}
Your best bet is to hold a data item for your Kings, and use an array adapter to render them, which you can notify of changes and set the appropriate colours as it renders.
A full example of what you're after is here:
public class TestStuff extends Activity {
private class KingItem {
public KingItem(String name, String description) {
this.name = name;
this.description = description;
this.hexColor = "#000000";
}
public String name;
public final String description;
public String hexColor;
}
private List<KingItem> kings = new ArrayList<TestStuff.KingItem>();
private ListView kingsList;
private ArrayAdapter<String> kingAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
kings.add(new KingItem("Henry IV(1)", "Description form Henry IV(1)"));
kings.add(new KingItem("Henry V", "Description for Henry V"));
kings.add(new KingItem("Henry VIII", "Description for Henry VIII"));
kings.add(new KingItem("Richard II", "Description for Richard II"));
kingsList = (ListView) findViewById(R.id.list);
if (kingAdapter == null) {
kingAdapter = new KingsListAdapter(this, this, android.R.layout.simple_list_item_1, new ArrayList<String>());
}
kingAdapter.clear();
kingsList.setAdapter(kingAdapter);
kingsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView desc = (TextView) TestStuff.this.findViewById(R.id.text);
KingItem king = kings.get(position);
desc.setText(king.description);
king.hexColor = "#0000ff";
for (int i=0; i<position; i++) {
kings.get(i).hexColor = "#ff0000";
}
for (int i=position+1; i<kings.size(); i++) {
kings.get(i).hexColor = "#000000";
}
kingAdapter.notifyDataSetChanged();
}
});
for (KingItem king : kings) {
kingAdapter.add(king.name);
}
kingAdapter.notifyDataSetChanged();
}
private class KingsListAdapter extends ArrayAdapter<String> {
private final Activity activity;
private final int textViewResourceId;
public KingsListAdapter(Activity activity, Context context, int textViewResourceId, List<String> kingsNames) {
super(context, textViewResourceId, kingsNames);
this.activity = activity;
this.textViewResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(textViewResourceId, null);
}
KingItem kingItem = kings.get(position);
TextView text1 = (TextView) v.findViewById(android.R.id.text1);
text1.setText(kingItem.name);
v.setBackgroundColor(Color.parseColor(kingItem.hexColor));
return v;
}
}
}
This will render every item up to the currently selected one in red, and the selected one blue, anything after the currently selected item goes back to black background.
Notice I'm just setting the KingItem to the right colour, then telling the list to re-render. There's no need to get views, or parents and try and cleverly set the background. Instead I'm keeping that information in the KingItem, and making the adapter very simple.
Yes it is possible ... please check below link
http://www.androidpeople.com/android-custom-listview-tutorial-part-1
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
And add one thing in custom listview inside used below code.
public View getView(int position, View convertView, ViewGroup parent) {
...... your code add extra line.
if (position == 0 || position == 1){
holder.layout.setBackgroundColor(Color.RED);
}
}
精彩评论