2011-04-02 22 views
20

पर फायरिंग नहीं है मेरे पास Activity है जो वेब सेवा से डेटा पुनर्प्राप्त करता है। यह डेटा ListView में एक ArrayAdapter के माध्यम से प्रस्तुत किया गया है जो RelativeLayout को तीन TextViews के अंदर बढ़ाता है, कुछ भी फैंसी नहीं है और यह ठीक काम करता है।onItemClickListener कस्टम ArrayAdapter

अब मैं एक विवरण Activity लागू करना चाहता हूं जिसे किसी उपयोगकर्ता द्वारा ListView में किसी आइटम पर क्लिक करने पर कॉल किया जाना चाहिए, आसान लगता है लेकिन मैं अपने जीवन के लिए अपने ArrayAdapter पर काम करने के लिए ItemClickListener को प्राप्त नहीं कर सकता।

यह मेरा मुख्य Activity:

public class Schema extends Activity { 

    private ArrayList<Lesson> lessons = new ArrayList<Lesson>(); 
    private static final String TAG = "Schema"; 
    ListView lstLessons; 
    Integer lessonId; 

    // called when the activity is first created. 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     // can we use the custom titlebar? 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

     // set the view 
     setContentView(R.layout.main); 

     // set the title 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); 

     // listview called lstLessons 
     lstLessons = (ListView)findViewById(R.id.lstLessons); 

     // load the schema 
     new loadSchema().execute(); 

     // set the click listeners 
     lstLessons.setOnItemClickListener(selectLesson);  

    }// onCreate 

// declare an OnItemClickListener for the AdapterArray (this doesn't work) 
private OnItemClickListener selectLesson = new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int i, long l) { 
     Log.v(TAG, "onItemClick fired!"); 
    }     
}; 

private class loadSchema extends AsyncTask<Void, Void, Void> { 

    private ProgressDialog progressDialog; 

    // ui calling possible 
    protected void onPreExecute() { 
     progressDialog = ProgressDialog.show(Schema.this,"", "Please wait...", true); 
    } 

    // no ui from this one 
    @Override 
    protected Void doInBackground(Void... arg0) { 
    // get some JSON, this works fine 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     progressDialog.dismiss(); 
     // apply to list adapter 
     lstLessons.setAdapter(new LessonListAdapter(Schema.this, R.layout.list_item, lessons));   
    } 

मेरे ArrayAdapter कोड:

// custom ArrayAdapter for Lessons 
private class LessonListAdapter extends ArrayAdapter<Lesson> { 
    private ArrayList<Lesson> lessons; 

    public LessonListAdapter(Context context, int textViewResourceId, ArrayList<Lesson> items) { 
     super(context, textViewResourceId, items); 
     this.lessons = items; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.list_item, null); 
     } 
     Lesson o = lessons.get(position); 

     TextView tt = (TextView) v.findViewById(R.id.titletext); 
     TextView bt = (TextView) v.findViewById(R.id.timestarttext); 
     TextView rt = (TextView) v.findViewById(R.id.roomtext); 

     v.setClickable(true); 
     v.setFocusable(true); 

     tt.setText(o.title); 
     bt.setText(o.fmt_time_start); 
     rt.setText(o.room); 
     return v; 
    } 
}// LessonListAdapter 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/main" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" android:layout_width="fill_parent" 
    android:screenOrientation="portrait" 
    > 

    <!-- student name --> 
    <TextView 
     android:id="@+id/schema_view_student" 
     android:text="Name" android:padding="4dip" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:gravity="center_vertical|center_horizontal" 
     style="@style/schema_view_student" 
    /> 

    <!-- date for schema -->  
    <TextView 
     android:id="@+id/schema_view_title" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dip" 
     style="@style/schema_view_day" 
     android:gravity="center_vertical|center_horizontal" 
     android:layout_below="@+id/schema_view_student"   
     android:text="Date" android:padding="6dip" 
     android:layout_width="fill_parent" 
    /> 

    <!-- horizontal line --> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dip" 
     android:background="#55000000" 
     android:layout_below="@+id/schema_view_title" 
    /> 

    <!-- list of lessons --> 
    <ListView 
     android:id="@+id/lstLessons" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@+id/schema_view_title" 
    /> 

</RelativeLayout> 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="60px" 
    android:padding="12dip"> 

    <TextView 
     android:id="@+id/timestarttext" 
     android:text="09:45" 
     style="@style/LessonTimeStartText" 

     android:layout_width="60dip" 

     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 

     android:layout_height="fill_parent" android:gravity="center_vertical|right" android:paddingRight="6dip"/> 

    <TextView 
     android:id="@+id/titletext" 
     android:text="Test" 
     style="@style/LessonTitleText" 

     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 

     android:layout_toRightOf="@+id/timestarttext" 

     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" android:gravity="center_vertical|center_horizontal"/> 

    <TextView 
     android:id="@+id/roomtext" 
     android:text="123" 

     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 

     style="@style/LessonRoomText" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:gravity="center_vertical" /> 

</RelativeLayout> 

पिछले कुछ घंटों के लिए इसके साथ गड़बड़ कर रहा था और मुझे समस्या का सामना करने के बारे में मेरा सिर नहीं लग रहा है। मेरी समस्या this question के समान दिखती है, लेकिन मैं ListActivity को विस्तारित नहीं कर रहा हूं, इसलिए मुझे अभी भी पता नहीं है कि मेरा onListClickItem() कहाँ जाना चाहिए।

अद्यतन: अब मैंने कई दिनों से इस के साथ परेशान किया है और अभी भी समस्या नहीं मिल रही है।

क्या मुझे गतिविधि को फिर से लिखना चाहिए, इस बार गतिविधि की बजाय ListActivity को विस्तारित करना चाहिए? क्योंकि यह onItemClick विधि स्वयं प्रदान करता है और शायद ओवरराइट करना आसान है।

या, क्या मुझे अपने ArrayAdapter में प्रत्येक getView() में सीधे श्रोता को बांधना चाहिए? मेरा मानना ​​है कि मैंने पढ़ा है कि यह बुरा अभ्यास है (मुझे ऐसा करना चाहिए जैसा मैंने कोशिश की और मेरी पोस्ट में असफल रहा)।

+0

मैं इसे समझ नहीं इन दो चीजों में से कोई भी मुझे मदद की: http: // stackoverflow .com/प्रश्न/18237218/एंड्रॉइड-लिस्टव्यू-ऑनिटमक्लिकलिस्टर-अवरुद्ध-द्वारा-प्रोग्रेसबार –

उत्तर

48

बग मिला - ऐसा लगता है कि यह this issue है। List_item.xml तत्वों में से प्रत्येक को android:focusable="false" जोड़ने से समस्या हल हो गई है, और ऑनक्लिक अब मूल कोड से ट्रिगर किया गया है।

+4

यह आपके समाधान (जो काम करता है) खोजने के लिए मुझे एक घंटे से अधिक समय लगा। एंड्रॉइड मंच शानदार हो सकता है, लेकिन यह उनके हिस्से पर कुल विफलता है। –

+0

यह इतना अजीब है कि काम किया, यह एक बग deffo है। –

+0

ट्रैकबॉल/डीपैड मुद्दे की तरह लगता है। लेकिन धन्यवाद, इसने इसे हल किया। – Meet

0
protected void onPostExecute(Void result) { 
    progressDialog.dismiss(); stLessons.setAdapter(new LessonListAdapter(Schema.this, R.layout.list_item, lessons)); 
    //add this 
ListView lv = getListView(); lv.setOnItemClickListener(new ListView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> a, View v, int i, long l) { 
        //do stuff 
       } 
      }); 
    } 
+0

"विधि getListView() को Schema.loadSchema प्रकार के लिए अपरिभाषित किया गया है" - lv lessLessons के समान नहीं होगा (lstLessons वैसे भी है ListView)? – cvaldemar

+0

मुझे लगता है कि ऐसा इसलिए हो सकता है क्योंकि स्कीमा एक सूची गतिविधि को विस्तारित नहीं कर रही है। – cvaldemar

4

एक बार मुझे एक ही समस्या थी। प्रत्येक सूची आइटम में एक टेक्स्ट व्यू और एक चेकबॉक्स था, और सिर्फ इसलिए कि चेकबॉक्स, ईवेंट को आग लगाने के लिए पूरी सूची 'सक्षम' नहीं थी। जब मैं दृश्य प्राप्त कर रहा था तो मैंने एडाप्टर के अंदर एक छोटी सी चाल बनाकर हल किया।

बस दृश्य लौटने से पहले मैं डाल:

v.setOnClickListener(listener); 

(वस्तु listener एक onItemClickListener मैं Adapter के निर्माता को दे दिया है)।

लेकिन मुझे आपको बताना है, समस्या यह है क्योंकि मंच, यह एक बग है।

+0

यह मेरे लिए काम किया। – jsDebugger

33

मुझे एक ही समस्या का सामना करना पड़ा है और आपने अपना फ़िक्स करने का प्रयास किया है लेकिन इसे काम नहीं कर सका। मेरे लिए क्या काम किया गया android:descendantFocusability="blocksDescendants"<RelativeLayout> आइटम लेआउट xml, list_item.xml से आपके मामले में जोड़ रहा था। यह onItemClick() कहलाता है।

+3

एंड्रॉइड: फोकस करने योग्य = "झूठा" और एंड्रॉइड: desendantFocusability = "blockDescendants" दोनों मेरे लिए काम नहीं करते थे। – jsDebugger

+4

यदि कोई 'एंड्रॉइड है: क्लिक करने योग्य =" सत्य "इसे हटा दें। – proxylittle

+0

आपने अपना दिन बचाया .... – Deepak

7

क्या मेरे लिए काम किया: सापेक्ष लेआउट टैग करने के लिए descendantFocusability = "blocksDescendants":

1) जोड़ा जा रहा है एंड्रॉयड। परिणाम नीचे दिखाया गया है:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:descendantFocusability="blocksDescendants" > 

2) जोड़ा जा रहा है एंड्रॉयड: फ़ोकस करने योग्य = हर तत्व "गलत" list_item.xml में में उदाहरण:

<TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"  
     android:text="TextView" 
     android:focusable="false" /> 
+0

एंड्रॉइड जोड़ना: सेल लेआउट के माता-पिता को descendantFocusability = "blockDescendants" मेरे लिए काम किया – MbaiMburu

1

मैं एक ही समस्या थी और मैंने कोशिश की

android:focusableInTouchMode="false" 
android:clickable="false" 
android:focusable="false" 

मेरे item.xml को जोड़कर इसे हल करने के लिए, लेकिन यह अभी भी काम नहीं करता है !!! दरअसल मैंने पाया रिश्तेदार लेआउट डायन में इस मुद्दे को

android:focusableInTouchMode="true" android:focusable="true" 

शामिल और जब मैं इसे सभी चीजों को हटा दिया ठीक है

संबंधित मुद्दे