How to mark views in a ListView?

android, initialization, listview

Solution

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

- 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.

Update:

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

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

Problem

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. This is my 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? Thanks!

Original source