1

在我的应用程序中,我有一个布局,它在 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();
        }

    }
}

}

我的 XML 代码如下:

<?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>

我还找到了有关此问题的以下链接,但我不知道如何在我的情况下实施。请看一下,可能你有什么想法,链接在这里: 链接点击这里

谢谢...

4

1 回答 1

2

将包含 ListView 的父布局的android:descendantFocusability属性设置为值blocksDescendants,例如:

<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>

编辑

我采取的另一种方法更方便恕我直言,支持复杂的内容并在 API 10 及更高版本上进行了测试:

<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>

您可以通过句柄使用(托管视图的活动实现 public void YOUR_CLICK_HANDLE(View view); 方法)或以编程方式注册 onclick 处理程序。

但是,我使用 Fragments 中的视图。

于 2013-04-09T12:00:10.770 回答