2013-02-15 128 views
10

我遇到了问题。在修改我的XML文件之前,我的listview能够完美地工作。但是现在,在xml中进行了一些修改之后,它不能正常工作。 我的listview是自定义的。所以,我创建了单独的xml来呈现listview中的每一行。从列表视图中获得点击的项目

我的单row.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:id="@+id/relativeLayoutSingleRowManageAddictions" 
    android:layout_height="40dp" 
    android:background="@drawable/box_midbg" > 

    <TextView 
     android:id="@+id/textViewSingleRowManageAddictions" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="17dp" 
     android:text="TextView" 
     android:textSize="20dp" 
     android:textColor="#ffffff" /> 

    <ImageView 
     android:id="@+id/imageViewSingleRowManageAddictions" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="18dp" 
     android:src="@drawable/listing_arrow" /> 

</RelativeLayout> 

main.xml中,其中的ListView所在代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/main_bg_edited" > 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     style="@style/top_bar_style" > 

     <ImageView 
      android:id="@+id/imageViewMAnageAddictionsBack" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:src="@drawable/back_arrow" 
      android:clickable="true" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@style/header_style" 
      android:text="Manage Addictions" /> 

     <ImageView 
      android:id="@+id/imageViewManageAddictionsAdd" 
      android:layout_width="25dp" 
      android:layout_height="20dp" 
      android:layout_marginRight="3dp" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:src="@drawable/plus_nav" 
      android:clickable="true" /> 

    </RelativeLayout> 

    <ListView 
     android:id="@+id/listViewManageAddictions" 
     android:layout_width="290dp" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/relativeLayout1" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:layout_marginTop="12dp" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="2dp" /> 

</RelativeLayout> 

并为它我的Java代码:

package com.addictioncounterapp; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 

public class ManageAddictionList extends Activity { 
    ImageView iv_manage_addictions_back, iv_manage_addictions_add; 
    ListView listview; 
    ArrayList <String> arraylist_manage_addiction; 
    ArrayAdapter <String> arrayadapter_manage_addiction; 
    SQLiteDatabase database; 

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

    loaddatabase(); 

    iv_manage_addictions_back = (ImageView) findViewById(R.id.imageViewMAnageAddictionsBack); 
    iv_manage_addictions_back.setClickable(true); 
    iv_manage_addictions_back.setOnClickListener(new OnClickListener() {@Override 
     public void onClick(View v) { 
     startActivity(new Intent(ManageAddictionList.this, Settings.class)); 
     } 
    }); 

    iv_manage_addictions_add = (ImageView) findViewById(R.id.imageViewManageAddictionsAdd); 
    iv_manage_addictions_add.setClickable(true); 
    iv_manage_addictions_add.setOnClickListener(new OnClickListener() {@Override 
     public void onClick(View v) { 
     Intent intent = new Intent(ManageAddictionList.this, MainActivity.class); 
     intent.putExtra("name", ""); 
     intent.putExtra("unit", ""); 
     intent.putExtra("attribute", ""); 
     intent.putExtra("limit", ""); 
     intent.putExtra("operation", "Add Addiction"); 
     startActivity(intent); 
     } 
    }); 


    arraylist_manage_addiction = new ArrayList <String>(); 
    manageList(); 
    listview = (ListView) findViewById(R.id.listViewManageAddictions); 

    if (arraylist_manage_addiction.isEmpty()) Toast.makeText(getBaseContext(), "No Addictions found to manage. Click on 'add' button to create new Addiction.", Toast.LENGTH_SHORT) 
     .show(); 
    else listview.setAdapter(arrayadapter_manage_addiction); 

    listview.setOnItemClickListener(new OnItemClickListener() {@Override 
     public void onItemClick(AdapterView <? > arg0, View arg1, int arg2, long arg3) { 
     String name = null, attribute = null, unit = null, limit = null; 

     View parentView = (View) arg0.getParent(); 
     name = ((TextView) parentView.findViewById(R.id.textViewSingleRowManageAddictions)) 
      .getText() + ""; 

     Toast.makeText(getBaseContext(), name, Toast.LENGTH_SHORT) 
      .show(); 

     int cat_id = 0; 

     //--------Fetching cat_id through name from the list-------- 

     Cursor cursor; 

     cursor = database.query("category", new String[] { 
      "cat_id" 
     }, new String("cat_name=?"), new String[] { 
      name 
     }, null, null, null); 

     if (cursor.getCount() > 0) { 
      while (cursor.moveToNext()) 
      cat_id = cursor.getInt(0); 
      cursor.close(); 
     } 

     //--------Fetching unit, attribute, limit through cat_id from the list-------- 

     cursor = database.query("category_attribute", new String[] { 
      "cat_attribute_name", "cat_attribute_unit", "cat_limit" 
     }, new String("cat_id=?"), new String[] { 
      cat_id + "" 
     }, null, null, null); 

     if (cursor.getCount() > 0) { 
      while (cursor.moveToNext()) { 
      attribute = cursor.getString(0); 
      unit = cursor.getString(1); 
      limit = cursor.getString(2); 
      } 
      cursor.close(); 
     } 

     Intent intent = new Intent(ManageAddictionList.this, MainActivity.class); 
     intent.putExtra("name", name); 
     intent.putExtra("unit", unit); 
     intent.putExtra("attribute", attribute); 
     intent.putExtra("limit", limit); 
     intent.putExtra("cat_id", cat_id); 
     intent.putExtra("operation", "Edit Addiction"); 
     startActivity(intent); 
     } 
    }); 
    } 

    private void loaddatabase() { 
    database = openOrCreateDatabase("AddictionCounter.db", SQLiteDatabase.OPEN_READONLY, null); 
    } 

    private void manageList() { 
    String[] columns = { 
     "cat_name" 
    }; 
    Cursor cursor; 

    cursor = database.query("category", columns, null, null, null, null, null); 

    if (cursor.getCount() > 0) { 
     while (cursor.moveToNext()) 
     arraylist_manage_addiction.add(cursor.getString(0)); 
     cursor.close(); 
    } 

    arrayadapter_manage_addiction = new ArrayAdapter <String> (this, R.layout.single_row_manage_addictions, R.id.textViewSingleRowManageAddictions, arraylist_manage_addiction); 
    } 
} 

这背后的主要错误是,当我得到这个名字使用这个:

View parentView = (View) arg0.getParent(); 
name = ((TextView) parentView.findViewById(R.id.textViewSingleRowManageAddictions)).getText()+""; 

listview任何记录,它只给出第一个记录的名称。 例如,如果我的第一行有textview命名为“游戏”,当我点击任何行,(为了调试目的,我用Toast.makeText(...))Toasts“Gaming”作为listview中每个记录的名称,在ListView是唯一的。请如果你想在TextViewonClickListener而已,它能够更好地定义适配器本身onClickListener帮我出这一点。

+1

'arg0'是你的ListView ...用'arg1'这是你的点击一行... – Selvin 2013-02-15 09:01:18

+1

...反正只获得点击的产品更好地使用'字符串名称=(字符串) agr0.getAdapter()。getItem(arg2);' – Selvin 2013-02-15 09:19:33

+0

@Selvin你是对的。我知道了。赞赏。 – 2013-02-15 10:36:54

回答

46
lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     // selected item 
     String selected = ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString(); 

     Toast toast = Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
}); 
+0

尼斯..答案,但如何获得onItemClick不同的项目点击事件。在我的列表视图中有5项我希望在不同的项目上执行事件cilck ..谢谢 – 2017-05-01 13:29:15

2

。像

yourTextView.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     // showToast() 
     } 
}); 
+1

你的方法也是可观的。但我需要暗示整个列表记录本身的听众,而不仅仅是其中的textview。你的意见也是如此,在某些情况下,同意... – 2013-02-15 10:32:13

1

除了上面的解决方案,这对我工作我正面临类似的情况,我能解决它添加以下

android:descendantFocusability="blocksDescendants"

后,我在XML列表视图。它正在工作。要真正好奇的用户这里有关于 http://developer.android.com/reference/android/R.attr.html#descendantFocusability

+1

很好的替代方式,不知道这个。我在列表视图和它的孩子身上挣扎不已。 – Deb 2015-02-17 14:21:07

+0

很高兴帮助你 – 2015-02-18 03:01:13

3
listView = (ListView)view.findViewById(R.id.list); 
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     String selected = ((TextView) view.findViewById(R.id.complaint)).getText().toString(); 
     Toast.makeText(context,selected,Toast.LENGTH_LONG).show(); 
    } 
}); 
+0

良好的例子,节省了我的时间! – 2017-05-25 03:27:34

0

更多信息,因为我没有足够的信誉评论我在这里评论的SKK的回答你的问题只要增加if语句会向您和执行不同的操作。 ..

lv.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    // selected item 
    String selected = ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString(); 

    if(selected.equals("Your comparing string, like Games"){            

    Toast toast = Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT); 
    toast.show(); }                             

    else {            
    if(selected.equals("Your comparing string, like Games")){            
    Toast toast = Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT); 
    toast.show();}} 


}});