我正在研究一个列表视图,并想突出显示列表视图中的第 3 行到第 7 行。
代码:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_list);
lv = (ListView) findViewById(R.id.sample_listview);
String[] Sample_List = getResources().getStringArray(R.array.Sample_List);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < Sample_List.length; ++i)
{
list.add(Sample_List[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_item, list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
...
}
});
}
更新:
我已将代码更新如下:
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
...
}
});
}
public class MyCustomArrayAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;
public MyCustomArrayAdapter(Context context, String[] values)
{
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
if(position>=3&&position<=7)
{
view.setBackgroundResource(R.drawable.green_btn);
}
else
{
view.setBackgroundResource(R.drawable.white_btn);
}
return view;
}
}
问题:
我在网上进行了很多研究,但我仍然无法弄清楚如何实现getView
...以这种方式如何更改上述内容,例如,将背景第 3 行到第 7 行设置为红色?
实现了更新的代码,listview不能正常展开来显示每个item,只能显示item的边框(绿色按钮有一些黑色边框),可能是因为没有链接列表。
如何将列表与 MyCustomArrayAdapter 连接起来?