0

I extends ArrayAdapter, painted the following picture enter image description here

public class UrlArrayAdapter extends ArrayAdapter<UrlItem> {
    private LayoutInflater inflater;
    public UrlArrayAdapter(Context context, List<UrlItem> urlLists) {
        super(context, R.layout.database_table_list, R.id.editLinearLayout1,urlLists);
        inflater = LayoutInflater.from(context);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UrlItem urlItem = (UrlItem) this.getItem(position);
        CheckBox checkBox = null;
        TextView textView = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.database_table_item, null);
            textView = (TextView) convertView.findViewById(R.id.editText1);
            checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
            convertView.setTag(urlItem);
            checkBox.setChecked(urlItem.getUse());
            textView.setText(urlItem.getUrl());
            textView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                }
            });
        } else {
        }
        return convertView;
    }
}

how to identify and remove the position to which I press? setOnClickListener() - does not work - account for a long time to click

Community
  • 1
  • 1
Max Usanin
  • 2,479
  • 6
  • 40
  • 70

2 Answers2

0

First of all, in your getView method, your code seems incorrect: you need to set values regardless of whether you use converted view or not. The code should be like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    UrlItem urlItem = (UrlItem) this.getItem(position);
    CheckBox checkBox = null;
    TextView textView = null;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.database_table_item, null);
    }

    textView = (TextView) convertView.findViewById(R.id.editText1);
    checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
    convertView.setTag(urlItem);
    checkBox.setChecked(urlItem.getUse());
    textView.setText(urlItem.getUrl());
}

Then, to detect clicks on individual items, do not use onClickListener on the items. Instead, use setOnItemClickListener on your ListView. You will have something like this:

final UrlArrayAdapter adapter = new UrlArrayAdapter(this, items);
ListView list = this.findViewById(R.id.my_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //deal here with removing the item at position _position_
        //from your adapter, then notify the list view that the data has changed
        adapter.notifyDataSetChanged();
    }
});
Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

I think this will help you.

list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // REMOVE DATA FROM YOUR LIST ACCORDING TO POSITION.
        adapter.notifyDataSetChanged();
    }
});

If you are facing any problem then you can ask.

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29