0

我有一个带有搜索列表视图的搜索框的列表视图。我在评估每个项目以启动不同的活动时遇到问题,因为当我搜索时,项目的位置和 ID 会发生变化,因为列表视图将仅显示搜索结果。可以有人帮忙吗??

这是我的代码:

        public class MainActivity extends Activity implements OnItemClickListener {


EditText edittext;
ListView listview;




String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten" };

int[] image = { R.drawable.one, R.drawable.two, R.drawable.three,
R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven,
R.drawable.eight, R.drawable.nine, R.drawable.ten };
int textlength = 0;
ArrayList text_sort = new ArrayList();
ArrayList image_sort = new ArrayList();

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

edittext = (EditText) findViewById(R.id.EditText01);
listview = (ListView) findViewById(R.id.ListView01);
listview.setAdapter(new MyCustomAdapter(text, image));
listview.setOnItemClickListener(this);






edittext.addTextChangedListener(new TextWatcher()
{

public void afterTextChanged(Editable s)
{

}

public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{

}

public void onTextChanged(CharSequence s, int start,
int before, int count)
{

textlength = edittext.getText().length();
text_sort.clear();
image_sort.clear();

for(int i=0; i<text.length; i++)
{
if (textlength <= text[i].length())
{
if (edittext.getText().toString().
equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
{
text_sort.add(text[i]);
image_sort.add(image[i]);
}
}
}

listview.setAdapter(new MyCustomAdapter
(text_sort, image_sort));

}
});
}


class MyCustomAdapter extends BaseAdapter
{

String[] data_text;
int[] data_image;

MyCustomAdapter()
{

}

MyCustomAdapter(String[] text, int[] image)
{
data_text = text;
data_image = image;
}
MyCustomAdapter(ArrayList text, ArrayList image)
{
data_text = new String[text.size()];
data_image = new int[image.size()];

for(int i=0; i<text.size(); i++)
{
data_text[i] = (String) text.get(i);
data_image[i] = (Integer) image.get(i);
}

}

public int getCount()
{
return data_text.length;
}

public String getItem(int position)
{
return null;
}

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

public View getView(int position, View convertView, ViewGroup parent)
{

LayoutInflater inflater = getLayoutInflater();
View row;

row = inflater.inflate(R.layout.listview, parent, false);
TextView  textview = (TextView) row.findViewById(R.id.TextView01);
ImageView imageview = (ImageView) row.findViewById(R.id.ImageView01);

textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);

return (row);

}






}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
    // TODO Auto-generated method stub

    //HOW SHOULD I HANDLE THE CLICKS HERE 






}
}

我的解决方案不起作用:

1)获取item的位置,去对应的activity

2) 获取 id ................................... …………

4

3 回答 3

0
public class MainActivity extends Activity implements OnItemClickListener {

EditText edittext;
ListView listview;

String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };

int[] image = { R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven, R.drawable.eight, R.drawable.nine, R.drawable.ten };
int textlength = 0;
ArrayList text_sort = new ArrayList();
ArrayList image_sort = new ArrayList();


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    edittext = (EditText) findViewById(R.id.EditText01);
    listview = (ListView) findViewById(R.id.ListView01);
    listview.setAdapter(new MyCustomAdapter(text, image,prepareIntent()));
    listview.setOnItemClickListener(this);

    edittext.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {

            textlength = edittext.getText().length();
            text_sort.clear();
            image_sort.clear();

            for (int i = 0; i < text.length; i++) {
                if (textlength <= text[i].length()) {
                    if (edittext.getText().toString().equalsIgnoreCase((String) text[i].subSequence(0, textlength))) {
                        text_sort.add(text[i]);
                        image_sort.add(image[i]);
                    }
                }
            }

            listview.setAdapter(new MyCustomAdapter(text_sort, image_sort));

        }
    });
}

private ArrayList<Intent > prepareIntent(){
    ArrayList<Intent> intentList = new ArrayList<Intent>();
    for (int i = 0; i < text.length; i++) {
        Intent intent = new Intent("your current activity","you wan to open activity");
        intent.putExtra("key", text[i]);//here you set intent put extra as per your requirement.
        intentList.add(intent);

    }
    return intentList;
}

class MyCustomAdapter extends BaseAdapter {

    String[] data_text;
    int[] data_image;
    ArrayList<Intent> intentList;

    MyCustomAdapter() {

    }

    MyCustomAdapter(String[] text, int[] image,ArrayList<Intent> intentList) {
        data_text = text;
        data_image = image;
        this.intentList = intentList;
    }

    MyCustomAdapter(ArrayList text, ArrayList image) {
        data_text = new String[text.size()];
        data_image = new int[image.size()];

        for (int i = 0; i < text.size(); i++) {
            data_text[i] = (String) text.get(i);
            data_image[i] = (Integer) image.get(i);
        }

    }

    public int getCount() {
        return data_text.length;
    }

    public String getItem(int position) {
        return null;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row;

        row = inflater.inflate(R.layout.listview, parent, false);
        TextView textview = (TextView) row.findViewById(R.id.TextView01);
        ImageView imageview = (ImageView) row.findViewById(R.id.ImageView01);

        textview.setText(data_text[position]);
        imageview.setImageResource(data_image[position]);

        row.setTag(intentList.get(position));
        return (row);

    }

}

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
    // TODO Auto-generated method stub

    // HOW SHOULD I HANDLE THE CLICKS HERE
    startActivity((Intent)view.getTag());
}

}

于 2013-09-02T11:22:45.980 回答
0
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) 
{
Intent i = new Intent(ActivityName.this,SecondActivity,class);
i.putExtra("key",position);
startActivity(i)
}
});

使用 Intent 在第二个活动中获取价值

int i = getIntent().getExtras().getInt("key");
于 2013-09-02T11:01:42.110 回答
0

在适配器的 getView 中,当您找到任何 textview 时,您可以将 setTag 设置为 like your_text_view.setTag("TextView"+position);。之后,您可以使用 findViewWithTag() 方法准确查找单击了哪个 textview 或 listItem 。例如:INSIDE GETVIEW `RelativeLayout rel = null; if (null == convertView) {

        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater) getContext().getSystemService(inflater);
        rel = (RelativeLayout) vi.inflate(resource, rel, true);

    } else {
        rel = (RelativeLayout) convertView;

    }
            rel.setTag("Txt:" + position);`

之后
`final RelativeLayout rel1 = rel;

    // click Listener of day list
    rel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            String seletedPosition = (String) v.getTag();
            String[] splitted = seletedPosition.split(":");
            int tag = Integer.parseInt(splitted[1]);
            mPosition = tag;
            SELECTED_DATE = getItem(tag).date;  
                           if(SELECTED_DATE.equals("something")){
                            startActivity(new Intent(OldActivityContext,NewActivity.class));}`  
于 2013-09-02T11:06:00.540 回答