如何为Android自定义ListView的每一行添加项侦听器?

时间:2014-06-17 03:03:46

标签: android listview android-custom-view

我创建了一个自定义列表视图,我想为每一行添加一个onItemClick侦听器,但不知道怎么做,没有任何问题, 这是代码。我尝试通过手动创建ListView的单独对象,然后添加OnitemClick Listener但它不起作用。

<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:background="@drawable/bg_main"
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="com.saadreviews.health_plus.Calories_Activity$PlaceholderFragment" >

<TextView 
    android:id="@+id/txtv_calories"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:layout_marginTop="3dp"
    android:text="Total Calories = "
    android:textSize="21sp"
    android:textColor="#006600"
/>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtv_calories"
    android:layout_marginTop="5dp"
    android:background="@drawable/line" />

 <ListView
     android:id="@android:id/list"
     android:layout_width="match_parent"
     android:layout_height="317dp"
     android:layout_alignLeft="@+id/imageView1"
     android:layout_below="@+id/imageView1"
     android:layout_marginTop="14dp"
     android:background="@drawable/bg_list" >

</ListView>

 <ImageView
     android:id="@+id/bttn_reset"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"
     android:onClick="reset"
     android:background="@drawable/custom_reset" />

这是javaFile。

public class Calories_Activity extends ListActivity {

ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calories);
this.getActionBar().hide();
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.textView_food, getResources().getStringArray(R.array.food)));

setListAdapter(new MyAdapter(this, 
                android.R.layout.simple_list_item_1, R.id.textView_calories,
                getResources().getStringArray(R.array.calories)));

    }

    public void reset(View view)
{
            Toast.makeText(getBaseContext(),
            "Reset", Toast.LENGTH_SHORT).show();
}


    private class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] strings) {
            super(context, resource, textViewResourceId, strings);
            // TODO Auto-generated constructor stub
        }


        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row=inflater.inflate(R.layout.list_item, parent, false);
            String[] items = getResources().getStringArray(R.array.food);
            String[] calo = getResources().getStringArray(R.array.calories);

            ImageView iv= (ImageView) row.findViewById(R.id.imageView1);
            TextView tv = (TextView) row.findViewById(R.id.textView_food);
            TextView tv1 = (TextView) row.findViewById(R.id.textView_calories);

            tv.setText(items[position]);
            tv1.setText(calo[position]);

            if(items[position].equals("Apple"))
            {
                iv.setImageResource(R.drawable.apple);
            }
            else if(items[position].equals("Burger"))
            {
                iv.setImageResource(R.drawable.burger);
            }
            else if(items[position].equals("Mango"))
            {
                iv.setImageResource(R.drawable.mango);
            }
            else if(items[position].equals("Orange"))
            {
                iv.setImageResource(R.drawable.orange);
            }
            else if(items[position].equals("Pizza"))
            {
                iv.setImageResource(R.drawable.pizza);
            }


            return row;
        }



    }

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.calories_, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Intent i2 = new Intent(Calories_Activity.this, Father_Activity.class);
        startActivity(i2);

    }

}

3 个答案:

答案 0 :(得分:0)

您可以在onListItemClick中覆盖Calories_Activity。根据职位,你可以做必要的事情

protected void onListItemClick (ListView l, View v, int position, long id)

Added in API level 1
This method will be called when an item in the list is selected. Subclasses should override. Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
l   The ListView where the click happened
v   The view that was clicked within the ListView
position    The position of the view in the list
id  The row id of the item that was clicked

答案 1 :(得分:0)

当您使用ListActivity时,您可以覆盖onListItemClick方法:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {}'

答案 2 :(得分:0)

你可以做iv..setonclickListener(新的OnClicklisten); //点击图片上的事件......或者

listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Toast.makeText(getApplicationContext(), "Clciked", Toast.LENGTH_SHORT).show();
        }
    });

但有时setonitemclick在Main类中不起作用所以你需要在适配器类中添加listen