2013-04-02 64 views
0

我是新的@andorid開發。請幫我解決這個問題。在構造函數上創建Android本地公共類textview

這裏是我的問題:

創建listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="wrap_content" 
android:gravity="left|center" 
android:layout_width="wrap_content" 
android:paddingBottom="5dp" 
android:paddingTop="5dp" 
android:paddingLeft="5dp"> 

<TextView android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:textColor="#FFFF00" 
    android:textIsSelectable="true"> 

</TextView> 

<TextView android:id="@+id/TextView02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:textColor="#0099CC" 
    android:textIsSelectable="true"> 

</TextView> 

和地方類聲明:

public class listview_row{ 
    TextView Text1; 
    TextView Text2; 
    public listview_row(){ 
     Text1 = (TextView) findViewById(R.id.TextView01); 
     Text2 = (TextView) findViewById(R.id.TextView02); 
    } 
} 

,當我從這個類來創建對象:

 listview_row obListview = new listview_row(); 
    obListview.Text1.setText(bundle.getString("first_name")); 
    obListview.Text2.setText(bundle.getString("last_name")); 

obListview.Text1 allways equal null and nullobjectexecption is raising。我怎樣才能解決這個問題。 我以爲我可以在構造函數上創建textview,但我想我錯了。

PS:我想創建帶有兩個textview的Listview,我使用了android.R.layout.simple_list_item_1,並且我可以設法在兩個acticity之間傳遞數據,沒有任何問題。現在我嘗試顯示first_name | last_name @first screen listview。

對於這裏的詳細信息是我的onActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent intent){ 
    if (resultCode == RESULT_OK){ 
     Bundle extras = intent.getExtras(); 

     if (extras != null){ 
     switch (requestCode){ 
     case Activity_New: 
      add_new_row(extras); 
      break; 
     case Activity_Edit: 
      edit_row(extras); 
      break; 
     }} 
    } 
} 

,這是add_new_row: 公共無效add_new_row(束束){// Başlıkbilgilerinikayıtediyoruz INT卅一= HeaderArray.size(); listview_row obListview = new listview_row(); obListview.Text1.setText(bundle.getString(「first_name」)); obListview.Text2.setText(bundle.getString(「last_name」));

HeaderArray.add(sayi, obListview); 
    HeaderAdapter.notifyDataSetChanged(); 

    // Kalem bilgilerini kayıt ediyoruz 
    mtable_row obMember = new mtable_row(); 
    obMember.index = sayi; 
    obMember.first_name = bundle.getString("first_name"); 
    obMember.last_name = bundle.getString("last_name"); 
    obMember.birth_date = bundle.getString("birth_date"); 
    itemArray.add(sayi, obMember); 
} 

我使用詳細信息陣列:

private ArrayList<mtable_row> itemArray; 

public class mtable_row{ 
    int index; 
    String first_name; 
    String last_name; 
    String birth_date; 
} 

我的主要目標是工作有兩個陣列: 第一個是標題和第二個是項目。 標題有兩個字段first_name和second_name

我喜歡在我的主屏幕上顯示此數組。

+0

這是什麼'內listview_row'指'Text1'和'Text2'?您顯然不會設置它們...... – t0mm13b

回答

0

我想有兩個TextView的創建列表視圖,

爲此,我建議你有一個自定義列表視圖。定義一個自定義適配器。爲每行填充自定義佈局。

您可以將您的值傳遞給CustomAdapter的構造函數。

CustomAdapter cus = new CustomAdapter(this); 
ListView lv= (ListView) findViewById(R.id.lv); 
lv.setAdapter(cus); 

public class CustomAdapter extends ArrayAdapter { 
Context c; 
    LayoutInfator mInflater; 
public CustomAdapter(CustomListView customListView) { 
    super(customListView, 0); 
    // TODO Auto-generated constructor stub 
    this.mInflater = LayoutInflater.from(customListView); 
    c=customListView; 
} 
public int getCount() { 
    return 20; //listview item count 
} 

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

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

public View getView(final int arg0, View arg1, ViewGroup arg2) { 
    final ViewHolder vh; 
    vh= new ViewHolder(); 

    if(arg1==null) 
    { 
     arg1=mInflater.inflate(R.layout.customlayout, arg2,false); 
     vh.tv1= (TextView)arg1.findViewById(R.id.textView1); 
     vh.tv2= (TextView)arg1.findViewById(R.id.textView2);  
    } 
    else 
    { 
    arg1.setTag(vh); 
    } 
     vh.tv1.setText("hello");  
     vh.tv2.setText("hello"); 

    return arg1; 
} 

static class ViewHolder 
{ 
TextView tv1,tv2; 
} 
} 

一旦你在兩個活動之間傳遞數據,在第二個活動中檢索數據。每個屏幕都有自己的用戶界面。你不能像你所做的那樣引用UI元素。

有兩臺TextViews你的第二個活動

public class SecondActivity extends Activity{ 
TextView tv1; 
TextView tv2; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 
    tv1 = (TextView) findViewById(R.id.TextView01); 
    tv2 = (TextView) findViewById(R.id.TextView02); 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     tv1.setText(extras.getString("first_name")); 
     tv2.setText(extras.getString("last_name")); 
    } 

    } 
} 
+0

感謝您的回答,現在我明白了LayoutInflater –

1

要這樣來做,你就必須擡高一個Layout文件傳遞給你的類並用它來獲取ID,如

Text1 = (TextView) layout.findViewById(R.id.TextView01); 

其中layout你傳遞給構造layout是充氣。我沒有嘗試過,但類似的東西可能會起作用。但是,除非你有必要做這種方式,它可能會更容易只是爲了讓那些ViewsActivitvy

public class YourActivity extends Activity { 

TextViw Text1; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview); 

    Text1 = (TextView) findViewById(R.id.TextView01); 
    Text2 = (TextView) findViewById(R.id.TextView02); 
    ..... 
} 
} 

你得到一個NPE,因爲據我所看到的,你沒有填充layout文件,其中存在這些Views

此外,一個小事情,但你應該考慮堅持Java naming conventions不要混淆或混淆他人。類名稱應該是駱駝式的,變量名稱應該是混合大小寫。

+0

感謝您的回答 –

+0

不客氣! – codeMagic

相關問題