列表视图长按

时间:2014-04-03 10:41:13

标签: android

我编写了一个java类来展示listview扩展ListActivity超类的listview。我已经抓住了click event.now我想使用longclick event.i已经累了几次但是没有方法可以覆盖longclick event.pls帮我做。

这是我的java类

public class Viewservay extends ListActivity {

    private TextView listName;
    private DbWorker dbworkers;
    private HashMap<Integer, Integer> ListPosition;
    private Button button_exit;

    public static int id_item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewservay);

        dbworkers = new DbWorker(this);

        LoadProduct();
        button_exit= (Button)findViewById(R.id.button_exit);
        button_exit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                 Intent intent_home = new Intent(Viewservay.this,Home.class);
                 startActivity(intent_home);
                 finish();
            }
        });

    }

    public void LoadProduct() {

        ArrayList<HashMap<String, String>> itemlist = dbworkers.getAllMSlist();
        int cout = 0;
        ListPosition = new HashMap<Integer, Integer>();

        for (HashMap<String, String> hashMap : itemlist) {

            ListPosition.put(cout, Integer.parseInt(hashMap.get("id_ms")));
            System.out.println("sync status is:"+hashMap.get("sync"));
            // Log.i("Loop  Product list",String.valueOf(hashMap.get("id_ms")) +
            // ""+ String.valueOf(cout));
            cout++;

        }
        itemlist.size();

        SimpleAdapter adapter = new SimpleAdapter(this, itemlist,
                R.layout.list_view_item, new String[] { "Number", "outlet",
                        "date" }, new int[] { R.id.number, R.id.Name,
                        R.id.value }){

            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);

        //      HashMap<String, String> obj = (HashMap<String, String>) getListView().getAdapter().getItem(position);
                String sync="";
                int a =ListPosition.get(position);
                Cursor cursor = dbworkers.getMsByid(a);
                if (cursor.moveToFirst()) {
                     sync=cursor.getString(5);

                }
                cursor.close();
                if(sync.equalsIgnoreCase("0")){

                }
                return view;

            };
        };

        /***/
        setListAdapter(adapter);

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub

        super.onListItemClick(l, v, position, id);

        id_item = ListPosition.get(position);
        System.out.println(ListPosition.get(position));
        Intent intent_home = new Intent(this,
                ViewAndSync.class);
        startActivity(intent_home);
        finish();



    }



    @Override
    public void onBackPressed() {

    }
}

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Viewservay" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

    <Button
        android:id="@+id/button_exit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@android:id/list"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Exit" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

ListActivity有一个名为getListView()的方法。然后为相同的

设置监听器
public ListView getListView ()

Added in API level 1
Get the activity's list view widget.

您可以使用

ListView lv = getListView();

然后

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){ 
@Override 
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) 
{ 
     return true;
} 
}); 

答案 1 :(得分:0)

您需要使用以下内容添加长按一下监听器

getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
              long arg3) {

        // do your stuff here

        return true;
    }
});