0

ListViewアイテム クリック イベントに対して次のコードを記述しましたが、アプリケーションを実行してアイテムをクリックしても何もアクションがありません。なぜこうなった?

    public class ListingActivity extends Activity implements OnClickListener{
    private ListView list;
    private Button sortBtn,filter;
    double destLat,destLng;
    AlertDialog alert;
    MyAdabpter adapter;

    ArrayList<HashMap<String,String>> filteredList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewer);
        list = (ListView) findViewById(R.id.list);
        sortBtn = (Button) findViewById(R.id.sort_btn);
        filter = (Button) findViewById(R.id.filter_btn);
    //      ListAdapter adapter = new SimpleAdapter(this, FetchXMLData.mylist , R.layout.list_item, 
    //              new String[] { "Name", "Vicinity","Distance","Rating"}, 
    //              new int[] { R.id.name, R.id.address,R.id.distance,R.id.rating });
        adapter = new MyAdabpter(this);

        list.setAdapter(adapter);
        list.setTextFilterEnabled(true);
        list.setEnabled(true);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                destLat = Double.parseDouble(FetchXMLData.mylist.get(position).get("Latitude"));
                destLng = Double.parseDouble( FetchXMLData.mylist.get(position).get("Longitude"));
                Intent intent = new Intent(Intent.ACTION_VIEW, 
                        Uri.parse("http://maps.google.com/maps?saddr=" + LocationSearch.latitude + "," + LocationSearch.longitude + 
                                "&daddr=" + destLat + "," + destLng));
                    intent.setComponent(new ComponentName("com.google.android.apps.maps", 
                        "com.google.android.maps.MapsActivity"));
                    startActivity(intent);

            }
        });

        sortBtn.setOnClickListener(this);
        filter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.sort_btn:
            showSortList();
            break;

        case R.id.filter_btn:
            showFilterList();
            break;
        default:
            break;
        }

    }
    protected void showSortList() {
        // TODO Auto-generated method stub
        final CharSequence[] items = {"Rating", "Distance"};
        AlertDialog.Builder builder = new AlertDialog.Builder(ListingActivity.this);
        builder.setTitle("Sort by");
        FetchXMLData.mylist = FetchXMLData.templist;
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Collections.sort(FetchXMLData.mylist,Collections.reverseOrder(new MyComparator(items[item].toString())));
                dialog.dismiss();
                adapter.notifyDataSetChanged();
            }
        });
        alert = builder.create();
        alert.show();
    }
    protected void showFilterByRating() {
        // TODO Auto-generated method stub
        final CharSequence[] items = {"only 5 stars", "only 4 stars","only 3 stars", "only 2 stars","only 1 star"};

        AlertDialog.Builder builder = new AlertDialog.Builder(ListingActivity.this);
        builder.setTitle("Filter by Rating");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                dialog.dismiss();
                FetchXMLData.mylist = FetchXMLData.templist;
                filteredList = new ArrayList<HashMap<String,String>>();
                if(item == 0 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(!FetchXMLData.mylist.get(i).get("Rating").equals(""))
                        {
                            if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Rating").toString()) == 5)
                            {
                                filteredList.add(FetchXMLData.mylist.get(i));
                            }
                        }
                    }
                    FetchXMLData.mylist = filteredList;
                    adapter.notifyDataSetChanged();
                }
                else if(item == 1 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(!FetchXMLData.mylist.get(i).get("Rating").equals(""))
                        {
                            if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Rating")) == 4.0 )
                            {
                                filteredList.add(FetchXMLData.mylist.get(i));
                            }
                        }
                    }
                    FetchXMLData.mylist = filteredList;
                    adapter.notifyDataSetChanged();
                }
                else if(item == 2 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(!FetchXMLData.mylist.get(i).get("Rating").equals(""))
                        {
                            if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Rating")) == 3.0 )
                            {
                                filteredList.add(FetchXMLData.mylist.get(i));
                            }
                        }
                    }

                }
                else if(item == 3 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(!FetchXMLData.mylist.get(i).get("Rating").equals(""))
                        {
                            if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Rating")) == 2.0 )
                            {
                                filteredList.add(FetchXMLData.mylist.get(i));
                            }
                        }
                    }

                }
                else if(item == 4 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(!FetchXMLData.mylist.get(i).get("Rating").equals(""))
                        {
                            if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Rating")) == 1.0 )
                            {
                                filteredList.add(FetchXMLData.mylist.get(i));
                            }
                        }
                    }

                }
                if(filteredList.size() > 0)
                {
                    FetchXMLData.mylist = filteredList;
                    adapter.notifyDataSetChanged();
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Sorry,no data found", Toast.LENGTH_SHORT).show();
                }
            }
        });
        alert = builder.create();
        alert.show();
    }
    protected void showFilterList() {
        // TODO Auto-generated method stub
        final CharSequence[] items = {"By Rating", "By Distance", "By Name"};

        AlertDialog.Builder builder = new AlertDialog.Builder(ListingActivity.this);
        builder.setTitle("Filter by");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                dialog.dismiss();
                FetchXMLData.mylist = FetchXMLData.templist;
                if(items[item].equals("By Rating"))
                {
                    showFilterByRating();
                }
                else if(items[item].equals("By Distance"))
                {
                    showFilterByDistance();
                }
                else if(items[item].equals("By Name"))
                {
                    //showFilterByName();
                }

            }
        });
        alert = builder.create();
        alert.show();
    }
    protected void showFilterByDistance() {
        // TODO Auto-generated method stub
        final CharSequence[] items = {"between 0-2km", "between 2-4km","between 4-5km"};

        AlertDialog.Builder builder = new AlertDialog.Builder(ListingActivity.this);
        builder.setTitle("Filter by Distance");
        FetchXMLData.mylist = FetchXMLData.templist;
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                dialog.dismiss();
                filteredList = new ArrayList<HashMap<String,String>>();
                if(item == 0 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Distance")) > 0 && Float.parseFloat(FetchXMLData.mylist.get(i).get("Distance")) <= 2  )
                        {
                            filteredList.add(FetchXMLData.mylist.get(i));
                        }
                    }

                }
                else if(item == 1 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Distance")) > 2 && Float.parseFloat(FetchXMLData.mylist.get(i).get("Distance")) <= 4 )
                        {
                            filteredList.add(FetchXMLData.mylist.get(i));
                        }
                    }

                }
                else if(item == 2 && FetchXMLData.mylist.size() > 0)
                {
                    for(int i = 0; i < FetchXMLData.mylist.size(); i++)
                    {
                        if(Float.parseFloat(FetchXMLData.mylist.get(i).get("Distance")) > 4 && Float.parseFloat(FetchXMLData.mylist.get(i).get("Distance")) <=5  )
                        {
                            filteredList.add(FetchXMLData.mylist.get(i));
                        }
                    }

                }
                if(filteredList.size() > 0)
                {
                    FetchXMLData.mylist = filteredList;
                    adapter.notifyDataSetChanged();
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Sorry,no data found", Toast.LENGTH_SHORT).show();
                }
            }
        });
        alert = builder.create();
        alert.show();
    }
    protected void showFilterByName() {
        // TODO Auto-generated method stub
        final CharSequence[] items = {"between 0-2km", "between 2-4km","between 4-5km"};
        FetchXMLData.mylist = FetchXMLData.templist;
        AlertDialog.Builder builder = new AlertDialog.Builder(ListingActivity.this);
        builder.setTitle("Enter key word");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });
        alert = builder.create();
        alert.show();
    }




}

以下は、私のレイアウトファイルですListView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:dividerHeight="5dp" >
</ListView>

<RelativeLayout
    android:id="@+id/footer_menu"
    style="@android:color/transparent"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:orientation="vertical" >

    <Button
        android:id="@+id/sort_btn"
        android:layout_width="160dp"
        android:layout_height="fill_parent"
        android:background="@drawable/footer_button"
        android:text="Sort"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

    <Button
        android:id="@+id/filter_btn"
        android:layout_width="160dp"
        android:layout_height="fill_parent"
        android:layout_toRightOf="@+id/sort_btn"
        android:background="@drawable/footer_button"
        android:text="Filter"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />
</RelativeLayout>

</RelativeLayout>
4

2 に答える 2

0

あなたはこのようなことをしたいです:

public class YourActivity extends ListActivity implements OnItemClickListener{

    @Override
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.your_layout);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // Do your thing here
    }
}
于 2012-05-05T07:41:24.900 に答える
0
 listView.setOnItemClickListener(new OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position,
               long id) {

           String item = values[position];

           Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

       }
   });
于 2012-05-04T10:37:41.520 に答える