1

我遇到了一个不错的可过滤列表视图教程,我想知道如何确定选择了哪个列表视图项目并显示敬酒。这是代码:

package com.example.listview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;


public class TestFilterListView extends Activity {
FrameLayout historyContainer;
ViewStub viewStub;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history_container);
    historyContainer = (FrameLayout) findViewById(R.id.historyContainerLayout);
    EditText filterEditText = (EditText) findViewById(R.id.filter_text);
    filterEditText.addTextChangedListener(new TextWatcher() {

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

            historyContainer.removeAllViews();
            final List<String> tempHistoryList = new ArrayList<String>();
            tempHistoryList.addAll(historyList);
            for(String data : historyList) {
                if(data.indexOf((s.toString())) == -1) {
                    tempHistoryList.remove(data);
                }
            }
            viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
            viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
            {
                public void onInflate(ViewStub stub, View inflated)
                {

                    setUIElements(inflated, tempHistoryList);
                }
            });
            historyContainer.addView(viewStub);
            viewStub.inflate();


        }


        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

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

        }
    });

    setViewStub();
}


/********************************************************************************************************/
private void setViewStub()
{
    historyList.add("first");
    historyList.add("second");
    historyList.add("third");
    historyList.add("fourth");
    historyList.add("fifth");
    historyList.add("sixth");
    historyList.add("seventh");


    viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
    viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
    {
        public void onInflate(ViewStub stub, View inflated)
        {

            setUIElements(inflated, historyList);


















        }
    });
    historyContainer.addView(viewStub);
    viewStub.inflate();



}



/********************************************************************************************************/
final List<String> historyList = new ArrayList<String>();
String displayName = "";
ListView historyListView;
private void setUIElements(View v, List<String> historyLists)
{


    if (v != null)
    {
        historyScheduleData.clear();
        //historyList.clear();

        historyScheduleData.addAll(historyLists);
        historyListView = (ListView) findViewById(R.id.historylist);
        historyListView.setAdapter(new BeatListAdapter(this));

        registerForContextMenu(historyListView);


    }


}

private static class BeatListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public BeatListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return historyScheduleData.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.history_list_view, null);
            holder = new ViewHolder();
            holder.historyData = (TextView) convertView
                    .findViewById(R.id.historytext);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.historyData.setText(historyScheduleData.get(position));

        return convertView;
    }

    static class ViewHolder {

        TextView historyData;
    }
}

private static final List<String> historyScheduleData = new ArrayList<String>();

}

我想过使用

protected void onListItemClick(ListView l, View v, int position, long id) {

        if(position == 0) {
            Intent intent = new Intent(getApplicationContext(), GODoc1Activity.class);
            startActivity(intent);

但是当我尝试实现它时,它不起作用。我在这里错过了什么吗?我该怎么做呢?

4

2 回答 2

0

你试过这个吗?:(在 onCreate 中)

historyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            //your code

            }
        });
于 2013-01-24T18:29:57.207 回答
0

如果您尝试在 ListView 中查找所选项目:

listView.getAdapter().getItem(listView.getCheckedItemPosition());

查看此处的文档以获取更多信息http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPosition%28%29

于 2013-01-24T18:43:14.067 回答