2012-03-09 14 views
8

मुझे स्क्रीन पर टेक्स्ट आइटम की एक सूची प्रदर्शित करने और उन्हें क्लिक करने योग्य बनाने की आवश्यकता है। तो यह एक वेब अनुप्रयोग पर लिंक की एक सूची की तरह कुछ होगा।एंड्रॉइड ऐप - आइटम्स की सूची कैसे प्रदर्शित करें और उन्हें क्लिक करने योग्य बनाएं

मैं एंड्रॉइड गतिविधि स्क्रीन में ऐसा कैसे कर सकता हूं?

यह कुछ यादृच्छिक संख्या होगी जो मुझे एक डीबी से खींचने और लिंक के रूप में प्रदर्शित करने के लिए है।

कोई विचार यह कैसे किया जा सकता है?

उत्तर

3

आपको ListView का उपयोग करना चाहिए। यह बहुत आसान है, बस ListActivity बनाएं, अपनी वस्तुओं को Adapter के अंदर रखें और फिर इसे ListActivity के Adapter के रूप में सेट करें।

आप listviews here

1

के बारे में अधिक पढ़ सकते हैं वहाँ भी एक नए प्रतिमान ListFragment कहा जाता है।

मैंने पहले सूची दृश्यों का उपयोग किया है लेकिन अब टुकड़े के दृष्टिकोण को पसंद करते हैं - यह टैबलेट पर बहुत सीधी आगे और काफी लचीला एएसपी है क्योंकि किसी आइटम का चयन करते समय स्क्रीन पर किसी अन्य क्षेत्र के साथ बातचीत काफी लचीली होती है और केवल बहुत कम कोड की आवश्यकता होती है।

बस एक उदाहरण:

public class Select_FoodCategories_Fragment extends android.app.ListFragment { 
    private static final boolean DEBUG = true; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
    super.onCreate(savedInstanceState); 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
    HoldingActivity a = (HoldingActivity) getActivity(); 
    //accessing a variable of the activity is easy 
    a.visibleListViewInFragment = getListView(); 

    List<XYZ> listTodisplay = a.getListToDisplay(); 

    MyAdapter adapter = new MyAdapter(
     getActivity(), 0, listTodisplay); 
    setListAdapter(adapter); 

    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
     XYZ item = (XYZ) getListAdapter() 
     .getItem(position); 

    } 

} 

अधिक यहाँ जानकारी: http://developer.android.com/reference/android/app/ListFragment.html

वैसे, मैं यह वास्तव में इसके लायक नई टुकड़े अवधारणा से परिचित कराने के लिए मिल - यह सिर्फ इतना आसान लाइव बनाता है - गोलियों पर esp!

ps मैंने जान-बूझकर में डिबग बयान छोड़ दिया - क्योंकि यह खोज करने ऑल्टो मेरे अनुभव

8

हाँ आप यह कर सकते में बहुत तेजी से पूरी अवधारणा को समझने के लिए। इसे डीबी से लाने के लिए डेटा एक्सचेंज क्लास बनाएं .. स्ट्रिंग्स को एक ऐरे में स्टोर करें।

डेटाबेस से प्राप्त स्ट्रिंग्स की सरणी प्रदर्शित करने के लिए एक ArrayAdapter बनाएँ।

उदाहरण

के लिए

public class AndroidListViewActivity extends ListActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // storing string resources into Array 
    String[] numbers = {"one","two","three","four"} 
    // here you store the array of string you got from the database 

    // Binding Array to ListAdapter 
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,  numbers)); 
    // refer the ArrayAdapter Document in developer.android.com 
    ListView lv = getListView(); 

    // listening to single list item on click 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

      // selected item 
      String num = ((TextView) view).getText().toString(); 

      // Launching new Activity on selecting single List Item 
      Intent i = new Intent(getApplicationContext(), SingleListItem.class); 
      // sending data to new activity 
      i.putExtra("number", num); 
      startActivity(i); 

     } 
    }); 
} 
} 

secondActivity विशेष आइटम आप क्लिक किया है

public class SingleListItem extends Activity{ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.single_list_item_view); 

    TextView txtProduct = (TextView) findViewById(R.id.product_label); 

    Intent i = getIntent(); 
    // getting attached intent data 
    String product = i.getStringExtra("number"); 
    // displaying selected product name 
    txtProduct.setText(product); 

} 
} 

होना चाहिए आप तदनुसार विभिन्न लेआउट फ़ाइलों को बनाने के लिए है प्रदर्शित करने के लिए .. आशा इस मदद करता है :)

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