2012-11-08 28 views
8

मैं अपनी सभी सूची आइटम सूची सूची में एक नए पृष्ठ में खोलना चाहता हूं, इसलिए प्रत्येक सूचीदृश्य आइटम एक नए काले पृष्ठ पर खुलता है जिसका मैं उपयोग कर सकता हूं। मुझे नहीं पता कि इसे कैसे कार्यान्वित किया जाए। मैंने अंत में घंटों की खोज की है और मेरे समाधान का जवाब नहीं मिल रहा है। यदि कोई लिंक प्रदान करने के बजाय कोई ऐसा दिखा सकता है और/या समझा सकता है, तो यह बहुत सराहना की जाएगी, लेकिन या तो सहायक है।एंड्रॉइड - क्लिक करने योग्य सूचीदृश्य कैसे बनाएं?

<string-array name="sections"> 
    <item >Pro Constructive</item> 
    <item >Con Constructive</item> 
    <item >1st Speaker Cross</item> 
    <item >Pro Rebbutal</item> 
    <item >Con Rebuttal</item> 
    <item >2nd Speaker Cross</item> 
    <item >Pro Summary</item> 
    <item >Con Summary</item> 
    <item >Grand Cross</item> 
    <item >Pro Final Focus</item> 
    <item >Con Final Focus</item> 
</string-array> 

यह मेरा string.xml

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:entries="@array/sections" > 
</ListView> 

यह मेरा activity_main.xml में है:

यहाँ मेरी कोड अब तक है।

मैं अपनी सूची में प्रत्येक आइटम को क्लिक करने योग्य और नए पेज पर खोलने में सक्षम बनाने के लिए यहां से कहां से जाऊं?

अग्रिम धन्यवाद!

संपादित करें:

Logcat अब प्रासंगिक नहीं।

उत्तर

29

वास्तव में यह काफी आसान है:

इस ListView के साथ अपने गतिविधि है, यह एक OnItemClickListener लागू करता है:

public class MainActivity extends Activity implements OnItemClickListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     //* *EDIT* * 
     ListView listview = (ListView) findViewById(R.id.listView1); 
     listview.setOnItemClickListener(this); 
    } 

    public void onItemClick(AdapterView<?> l, View v, int position, long id) { 
     Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position); 
      // Then you start a new Activity via Intent 
      Intent intent = new Intent(); 
      intent.setClass(this, ListItemDetail.class); 
      intent.putExtra("position", position); 
      // Or/And 
      intent.putExtra("id", id); 
      startActivity(intent); 
    } 

संपादित

ऊपर कोड होगा आपके MainActivity.java में रखा गया है। मैंने कक्षा का नाम बदलकर MainActivity और सामग्री को setContentView(R.layout.activity_main) पर देखा - नाम ग्रहण में ताज़ा रूप से बनाए गए एंड्रॉइड प्रोजेक्ट के हैं।
कृपया // * के तहत 2 नई लाइनें भी देखें * संपादित करें - वे सूची में आइटमों पर क्लिक के लिए श्रोता सेट करेंगे।

आपका activity_main.xml इस तरह दिखना चाहिए:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:entries="@array/sections" > 
    </ListView> 
</RelativeLayout> 

array.xml (string.xml नहीं) अपने `में res/values ​​/` फ़ोल्डर इस

<resources> 
    <string-array name="sections"> 
     <item >Pro Constructive</item> 
     <item >Con Constructive</item> 
     <item >1st Speaker Cross</item> 
     <item >Pro Rebbutal</item> 
     <item >Con Rebuttal</item> 
     <item >2nd Speaker Cross</item> 
     <item >Pro Summary</item> 
     <item >Con Summary</item> 
     <item >Grand Cross</item> 
     <item >Pro Final Focus</item> 
     <item >Con Final Focus</item> 
    </string-array> 
</resources> 
तरह लग रहा है

एनबी: यदि आप & कॉपी करते हैं तो इस कोड को पेस्ट करना चाहिए। लेकिन आपको किसी आइटम पर क्लिक करके एक त्रुटि मिलेगी क्योंकि आपने अभी तक ListItemDetail.class नहीं बनाया है।

आपका ListItemDetail.java:

public class ListItemDetail extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_listitem); 

    Intent intent = getIntent(); 
    int position = intent.getIntExtra("position", 0); 

    // Here we turn your string.xml in an array 
    String[] myKeys = getResources().getStringArray(R.array.sections); 

    TextView myTextView = (TextView) findViewById(R.id.my_textview); 
    myTextView.setText(myKeys[position]); 


    } 

} 

और इसकी activity_listitem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/my_textview"/> 

</LinearLayout> 

आप इस कोड को यह काम करेंगे अतीत की प्रतिलिपि बनाते हैं

यह इस तरह लग सकता है का एक उदाहरण है।

+0

मैं माफी चाहता हूँ, मैं इस के लिए नया हूँ। क्या मैं इसे string.xml या activity_main.xml में रखूंगा, और क्या मैं अपनी जानकारी के साथ इसमें कुछ भी बदल रहा हूं? –

+0

मैंने अपना जवाब संपादित किया। उम्मीद है कि यह आपको सही रास्ते पर ले जाएगा :) – Delpes

+0

ठीक है जैसे आपने कहा, यह मुझे एक त्रुटि देता है, लेकिन मैंने गतिविधि_listitem.xml और ListItemDetail.java जोड़ा और मुझे नहीं पता कि ऐप एफसी क्यों है, खासकर क्योंकि मेरे पास है त्रुटियाँ नहीं। –

0

आप onListItemClick फ़ंक्शन का उपयोग अपने इरादे को सेट करने के लिए करते हैं जो अगली गतिविधि लोड करता है और किसी भी डेटा को पास करता है।

public void onListItemClick(ListView parent, View view, int position, long id) 
{ 
    Intent intent = new Intent(this, AnotherActivity.class); 
    intent.putExtra("position", position); 
    startActivity(intent); 
} 
0

आप इस

String[] myKeys = getResources().getStringArray(R.array.sections); 
ListView mListView = (ListView)findViewById(R.id.listView1); 
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myKeys)); 

की तरह और सूचीदृश्य वस्तुओं पर क्लिक करने के रूप string.xml में सरणी से सूचीदृश्य पॉप्युलेट कर सकते हैं काफी सरल और आसान method.just उपयोग setOnItemClickListener

mListView.setOnItemClickListener(new OnItemClickListener() 
{ 
    public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) 
    { 

     Intent n = new Intent(getApplicationContext(), yourclass.class); 
     n.putExtra("position", position); 
     startActivity(n); 
    } 
}); 
0

ListView है = (ListView) findViewById (R.id.listview);

  • सूची आइटम

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    
         if(position==0){ 
          Intent i=new Intent(MainActivity.this,Main3Activity.class); 
          startActivity(i); 
         } 
        } 
    });