Listview ListActivity标头中按钮的OnClickListner()不工作

Listview ListActivity标头中按钮的OnClickListner()不工作,listview,button,listactivity,Listview,Button,Listactivity,在我的应用程序中,我有一个布局,在ListActivity的标题中显示公共汽车站的详细信息,同时将所有可用的到达预测显示为一个列表。列表标题包含两个按钮,第一个用于将公交车站保存到数据库,第二个用于刷新活动。但是OnClickListner()不起作用。我已经在没有onClickListner()的情况下测试了我的代码,它运行良好。 记得!在我的代码中,按钮是固定列表标题的一部分 我已经尝试过这里已经讨论过的建议,但这些都不适合我。请帮帮我。。。我的代码如下: public class Main

在我的应用程序中,我有一个布局,在ListActivity的标题中显示公共汽车站的详细信息,同时将所有可用的到达预测显示为一个列表。列表标题包含两个按钮,第一个用于将公交车站保存到数据库,第二个用于刷新活动。但是OnClickListner()不起作用。我已经在没有onClickListner()的情况下测试了我的代码,它运行良好。
记得!在我的代码中,按钮是固定列表标题的一部分 我已经尝试过这里已经讨论过的建议,但这些都不适合我。请帮帮我。。。我的代码如下:

public class MainBusStopByIdActivity extends ListActivity implements OnClickListener{

private final String TAG = getClass().getSimpleName();
ReadingBusStopByIdData readingData = new ReadingBusStopByIdData();
LinkedList<BusStop> stopList = new LinkedList<BusStop>();
LinkedList<Predictions> predictionsList = new LinkedList<Predictions>();
private AdapterForBusStopById mAdapter;
String userInput;
Button saveButton;
Button refreshButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initialising buttons
    saveButton = (Button) findViewById(R.id.save_button);
    refreshButton = (Button) findViewById(R.id.refresh_button);

    // Creating new adapter instance
    mAdapter = new AdapterForBusStopById();

    // Retrieving data (user Input) sent from previous page
    Intent newIntent = getIntent();
    userInput = newIntent.getStringExtra("input");

    // Retrieving data on background thread
    new loadBusStopDetails().execute(userInput);
    new loadBusPredictions().execute(userInput);

    // Setting up onClickListner() for both buttons
    refreshButton.setOnClickListener(this);
    saveButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {

    case R.id.save_button:
        boolean didItWork = true;
        try {
            if (stopList.size() != 0) {
                String stopId = null;
                String stopName = null;
                for (BusStop stop : stopList) {
                    stopId = stop.getStopId();
                    stopName = stop.getStopName().toString();
                }
                Toast.makeText(MainBusStopByIdActivity.this,
                        stopId + "\n" + stopName, Toast.LENGTH_LONG).show();

                DatabaseAccess entry = new DatabaseAccess(
                        MainBusStopByIdActivity.this);
                entry.open();
                entry.createEntry(stopId, stopName);
                entry.close();

            } else {
                Toast.makeText(MainBusStopByIdActivity.this,
                        "No Bus-stop Information Found!!!",
                        Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {
            didItWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Error!!!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();

        } finally {
            if (didItWork) {
                Dialog d = new Dialog(this);
                d.setTitle("Entry to Favourites");
                TextView tv = new TextView(this);
                tv.setText("Scuccessfully Added Bus-Stop to Favourites...");
                d.setContentView(tv);
                d.show();
            }
        }
        break;

    case R.id.refresh_button:

        break;
    }

}



// Adapter Class
private class AdapterForBusStopById extends BaseAdapter {
    private static final int TYPE_PREDICTION = 0;
    private static final int TYPE_BUSSTOP_HEADER = 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    public AdapterForBusStopById() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {

        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_BUSSTOP_HEADER
                : TYPE_PREDICTION;
    }

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

    public String getItem(int position) {
        return mData.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView
                + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
            case TYPE_PREDICTION:
                convertView = mInflater.inflate(R.layout.predictions, null);
                holder.textView = (TextView) convertView
                        .findViewById(R.id.predictionText);
                break;
            case TYPE_BUSSTOP_HEADER:
                convertView = mInflater.inflate(R.layout.bus_stop_header,
                        null);
                holder.textView = (TextView) convertView
                        .findViewById(R.id.textSeparator);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(mData.get(position));
        return convertView;
    }

}

public static class ViewHolder {
    public TextView textView;
}

// Downloading Bus-stop Information
// AsyncTask Class for downloading Bus-Stop data in Background
public class loadBusStopDetails extends
        AsyncTask<String, Integer, LinkedList<BusStop>> {

    @Override
    protected LinkedList<BusStop> doInBackground(String... params) {
        // TODO Auto-generated method stub

        stopList = readingData.readStopDetailsByBusStopId(userInput);
        // Retrieving & Showing bus-stop data
        for (BusStop stop : stopList) {
            String stopData = "  NAME:                  "
                    + stop.getStopName().toString() + " ("
                    + stop.getStopPointer().toString()
                    + ")\n  STOP ID:              "
                    + stop.getStopId().toString() + "\n  STOP STATUS:   "
                    + stop.getStopStatus().toString()
                    + "\n  TOWARDS:           "
                    + stop.getTowards().toString();

            Log.d(TAG, "::::::::::::::::::::DATA:::::::::::::::::::\n"
                    + "<<<<<<" + stopData + ">>>>>>\n");
            mAdapter.addSeparatorItem(stopData);
        }
        Log.d(TAG, "::::::::::::::::::::DATA:::::::::::::::::::\n"
                + "<<<<<<" + stopList.size() + ">>>>>>\n");
        return stopList;
    }

    protected void onPostExecute(LinkedList<BusStop> result) {
        stopList = result;
    }
}

// Downloading Bus-predictions Information
// AsyncTask Class for downloading Bus-Predictions data in Background
public class loadBusPredictions extends
        AsyncTask<String, Integer, LinkedList<Predictions>> {

    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog = new ProgressDialog(MainBusStopByIdActivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.show();
    }

    @Override
    protected LinkedList<Predictions> doInBackground(String... params) {
        // TODO Auto-generated method stub

        for (int i = 0; i < 20; i++) {
            publishProgress(4);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        dialog.dismiss();

        // Retrieving & Showing bus-prediction data
        predictionsList = readingData
                .readStopPredictionsByBusStopId(userInput);

        for (Predictions prediction : predictionsList) {
            int time = prediction.getTime();
            String stringTime;
            if (time == 0) {
                stringTime = "DUE";
            } else {
                stringTime = String.valueOf(time);
            }
            String predictionData = "       "
                    + prediction.getRoute().toString() + "             "
                    + stringTime + "             "
                    + prediction.getDestination().toString() + " \n ";

            mAdapter.addItem(predictionData);
        }
        return predictionsList;
    }

    protected void onProgressUpdate(Integer... progress) {
        dialog.incrementProgressBy(progress[0]);
    }

    protected void onPostExecute(LinkedList<Predictions> result) {
        predictionsList = result;
        setListAdapter(mAdapter);

        // Displaying related messages if there's something wrong
        if (stopList.size() != 0) {
            if (predictionsList.size() != 0) {
            } else {
                Toast.makeText(
                        MainBusStopByIdActivity.this,
                        "There are no Buses to this Bus Stop in Next 30 Minutes.",
                        Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(
                    MainBusStopByIdActivity.this,
                    "There is 'NO' Bus Stop Exist with " + userInput
                            + " Bus-Stop ID.", Toast.LENGTH_LONG).show();
        }

    }
}

}
public类MainBusStopByIdActivity扩展ListActivity实现OnClickListener{
私有最终字符串标记=getClass().getSimpleName();
ReadingBusStopByIdData readingData=新建ReadingBusStopByIdData();
LinkedList停止列表=新建LinkedList();
LinkedList predictionsList=新建LinkedList();
私用适配器福布斯topbyid mAdapter;
字符串用户输入;
按钮保存按钮;
按钮刷新按钮;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//初始化按钮
saveButton=(按钮)findViewById(R.id.save_按钮);
refreshButton=(按钮)findViewById(R.id.refresh_按钮);
//创建新适配器实例
mAdapter=新适配器ForBussTopById();
//检索从上一页发送的数据(用户输入)
Intent newIntent=getIntent();
userInput=newIntent.getStringExtra(“输入”);
//在后台线程上检索数据
新建loadBusStopDetails().execute(用户输入);
新建loadBusPredictions().execute(用户输入);
//为两个按钮设置onClickListner()
refreshButton.setOnClickListener(此);
saveButton.setOnClickListener(这个);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.save_按钮:
布尔didItWork=true;
试一试{
如果(stopList.size()!=0){
字符串stopId=null;
字符串stopName=null;
用于(公共汽车站:停止列表){
stopId=stop.getStopId();
stopName=stop.getStopName().toString();
}
Toast.makeText(MainBusStopByIdActivity.this,
stopId+“\n”+stopName,Toast.LENGTH\u LONG).show();
DatabaseAccess条目=新的DatabaseAccess(
MainBusStopByIdActivity.this);
entry.open();
entry.createEntry(stopId、stopName);
entry.close();
}否则{
Toast.makeText(MainBusStopByIdActivity.this,
“找不到公交车站信息!!!”,
Toast.LENGTH_LONG).show();
}
}捕获(例外e){
didItWork=假;
字符串错误=e.toString();
对话框d=新对话框(此对话框);
d、 setTitle(“Error!!!”;
TextView tv=新的TextView(此);
tv.setText(错误);
d、 设置内容视图(电视);
d、 show();
}最后{
如果(didItWork){
对话框d=新对话框(此对话框);
d、 setTitle(“收藏条目”);
TextView tv=新的TextView(此);
tv.setText(“SCU成功地将公交车站添加到收藏夹…”);
d、 设置内容视图(电视);
d、 show();
}
}
打破
案例R.id.refresh_按钮:
打破
}
}
//适配器类
私有类AdapterForBusStopById扩展BaseAdapter{
私有静态最终整数类型_预测=0;
专用静态最终整型\u总线停止\u头=1;
private ArrayList mData=new ArrayList();
私人停车场;
私有树集mseparatorset=新树集();
公共适配器ForBussTopById(){
mInflater=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
}
公共无效附加项(最终字符串项){
mData.add(项目);
notifyDataSetChanged();
}
公共void addSeparatorItem(最终字符串项){
mData.add(项目);
//保存分隔符位置
mSeparatorsSet.add(mData.size()-1);
notifyDataSetChanged();
}
@凌驾
public int getItemViewType(int位置){
返回MSEParatorSet.contains(位置)?类型\u总线停止\u标题
:类型_预测;
}
public int getCount(){
返回mData.size();
}
公共字符串getItem(int位置){
返回mData.get(位置);
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
int type=getItemViewType(位置);
System.out.println(“getView”+位置+“”+转换视图
+“类型=”+类型);
if(convertView==null){
holder=新的ViewHolder();
开关(类型){
病例类型预测:
convertView=mInflater.充气(R.layout.predictions,null);
holder.textView=(textView)convertView
.findViewById(R.id.predictionText);
打破
外壳类型\u总线停止\u标题:
convertView=mInflater.充气(右布局、公共汽车站、车头、,
无效);
holder.textView=(textView)convertView
.findViewById(R.id.textSeparator);
打破
}
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
返回视图;
}
}
公共静态类视图持有者{
公共文本视图文本视图;
}
//下载巴士站资料
//用于下载公共汽车站的AsyncTask类
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/holo_blue_light" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/tflImage"
        android:layout_width="50dp"
        android:layout_height="60dp"
        android:layout_gravity="center_vertical"
        android:layout_margin="2.5dp"
        android:background="@drawable/redtrain128"
        android:contentDescription="@string/hello_world" />

    <TextView
        android:id="@+id/textSeparator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:gravity="left"
        android:text="text"
        android:textColor="#FFFFFFFF"
        android:visibility="visible" />
</LinearLayout>


<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/holo_blue_light" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add to Favourites"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:onClick="onClick" />

    <Button
        android:id="@+id/refresh_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Refresh"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:onClick="onClick" />
</LinearLayout>



<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/holo_blue_light" />

<TextView
    android:id="@+id/textSeparator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:gravity="left"
    android:text="  ROUTE     TIME        DESTINATION"
    android:textColor="#FFFFFFFF"
    android:textSize="8pt"
    android:textStyle="bold"
    android:typeface="normal"
    android:visibility="visible" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/holo_blue_light" />

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:onClick="YOUR_CLICK_HANDLE"
android:background="@android:drawable/btn_default"
android:id="@+id/complexButtonLayout"
>
..
complex contents
...
</LinearLayout>