开发者

Controlling individual views in a ListActivity

开发者 https://www.devze.com 2023-03-16 16:12 出处:网络
I want all of the items in a list displayed through a ListActivity to be a different color (please no comments on poor graphic design; I am trying to create a demo).

I want all of the items in a list displayed through a ListActivity to be a different color (please no comments on poor graphic design; I am trying to create a demo).

Displaying the ListActivity is simple. However, I have seen from reading other questions here that you cannot iterate through the ListView. So, how can you control the TextView for the individual list entries?

Here is my sample program; the commented out area shows code that does not work.

package com.explorenm.colorlist;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ColorList extends ListActivity {
    private static final String colors[] = {
        "red", "green", "blue", "cyan", "magenta",
        "yellow", "black", "white"
    };
    private static final int colorvalues[] = {
        // red,   green,    blue,     cyan,     
        0xFF0000, 0x00FF00, 0x0000FF, 0x00FFFF, 
        // magenta, yellow,   black,   white
        0xFF00FF, 0xFFFF00, 0x000000, 0xFFFFFF
    };
@Override
public void onCreate(Bundle savedInstanceState) {
    TextView tv = null;
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, colors));
    ListView lv = getListView();
//    for (int i=0; i<colors.length开发者_开发知识库; i++) {
//        tv = (TextView) lv.getItemAtPosition(i); // lv.getChildAt(i) also fails
//        tv.setBackgroundColor(colorvalues[i]);
//    }
    }
}


You can implement your own ListAdapter (or, more specifically, extend ArrayAdapter<String>) and override the getView( int position, View convertView, ViewGroup parent) method. This enables you to construct the views for the individual list items yourself, and you can therefore do exactly what you want with them.

public class MyAdapter extends ArrayAdapter<String>
    public View getView( final int position, View convertView, final ViewGroup parent )
    {
        View v = convertView;
        if( v == null )
        {
            LayoutInflater li = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            v = li.inflate( R.layout.listitem, null );
        }
        String getItem = getItem( position );
        // Style the item and set the text here
    }
}

It is worth pointing out that you should always try and re-use the convertView parameter rather than inflating a new view each time this method is called. The reason for this is that inflating view is expensing, and doing this needlessly will affect the smooth scrolling of your ListView quite considerably.


hope this will help..

MyList.class

public class MyList extends Activity implements OnItemClickListener {

private List<String> strings = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(new MyArrayAdatpter(getApplicationContext(), 0,
            strings));
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    Toast.makeText(getApplicationContext(), strings.get(arg2)+" at position-"+arg2,
            Toast.LENGTH_SHORT).show();
}
}   

custom ArrayAdapter

public class MyArrayAdatpter extends ArrayAdapter<String>{

private List<String> list=new ArrayList<String>();
private Context context;
private int randomColour;
private static final int colorvalues[] = {
    // red,   green,    blue,     cyan,     
    Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, 
    // magenta, yellow,   black,   white
    Color.MAGENTA, Color.YELLOW, Color.BLACK, Color.WHITE
};
public MyArratAdatpter(Context applicationContext, int i,   List<String> strings) {
    super(applicationContext,i);
    this.context=applicationContext;
    list=strings;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public String getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}
@Override
public int getPosition(String item) {
    // TODO Auto-generated method stub
    return list.indexOf(item);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view=LayoutInflater.from(context).inflate(R.layout.textlayout,null);
    TextView textView=(TextView)view.findViewById(R.id.text);
    textView.setText(list.get(position));
    randomColour = new Random().nextInt(7);
    textView.setTextColor(colorvalues[randomColour]);
    return view;
}   
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消