How to tag views in a ListView?

I have a list app. The list works great. The problem starts when I want the list to start with some checked lines. I can mark a row if I click on it. But it doesn't seem to find a way to mark any line during 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 this?

Thank!

+3
source share
2 answers

You can achieve this using a custom adapter. Here is a workaround.

  • User adapter initialization
  • Add a flag for marked device names.
  • Cancel getView()and check the flag. And accordingly set the background of the list item.

The answer is if you do not receive it or do not encounter any difficulty.

Update:

. . , .

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

+4

, , .

, , .

, :

@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 

    }
}
0

All Articles