Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android listview,更改特定视图的属性_Java_Android_Listview_View - Fatal编程技术网

Java Android listview,更改特定视图的属性

Java Android listview,更改特定视图的属性,java,android,listview,view,Java,Android,Listview,View,在我的listview上,我滚动到它的一个特定位置,如下所示 getListView().smoothScrollToPosition(scrollposition+1) 但我也想“强调”这一观点。如何访问此特定视图的属性以更改此位置的背景色 下面是listview代码 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/androi

在我的listview上,我滚动到它的一个特定位置,如下所示

getListView().smoothScrollToPosition(scrollposition+1)

但我也想“强调”这一观点。如何访问此特定视图的属性以更改此位置的背景色

下面是listview代码

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/currentText"
    android:padding="10dp" android:textSize="24sp" android:textColor="#000000" android:background="@drawable/bglistitem"
    android:gravity="center_vertical|center_horizontal">
</TextView>
但它总是崩溃!我可以理解为什么
setDrawingCacheBackgroundColor
崩溃,但即使从该视图获取文本视图也会崩溃


帮助?

getChildAt需要索引,而不是滚动位置

问候,,
几周前我遇到了这个问题

您不应该使用ListView的getChildAt()函数。例如,如果使用getChildAt(5),它将拉取可见的第五个视图

为了解决这个问题,我必须创建一个自定义适配器来覆盖getView(),并根据发送的内容设置颜色

  public class RunAdapter extends SimpleAdapter {

public RunAdapter(Context context, List<HashMap<String, String>> items,
        int resource, String[] from, int[] to) {
    super(context, items, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView txtView = (TextView) view.findViewById(R.id.lstTurnout);
        if (txtView.getText().toString().contains("BLU")) {
            txtView.setBackgroundColor(Color.rgb(0x00, 0x00, 0xaa));
            txtView.setText(txtView.getText().toString().replace("BLU", ""));
        } else {
            txtView.setBackgroundColor(Color.rgb(0xaa, 0x00, 0x00));
            txtView.setText(txtView.getText().toString().replace("RED", ""));
        }
  }
公共类RunAdapter扩展了SimpleAdapter{
公共RunAdapter(上下文、列表项、,
int资源,字符串[]从,int[]到){
超级(上下文、项目、资源、发件人、收件人);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
TextView txtView=(TextView)view.findViewById(R.id.lstView);
if(txtView.getText().toString()包含(“BLU”)){
setBackgroundColor(Color.rgb(0x00,0x00,0xaa));
setText(txtView.getText().toString().replace(“BLU”,“BLU”));
}否则{
setBackgroundColor(Color.rgb(0xaa,0x00,0x00));
setText(txtView.getText().toString().replace(“红色”),替换;
}
}
这不是最漂亮的方法,但它有效! 根据我想要文本视图的颜色,我传递了“蓝色”或“红色” 然后在getView中,检查字符串包含的内容,并相应地更改颜色

希望我能有所帮助,祝你好运

编辑
根据请求的构造函数

它是如何崩溃的?哪些日志?setDrawingCache上的nullpointerexception。如果我注释掉setDrawingCash,则它在textview分配上的nullpointerexception。因此视图本身没有被填充ScrollPosition是一个整数,它告诉它要转到哪个视图。它包含我想要的索引,是的,我已经自定义了适配器,我想我不必为这个问题这么做。我能看看你的构造函数吗?ListView非常奇怪……我花了一段时间才弄明白它们,这个方法有点非正统,但我对它没有任何问题。它使用一个HashMap列表,每个HashMap值包含一个有意义的HashMap值。
  public class RunAdapter extends SimpleAdapter {

public RunAdapter(Context context, List<HashMap<String, String>> items,
        int resource, String[] from, int[] to) {
    super(context, items, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView txtView = (TextView) view.findViewById(R.id.lstTurnout);
        if (txtView.getText().toString().contains("BLU")) {
            txtView.setBackgroundColor(Color.rgb(0x00, 0x00, 0xaa));
            txtView.setText(txtView.getText().toString().replace("BLU", ""));
        } else {
            txtView.setBackgroundColor(Color.rgb(0xaa, 0x00, 0x00));
            txtView.setText(txtView.getText().toString().replace("RED", ""));
        }
  }