1

私のアプリケーションでは、ListActivity のヘッダーにバス停の詳細を表示し、利用可能なすべての到着予測をリストとして表示するレイアウトがあります。リスト ヘッダーには、バス停をデータベースに保存するためのボタンと、アクティビティを更新するためのボタンの 2 つのボタンがあります。しかし、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 ハンドラーを登録できます。

ただし、フラグメントのビューを使用します。

于 2013-04-09T12:00:10.770 に答える