2013-02-15 21 views
10

से क्लिक किया गया आइटम प्राप्त करें मुझे एक समस्या है। मेरी एक्सएमएल फ़ाइल में संशोधन से पहले, मेरी सूचीदृश्य पूरी तरह से काम करने में सक्षम था। लेकिन अब, एक्सएमएल में कुछ संशोधनों के बाद, यह ठीक से काम नहीं कर रहा है। मेरी सूचीदृश्य कस्टम है। इसलिए, मैंने सूचीदृश्य में प्रत्येक पंक्ति को प्रस्तुत करने के लिए अलग XML बनाया है।सूचीदृश्य

मेरे एकल 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 कोड जहां सूचीदृश्य रहता है:

<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> 

और इसके लिए मेरी जावा कोड:

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()+""; 

किसी भी रिकॉर्ड की सूचीदृश्य, यह पहली रिकॉर्ड केवल का नाम देता है: कि, जब मैं नाम इस का उपयोग कर पाने के लिए है। उदाहरण के लिए, यदि मेरी पहली पंक्ति में "गेमिंग" नामक टेक्स्टव्यू है, तो जब मैं किसी भी पंक्ति पर क्लिक करता हूं, (डीबगिंग उद्देश्य के लिए, मैंने Toast.makeText(...))टोस्ट "गेमिंग" सूचीदृश्य में प्रत्येक रिकॉर्ड के नाम के रूप में, हालांकि प्रत्येक रिकॉर्ड में सूचीदृश्य अद्वितीय है। मुझे इस के साथ मदद कृपया।

+1

'arg0' अपने ListView ... arg1' जो आपके क्लिक किया पंक्ति ... – Selvin

+1

... है वैसे भी उपयोग करें' केवल क्लिक किए गए जा रहा आइटम के लिए उपयोग करने के लिए 'बेहतर है स्ट्रिंग नाम = (स्ट्रिंग) agr0.getAdapter()। getItem (arg2); ' – Selvin

+0

@ सेल्विन आप सही हैं। मैं समझ गया। की सराहना की। –

उत्तर

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

अच्छा .. उत्तर लेकिन विभिन्न आइटम कैसे प्राप्त करें ITemClick पर ईवेंट क्लिक करें। मेरी सूचीदृश्य में 5 आइटम हैं जो मैं अलग-अलग आइटम सिल्क पर ईवेंट करना चाहता हूं .. धन्यवाद –

2

आप TextView पर onClickListener चाहते हैं केवल, इसके बेहतर एडाप्टर अपने आप में onClickListener परिभाषित करने के लिए। जैसे

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

आपकी विधि भी सराहनीय है। लेकिन मुझे श्रोता को पूरी सूची रिकॉर्ड पर ही सूचित करने की आवश्यकता थी, न केवल इसके अंदर टेक्स्टव्यू। आपकी राय भी सच है, कुछ मामलों में, सहमत ... –

1
से

अलावा समाधान से ऊपर, यह मेरे लिए काम किया मैं एक ऐसी ही स्थिति का सामना करना पड़ रहा था, मैं इसे हल करने में सक्षम के बाद मैं एक्सएमएल में मेरी सूचीदृश्य को निम्नलिखित

android:descendantFocusability="blocksDescendants"

जोड़ा था। यह अब काम कर रहा है। असली उत्सुक उपयोगकर्ता के लिए यहाँ कि http://developer.android.com/reference/android/R.attr.html#descendantFocusability

+1

अच्छा वैकल्पिक तरीका, इस बारे में नहीं पता था। मैं सूची दृश्य और उसके बच्चे के साथ बहुत संघर्ष कर रहा था। – Deb

+0

खुशी है कि यह आपको मदद मिली –

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

अच्छा उदाहरण, मेरा समय बचाया! –

0

बारे में अधिक जानकारी नहीं है, इसलिए मैं सिर्फ बयान करता है, तो अपने को जोड़ सकते हैं और अलग अलग क्रियाएं यहाँ टिप्पणी करने के लिए मैं SKK के जवाब पर अपने प्रश्न पर टिप्पणी कर रहा हूँ के लिए पर्याप्त नहीं प्रतिष्ठा है। ..

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();}} 


}});