2011-03-08 10 views
14

मैं एकाधिक कॉलम के साथ एंड्रॉइड में एक टेबल बनाना चाहता हूं। मैंने देखा कि अधिकांश उदाहरण 2 कॉलम के साथ है। (मैं जावा और एंड्रॉइड के लिए नया हूं।) मुझे 3-4 कॉलम चाहिए और मुझे तालिका में गतिशील रूप से पंक्तियों को जोड़ने में सक्षम होना चाहिए। क्या कोई मुझे नमूना कोड प्रदान कर सकता है। (मैं जीत में ग्रहण का उपयोग कर रहा हूं 7)एकाधिक कॉलम के साथ एंड्रॉइड में एक टेबल कैसे बनाएं?

उत्तर

23

मुझे लगता है कि आप एक टेबललेआउट दृश्य के बारे में बात कर रहे हैं और डेटाबेस में कोई तालिका नहीं है ??

यदि हां, तो यहां तीन स्तंभों और तीन पंक्तियों वाली तालिका का एक एक्सएमएल उदाहरण है।

प्रत्येक < तालिका पंक्ति> तत्व तालिका में एक पंक्ति बनाता है, और तत्व के अंदर प्रत्येक दृश्य "कॉलम" बनाता है। मैं TextViews का उपयोग किया है, लेकिन वे ImageViews, EditText, आदि हो सकता है

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id = "@+id/RHE" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:padding="5dp"> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/runLabel" 
      android:text="R" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/hitLabel" 
      android:text="H" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/errorLabel" 
      android:text="E" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/visitorRuns" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/visitorHits" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/visitorErrors" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/homeRuns" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/homeHits" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/homeErrors" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 
</TableLayout> 

गतिशील कोड में इन बदलने के लिए, आप कुछ इस तरह होगा:

// reference the table layout 
TableLayout tbl = (TableLayout)findViewById(R.id.RHE); 
// delcare a new row 
TableRow newRow = new TableRow(this); 
// add views to the row 
newRow.addView(new TextView(this)); // you would actually want to set properties on this before adding it 
// add the row to the table layout 
tbl.addView(newRow); 
+0

हाँ, मैं एक चाहते टेबल लेआउट व्यू। कॉलम ठीक से तैनात नहीं हैं। कॉलम कैसे रखें? मैं पंक्तियों को गतिशील रूप से कैसे जोड़ सकता हूं? – narayanpatra

+0

मुझे यकीन नहीं है कि 'ठीक से तैनात नहीं' से आपका क्या मतलब है। TableLayout को इसके गुणों के अनुसार आकार दिया जाएगा, जैसा कि अंदर आइटम हैं। शायद अगर आपने अपना एक्सएमएल पोस्ट किया है और साथ ही आप जो भी देखना चाहते हैं, तो हमारे पास इस सवाल का जवाब देने की अधिक क्षमता होगी। – Peter

+0

मैंने xml फ़ाइल के कोड का उपयोग किया है जैसा कि यह है। बस पहली 2 लाइनों को बदलें। यानी <टेबललेआउट xmlns: android = "http://schemas.android.com/apk/res/android" एंड्रॉइड: id = "@ + आईडी/आरएचई " यह बहुत सारी त्रुटि दिखा रहा है। मैं यहाँ क्या गलती कर रहा हूँ? – narayanpatra

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