gpt4 book ai didi

android - 过滤ListView后无法获取项目的实际位置

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:27 24 4
gpt4 key购买 nike

这是我的 MainActivity.java,我在其中为项目定义了一个 OnClickListener。在这里,过滤后我总是得到错误的项目值。

如果 ListView 中有 10 个项目,那么在过滤后如果我得到 3 个项目并且正确的位置是 3,6,9 那么我得到的是 0,1,2。

package com.focusmedica.maadiabetes;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity {

private MainAdapter adapter;
private static ListView lvFiltered;
private static ArrayList<DataModel> data=new ArrayList<>();;
MyDatabase handler;
private EditText etSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lvFiltered =(ListView)findViewById(R.id.lvFiltered);
etSearch = (EditText)findViewById(R.id.etSearch);
etSearch.setText("");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

handler=new MyDatabase(this);

data=handler.getChapterDetails();
adapter = new MainAdapter(this,data);
lvFiltered.setAdapter(adapter);
lvFiltered.setTextFilterEnabled(true);

lvFiltered.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
});

etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
if(cs.length()>0) {
etSearch.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.delete, 0);
}else{
etSearch.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
// When user changed the Text
adapter.getFilter().filter(cs);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});

etSearch.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(etSearch.getCompoundDrawables()[2]!=null){
if(event.getX() >= (etSearch.getRight()- etSearch.getLeft() - etSearch.getCompoundDrawables()[2].getBounds().width())) {
etSearch.setText("");
}
}
}
return false;
}
});
}
}

这里是适配器类

package com.focusmedica.maadiabetes;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by windev on 8/2/2016.
*/

public class MainAdapter extends BaseAdapter implements Filterable {

ArrayList<DataModel> dataSet;
Context context;
DataModel content;
public ArrayList<DataModel> orig;

public MainAdapter(Context context, ArrayList dataSet) {
super();
this.context = context;
this.dataSet = dataSet;
}


@Override
public int getCount() {
return dataSet.size();
}

@Override
public Object getItem(int position) {
return dataSet.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

class ViewHolder {
private TextView tvChapterName;
private ImageView ivChapterIcon;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.card_layout, null);
viewHolder = new ViewHolder();
viewHolder.tvChapterName = (TextView) view.findViewById(R.id.tvChapterName);
viewHolder.ivChapterIcon=(ImageView)view.findViewById(R.id.ivChapterIcon);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
content = dataSet.get(position);
viewHolder.tvChapterName.setText(content.getChapterName());
viewHolder.ivChapterIcon.setImageResource(R.drawable.caticon);
return view;
}

public Filter getFilter() {
return new Filter() {

@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<DataModel> results = new ArrayList<>();
final ArrayList<DataModel> ids = new ArrayList<>();
if (orig == null)
orig = dataSet;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final DataModel g : orig) {
if (g.getChapterName().toLowerCase().contains(constraint.toString())||
g.getChapterName().toUpperCase().contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
dataSet = (ArrayList<DataModel>) results.values;
notifyDataSetChanged();
}
};
}

public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}

最佳答案

获取筛选列表中项目的“旧”位置(相当于项目 ID):

@Override
public long getItemId(int position) {

int itemID;

// orig will be null only if we haven't filtered yet:
if (orig == null)
{
itemID = position;
}
else
{
itemID = orig.indexOf(dataSet.get(position));
}
return itemID;
}

现在将此方法与 OnItemClickListener 一起使用:

    @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
intent.putExtra("position", adapter.getItemId(position) );
startActivity(intent);
}

注意:如果您需要使用 notifyDatasetChanged(),最好也覆盖此方法:

@Override
public void notifyDatasetChanged()
{
// either this or: orig = dataSet;
orig = null;
super.notifyDatasetChanged();
}

关于android - 过滤ListView后无法获取项目的实际位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38744080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com