如何在ListView中标记视图?

3

我有一个带列表视图的应用程序。列表视图正常工作。问题出现在当我想让列表从一些行开始标记时。如果我按下一行,我可以标记一行。但是,在初始化时似乎找不到标记任何行的方法。

这是我的代码:

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);
        }
    }       
}

我该如何修复它?谢谢!

我使用ListAdapter:setListAdapter(new ArrayAdapter<String>(this, R.layout.list, arrayOfBluetoothDevicesNames)); - roiberg
为什么不创建一个扩展ArrayAdapter的适配器?并重写getView()方法。 - IvoryCirrus
2个回答

4
您可以通过使用自定义适配器来实现此目的。以下是解决方法。
  • 初始化您的自定义适配器
  • 为标记的设备名称添加一些标志。
  • 重写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;
    }
}

现在创建适配器对象并将适配器设置为listView。通过调用addDeviceToList()方法添加单个项目。

我是新来的...所以对我来说不太清楚。 我希望你可以给我展示一些代码,但如果这太难了,我也能理解。 - roiberg

0

看起来很麻烦,但我认为你想在加载列表视图之前修改其中的视图。

问题是,在列表未显示给用户之前,列表不会有子项。因此,在向用户显示之前,您可能无法修改视图。

但是,如果您确实需要在这种低级别上与视图通信,可以尝试将滚动监听器附加到列表中:

@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 

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接