2011-07-14 26 views
9

मैं कुछ अन्य विजेट (बटन, संपादन टेक्स्ट, आदि) के तहत एक सूचीदृश्य सेट करने का प्रयास कर रहा हूं। मैं सूचीदृश्य के लिए एक और गतिविधि का उपयोग नहीं करना चाहता। कुछ पढ़ने के बाद मैं How can I implement a ListView without ListActivity? (use only Activity) पाया और मैं इसे करने की कोशिश की, साथ समाप्त: यहाँ मेरी main.xml है:सूची दृश्य के बिना सूची दृश्य

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    button1 = (ImageButton) findViewById(R.id.button1); 
    mChronometer = (Chronometer) findViewById(R.id.chronometer1); 
    editText1 = (EditText) findViewById(R.id.editText1); 
    ListView lv = (ListView) findViewById(R.id.listView1); 
    String[] listword = new String[] {"Hello","World","Foo","Bar"}; 
    lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, listword)); 
    } 

और यहाँ:

<LinearLayout android:id="@+id/relativeLayout1" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="vertical"> 
<TextView ...[some code].../> 
<EditText ...[some code].../> 
<ImageButton ...[some code].../> 
<Chronometer ..[some code]..../> 
<ListView android:id="@+id/listView1" android:layout_height="wrap_content" 
    android:layout_width="fill_parent"></ListView> 

यहाँ मेरी onCreate है list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dp" 
android:textSize="16sp" > 
</TextView> 

जब मैं अपने भौतिक डिवाइस में डीबग करता हूं, टी वह बस सभी काले रहता है। अगर मैं सूची() में लाइनों को शामिल करता हूं जिसमें सूची शामिल है, तो एप्लिकेशन काम करता है (स्पष्ट रूप से सूचीदृश्य के बिना)। कोई विचार क्या गलत हो सकता है?

+0

जैसा कि आपको संदेह है कि विजेट बिना कारण के गायब हो सकते हैं। कौन सा दिखा रहा है यह देखने के लिए विभिन्न विगेट्स में पृष्ठभूमि रंग सेट करने का प्रयास करें। –

+0

सूचीदृश्य हटा दिए जाने पर निम्न दिखाया गया है? <संपादन टेक्स्ट ... [कुछ कोड] ... /> <छविबटन ... [कुछ कोड] ... /> <क्रोनोमीटर .. [ कुछ कोड] .... /> –

+0

हाँ जब मैंने सूचीदृश्य हटा दिया सबकुछ दिखाता है। – leonsas

उत्तर

16

मेरे पास वास्तव में कुछ टेक्स्ट व्यू के नीचे एक सूची दृश्य के साथ एक ऐप है और दो बटन से ऊपर, मुझे अपना कोड पकड़ने दो!

<TextView 
     android:id="@+id/results" 
     android:text="Name" 
     android:gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" android:textStyle="bold"/> 
    <TextView 
     android:id="@+id/resultsTotal" 
     android:text="Phone" 
     android:gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" android:textStyle="bold"/> 
    <TextView 
     android:id="@+id/pages" 
     android:text="AIM" 
     android:gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" android:textStyle="bold"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/name" 
     android:text="Name" 
     android:gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    <TextView 
     android:id="@+id/phone" 
     android:text="Phone" 
     android:gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    <TextView 
     android:id="@+id/aim" 
     android:text="AIM" 
     android:gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    <TextView 
     android:id="@+id/dept" 
     android:text="Dept" 
     android:gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"/> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    <Button 
     android:id="@+id/prevButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Previous" 
     android:layout_weight="1"/> 
    <Button 
     android:id="@+id/nextButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Next" 
     android:layout_weight="1"/> 

    </LinearLayout> 
</LinearLayout> 

:

public class BNYDirectoryResults extends Activity{ 

    public static String[] stuff; 
    ListView list; 
    BNYAdapter adapter; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.resultscreen); 

     TextView headDisplay = (TextView)findViewById(R.id.results); 
     TextView headCount = (TextView)findViewById(R.id.resultsTotal); 
     TextView headPages = (TextView)findViewById(R.id.pages); 

     //Set up the results list, see BNYAdapter 
     list = (ListView)findViewById(R.id.list); 
     adapter = new BNYAdapter (this, BNYDirectory.ReturnResults); 
     list.setAdapter(adapter); 

     //Sets up header information (pages, total results) 
     //Just some stuff to change the TextViews 


     //Passes EmployeeID and Phone Number to AND opens the details page 
     list.setOnItemClickListener(new OnItemClickListener(){ 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
       String EID = BNYDirectory.ReturnResults[position][0]; 
       String phoneNumber = BNYDirectory.ReturnResults[position][2]; 

       BNYDirectoryTransaction.doDetails(EID, phoneNumber); 
       Intent i = new Intent(view.getContext(), BNYDirectoryDetails.class); 
       startActivity(i); 

      } 
     }); 
    } 
} 

यहाँ यह के लिए एक्सएमएल फ़ाइल है:

यहाँ (काफ़ी संक्षिप्तता के लिए हटाया के साथ) textviews नीचे सूचीदृश्य साथ और बटन के ठीक ऊपर मेरी गतिविधि है और यहां सूची के लिए कस्टम एडाप्टर है:

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

public class BNYAdapter extends BaseAdapter { 

    private Activity activity; 
    private String[][] results; 
    private static LayoutInflater inflater=null; 

    public BNYAdapter(Activity a, String[][]info) { 
     activity = a; 
     results = info; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return results.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public static class ViewHolder{ 
     public TextView name; 
     public TextView phone; 
     public TextView aim; 
     public TextView dept; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     ViewHolder holder; 
     if(convertView==null){ 
      vi = inflater.inflate(R.layout.item, null); 
      holder=new ViewHolder(); 
      holder.name=(TextView)vi.findViewById(R.id.nameItem); 
      holder.phone=(TextView)vi.findViewById(R.id.phoneItem); 
      holder.aim=(TextView)vi.findViewById(R.id.aimItem); 
      holder.dept=(TextView)vi.findViewById(R.id.deptItem); 
      vi.setTag(holder); 
     } 
     else 
      holder=(ViewHolder)vi.getTag(); 

     holder.name.setText(BNYDirectory.ReturnResults[position][1]); 
     holder.phone.setText(BNYDirectory.ReturnResults[position][2]); 
     holder.aim.setText(BNYDirectory.ReturnResults[position][3]); 
     holder.dept.setText(BNYDirectory.ReturnResults[position][4]); 
     return vi; 
    } 

} 

और सभी एक साथ जो Look, a list with other stuff around it!

+0

धन्यवाद! मुझे बस सबकुछ एक और लाइनरलाउट – leonsas

+0

में रखना पड़ा, खुशी है कि आपको यह काम मिल गया है। मुझे अपने कुछ ऐप्स के साथ पता है, शायद मेरे पास बहुत सारे लाइनरलेआउट हैं। – Rob

-2

जैसे पृष्ठ बनाता है आप सूचीकरण के साथ अपने स्वयं के लेआउट का उपयोग कर सकते हैं। अपनी गतिविधि को सूचीत्मकता बढ़ाएं, अपना स्वयं का लेआउट बनाएं और सुनिश्चित करें कि ListView में android:id="@android:id/list" है (इस प्रकार ListActivity आपके स्वयं के लेआउट में ListView से लिंक करता है), फिर क्रिएट में setContentView के साथ अपना लेआउट सेट करें।

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