0

我改变了我最初的活动,SherlockActivity以便从 ABS 的所有优势中受益。为此,我使用ListAdapterAnSco extends BaseAdapter. 现在我想onclick管理我onLongClick的. 我使用了几种组合,但我做不到。getViewListAdapter

注意:我使用SQLiteCursor来获取物品

原始 OnItemClickListener 代码


public void onItemClick(AdapterView<?>  parent, View v, int position,
                    long id) {
                Intent intent = new Intent(getApplicationContext(),ListPeriode.class);

                SQLiteCursor cr=(SQLiteCursor)parent.getItemAtPosition(position);
                String ansco=cr.getString(cr.getColumnIndex(DatabaseHelper.ANSCO));
                AnSco an=new AnSco(ansco);
                an.setIdan((int)id);

                Toast.makeText(getApplicationContext(), "Année Scolaire: "+an.getAnsco(), Toast.LENGTH_LONG).show();
                intent.putExtra("ANSCO_ID", an.getIdan());
                intent.putExtra("ANSCO_AN", an.getAnsco());
                cr.close();
                startActivity(intent);
                finish();
            }

如何在 OnClick 方法中更改此代码以完成相同的工作?

我的getView


public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.annee_sco_item, null);
            // Locate the TextViews in listview_item.xml
            holder.an = (TextView) view.findViewById(R.id.ansco_item);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.an.setText(listAnsco.get(position).getAnsco());

        // Listen for ListView Item Click
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


            }
        });

        return view;
    }
4

2 回答 2

0

将 OnClickListener 提取到单独的类并再次传递

MyClickListener.java

 public class MyClickListener implements onItemClickListener
 {
     @override
     public void onItemClick(AdapterView<?>  parent, View v, int position, long id)
     {
         Intent intent = new Intent(getApplicationContext(),ListPeriode.class);
         SQLiteCursor cr=(SQLiteCursor)parent.getItemAtPosition(position);
         String ansco=cr.getString(cr.getColumnIndex(DatabaseHelper.ANSCO));
         AnSco an=new AnSco(ansco);
         an.setIdan((int)id);
         Toast.makeText(getApplicationContext(), "Année Scolaire:"+an.getAnsco(), Toast.LENGTH_LONG).show();
         intent.putExtra("ANSCO_ID", an.getIdan());
         intent.putExtra("ANSCO_AN", an.getAnsco());
         cr.close();
         startActivity(intent);
         finish();
     }
}

在您的适配器中删除

   // Listen for ListView Item Click
    view.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View arg0) {
            }
        });

你的活动.java

@override
onCraete(Bundle x)
{
    ListView listView = (ListView) findViewById(R.i.your_list_id);
    listView.setAdapter(new YourAdapter()); 
    listView.setOnItemClickListener(new MyClickListener());
}
于 2013-10-29T12:39:46.460 回答
0

您可以使用 :

        holder.an.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

          Intent intent = new Intent(contetx,ListPeriode.class);

            SQLiteCursor cr=(SQLiteCursor)parent.getItemAtPosition(position);
            String ansco=cr.getString(cr.getColumnIndex(DatabaseHelper.ANSCO));
            AnSco an=new AnSco(ansco);
            an.setIdan((int)id);

            Toast.makeText(context, "Année Scolaire: "+an.getAnsco(), Toast.LENGTH_LONG).show();
            intent.putExtra("ANSCO_ID", an.getIdan());
            intent.putExtra("ANSCO_AN", an.getAnsco());
            cr.close();
            startActivity(intent);
            finish();

        }
    });

并且必须将上下文传递给您扩展 BaseAdapter 的类

于 2013-10-29T13:17:06.063 回答