Android 如何在ListView中标记视图?

Android 如何在ListView中标记视图?,android,listview,initialization,Android,Listview,Initialization,我有一个带有列表视图的应用程序。listview工作正常。当我希望列表以标记的某些行开始时,问题就开始了。如果我按它,我可以标记一行。但是,似乎没有找到在初始化时标记任何行的方法 这是我的代码: listViewOfBluetooth = getListView(); setInitialEnabledDevices(); listViewOfBluetooth.setOnItemClickListener(new OnItemClickListener() { public void

我有一个带有列表视图的应用程序。listview工作正常。当我希望列表以标记的某些行开始时,问题就开始了。如果我按它,我可以标记一行。但是,似乎没有找到在初始化时标记任何行的方法

这是我的代码:

listViewOfBluetooth = getListView();
setInitialEnabledDevices();
  listViewOfBluetooth.setOnItemClickListener(new OnItemClickListener() {

  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      String chosenBluetoothDevice = (String) ((TextView) view).getText();

      BluetoothEnableOrDisable(view, chosenBluetoothDevice);
      Toast.makeText(getApplicationContext(), chosenBluetoothDevice, Toast.LENGTH_SHORT).show();
      editor.putString("bluetooth_name_from_list1", chosenBluetoothDevice);
      editor.putBoolean("have_the_cars_bluetooth", true);
      editor.commit();
      Intent intent = new Intent(List.this, ParkOGuardActivity.class);
      startActivity(intent);
      }
  });
}

public static void setInitialEnabledDevices(){
    int length = listViewOfBluetooth.getChildCount();
    View view = null;
    String first = prefs.getString("bluetooth_name_from_list0", "");
    String second = prefs.getString("bluetooth_name_from_list1", "");
    String third = prefs.getString("bluetooth_name_from_list2", "");
    for(int i = 0; i < length; i++){
        view = listViewOfBluetooth.getChildAt(i);
        if(view.equals(first) || view.equals(second) || view.equals(third)) {
            view.setBackgroundColor(Color.GRAY);
        }
    }       
}
listViewOfBluetooth=getListView();
setInitialeEnabledDevices();
setOnItemClickListener(新的OnItemClickListener()){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串chosenBluetoothDevice=(字符串)((TextView)视图).getText();
BluetoothEnablerDisable(视图,chosenBluetoothDevice);
Toast.makeText(getApplicationContext(),chosenBluetoothDevice,Toast.LENGTH_SHORT).show();
putString(“bluetooth\u name\u from\u list1”,chosenBluetoothDevice);
putBoolean(“有车有蓝牙”,真的);
commit();
意向意向=新意向(List.this,ParkOGuardActivity.class);
星触觉(意向);
}
});
}
公共静态void setInitialeEnabledDevices(){
int length=listViewOfBluetooth.getChildCount();
视图=空;
String first=prefs.getString(“bluetooth_name_from_list0”,”);
String second=prefs.getString(“bluetooth_name_from_list1”,”);
stringthird=prefs.getString(“bluetooth_name_from_list2”,”);
for(int i=0;i
我怎样才能修好它


谢谢

您可以通过使用自定义适配器来实现这一点。这里是解决方法

  • 初始化自定义适配器
  • 为标记的设备名称添加一些标志
  • 覆盖
    getView()
    &检查标志。并相应地设置列表项的背景
如果你不明白或面临任何复杂问题,请回复

更新:

下面是一个适配器示例。我没有编译代码。所以可能有一些错误

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TestAdapter extends BaseAdapter
{
    ArrayList<String> deviceNames;
    ArrayList<Boolean> selected;
    Context context;

    public TestAdapter(Context context)
    {
        this.context = context;
        deviceNames = new ArrayList<String>();
        selected = new ArrayList<Boolean>();
    }

    public void addDeviceToList(String deviceName, boolean isSelected)
    {
        deviceNames.add(deviceName);
        selected.add(isSelected);
        notifyDataSetChanged();
    }

    public int getCount()
    {
        return deviceNames.size();
    }

    public Object getItem(int position)
    {
        return deviceNames.get(position);
    }

    public long getItemId(int position)
    {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv = new TextView(context);
        tv.setText(deviceNames.get(position));
        if(selected.get(position) == true)
        {
            tv.setBackgroundColor(Color.parseColor("#ff0000"));
        }
        return tv;
    }
}
import java.util.ArrayList;
导入android.content.Context;
导入android.graphics.Color;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
公共类TestAdapter扩展了BaseAdapter
{
ArrayList deviceNames;
选择ArrayList;
语境;
公共测试适配器(上下文)
{
this.context=上下文;
deviceNames=新的ArrayList();
selected=newarraylist();
}
public void addDeviceToList(字符串deviceName,布尔值isSelected)
{
deviceName.add(deviceName);
已选择。添加(已选择);
notifyDataSetChanged();
}
public int getCount()
{
返回deviceNames.size();
}
公共对象getItem(int位置)
{
返回deviceNames.get(位置);
}
公共长getItemId(int位置)
{
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
TextView tv=新的TextView(上下文);
tv.setText(deviceNames.get(position));
如果(已选择。获取(位置)=真)
{
tv.setBackgroundColor(Color.parseColor(#ff0000”);
}
返回电视;
}
}
现在创建适配器对象并将适配器设置为listView。并通过调用
addDeviceToList()
方法来添加单个项。

这似乎令人讨厌 但我认为您需要在加载listview之前修改它内部的视图

问题是,只要列表没有显示给用户,列表就不会有子项。因此,在向用户显示视图之前,您不能修改视图

但是,如果您确实需要在如此低的级别上与视图通信,您可以尝试在列表中附加一个滚动侦听器:

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    for (int i = 0; i < visibleItemCount; i++) {
        View child = getChildAt(i);
        Now edit this view 

    }
}
@覆盖
public void onScroll(AbsListView视图、int firstVisibleItem、int visibleItemCount、int totalItemCount){
对于(int i=0;i
我使用listadapter:setListAdapter(新的ArrayAdapter(this,R.layout.list,arrayOfBluetoothDevicesNames));为什么不创建一个扩展ArrayAdapter的适配器呢?并重写getView()method.im此处的新内容。。。所以,我不清楚。我希望你能给我看一些代码,但会明白如果它要求太多。。。