如何标记在ListView的看法? [英] How to mark views in a ListView?

查看:101
本文介绍了如何标记在ListView的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图的应用程序。列表视图工作正常。这个问题开始,当我想要列表开始与一些标记行。我可以标注一排,如果我美元是p $ PSS。但是,似乎还没有找到一种方法,以纪念在初始化任何行。

I have an app with a list view. The listview works fine. The problem starts, when I want the list to start with some of the rows marked. I can mark a row, if I press on it. But, don't seem to find a way to mark any row on initialization.

这是我的code:

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

我怎样才能解决这个问题?

How can I fix it?

谢谢!

推荐答案

您可以通过使用自定义适配器achive这一点。这里是解决办法。

You can achive this by using custom adapter. Here is the workaround.


  • 初始化您的自定义适配器

  • 添加一些标志显着的设备名称。

  • 重写 getView()&安培;检查标志。并设置相应列表项的背景。

  • Initialize your custom adapter
  • Add some flag for marked device names.
  • Override the getView() & check for the flag. And set the background of the list item accordingly.

回复,如果你不明白这一点,否则将面临任何复杂。

Reply if you don't get it or face any complexity.

更新:

下面是一个简单的适配器。我没编译code。所以可能会有一些错误。

Here is a sample adapter. I didn't compile the code. So there might be some errors.

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()方法添加单个项目。

Now create adapter object and set the adapter to listView. And add single item by calling addDeviceToList() method.

这篇关于如何标记在ListView的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆