2012-10-31 131 views
0

我想膨脹一個佈局的特定部分,但我收到編譯器錯誤,我不確定採取什麼方法。特別是與線 -如何膨脹兒童視圖?

LinearLayout childList = (LinearLayout)findViewById(R.id.listLayout). 
//(This can be found near the bottom of Java code.) 

我也不能確定,如果我採取正確的方法試圖膨脹listLayout時。

在這個階段,我試着在上面有一個按鈕,然後動態地在它下面的一個數組中添加一個項目列表。

任何幫助,將不勝感激

XML

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

    <Button 
     android:id="@+id/labelAddCourseButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:onClick="addCourseButton" 
     android:padding="10dp" 
     android:text="@string/CourseName" /> 

    <LinearLayout 
    android:id="@+id/listLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" >  

    <TextView 
     android:id="@+id/labelModuleCode" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:text="@string/CourseName" 
     android:textSize="10dp" /> 

    <TextView 
     android:id="@+id/labelCourseType" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/CourseType" 
     android:layout_marginLeft="10dp" 
     android:textSize="10dp" /> 

    </LinearLayout> 

</LinearLayout> 

的Java

package com.example.mycoursetimetable; 

import android.os.Bundle; 
import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MyCourses extends ListActivity { 

    static final String TEST = "com.example.mycoursetimetable.TEST"; 
    String [] MODULE; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my_courses); 
     MODULE = getResources().getStringArray(R.array.module); 
     setListAdapter(new ListArrayAdapter(this,MODULE)); 
    } 

    public void addCourseButton (View addCourseButton) 
    { 
     Intent intent = new Intent(this,AddCourse.class); 
     startActivity(intent); 
    } 

    public void onListItemClick(ListView l, View v, int position, long id) 
    { 
     super.onListItemClick(l, v, position, id); 

     try { 
     Class test = Class.forName("com.example.MyCourseTimeTable.AddCourse"); 
     Intent intent = new Intent(MyCourses.this, test); 

     TextView textView = (TextView) v.findViewById(R.id.labelModuleCode); 
     String module = textView.getText().toString(); 

     intent.putExtra(TEST,module); 
     startActivity(intent); 
     } 
     catch (ClassNotFoundException e) 
     { 
     e.printStackTrace(); 
     }    
    }  
} 

class ListArrayAdapter extends ArrayAdapter<String> 
{ 
    //Context allows the retrieval of resources such as layout 
    private final Context context; 
    private final String[] test; 

    //create the ArrayAdpater 
    public ListArrayAdapter(Context context, String[] test) 
    { 
     super(context, R.layout.activity_my_courses, test); 
     this.context = context; 
     this.test = test; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     //inflater dynamically loads the layout 

     LinearLayout childList = (LinearLayout)findViewById(R.id.listLayout); 
     View rowView = inflater.inflate(R.layout.childList, parent, false); 

     //get the textView 
     TextView textView = (TextView) rowView.findViewById(R.id.labelModuleCode); 
     //set the text to the string values based on position 
     textView.setText(test[position]); 

     // Change item based on its position in the string array 
     String modulePosition = test[position];    

     //return the layout 
     return rowView; 
    } 
} 
+0

請問您能否分享編譯器錯誤? – Eric

+0

我可以看到,你對膨脹佈局的想法有點困惑,但你並不遙遠。但是發佈佈局'activity_my_courses.xml'和'childList.xml',以便我可以看到你想要做什麼。 – Sam

+0

發佈雖然現在我認爲我的孩子佈局的定義是完全關閉 – Calgar99

回答

0

以下是您可能需要的一個示例。通過觀看Android的Romain Guy,瞭解我爲什麼使用ViewHolder和其他技巧discuss adapters and efficiency

class ListArrayAdapter extends ArrayAdapter<String> 
{ 
    // You only need one copy of LayoutInflater 
    private final LayoutInflater inflater; 

    //create the ArrayAdpater 
    public ListArrayAdapter(Context context, String[] test) 
    { 
     // The layout here doesn't really matter since we don't use it 
     super(context, R.layout.childList, test); 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ViewHolder holder; 
     if(convertView == null) { 
      // Inflate the layout that you want for each row 
      convertView = inflater.inflate(R.layout.childList, parent, false); 

      holder = new ViewHolder(); 
      holder.code = (TextView) convertView.findViewById(R.id.labelModuleCode); 
      holder.type = (TextView) convertView.findViewById(R.id.labelModuleType); 

      // Add a reference in ViewHolder for your Button, find it like the TextViews and define its OnClickListener here too, eventually... 

      convertView.setTag(holder); 
     } 
     else 
      holder = (ViewHolder) convertView.getTag(); 

     String module = getItem(position); 
     //set the text to the string values based on position 
     holder.code.setText(module); 

     //return the layout 
     return convertView; 
    } 

    class ViewHolder { 
     TextView code; 
     TextView type; 
    } 
} 
+0

完美。非常感謝你。非常感謝。 – Calgar99

1

嘗試編輯您的getView()下面的一個:

 @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     //inflater dynamically loads the layout 

     LinearLayout childList = (LinearLayout)findViewById(R.id.listLayout); 
     convertView = inflater.inflate(R.layout.childList, null); 

     //get the textView 
     TextView textView = (TextView) convertView.findViewById(R.id.labelModuleCode); 
     //set the text to the string values based on position 
     textView.setText(test[position]); 

     // Change item based on its position in the string array 
     String modulePosition = test[position]; 



     //return the layout 
     return convertView; 
     } 

使用getView()convertView查看而不是您自己的rowView

+0

謝謝,我試過你的建議,但日食仍然突出顯示findViewById作爲錯誤。是因爲它在ListArrayAdapter類中嗎?如果是這樣,我該如何解決它? – Calgar99

+0

在我的項目中,我不使用ListActivty,而是使用Activity,並使用BaseAdapter而不是ArrayAdapter。 –