2012-10-17 65 views
0

我在Realtive Layout中使用ImageView和textview。相對佈局Onclcik我需要獲取文本視圖的值,並且需要更改imageview。獲取空指針異常。 這裏是代碼從RelativeLayout獲取ImageView和TextView時點擊它

活動代碼

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       int id=0; 

        for (int idx = 0; idx < dataFromDb.size(); idx++) { 
         id = idx + 1; 
         rel = (RelativeLayout) inflater.inflate(
           R.layout.newdata, null); 
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
           LayoutParams.WRAP_CONTENT, 
           LayoutParams.WRAP_CONTENT); 
         params.setMargins(0, 50, 0, 0); 
         txt = (TextView) rel.findViewById(R.id.fetchData); 
         txt.setText(dataFromDb.get(idx)); 
         txt.setId(id); 

         img = (ImageView) rel.findViewById(R.id.tickbox); 
         img.setImageResource(R.drawable.tickbox1a); 
         img.setId(id); 

         rel.setOnClickListener(new ImageView.OnClickListener() { 

          @Override 
          public void onClick(View v) { 
           // TODO Auto-generated method stub 
           Toast.makeText(DoTask.this, 
             "Selected" + v.getId(), 
             Toast.LENGTH_LONG).show(); 
           RelativeLayout im=(RelativeLayout)v; 
           ImageView i=(ImageView)im.findViewById(im); 
           i.setImageResource(R.drawable.tickbox1b); 
          } 
         }); 

         content.addView(rel, params); 

        } 

newData.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/tickbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/tickbox1a" 
     android:paddingBottom="10dp" /> 

    <TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fetchData" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="50dp" 
     android:paddingTop="2dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/gray" 
     android:textSize="18sp" /> 

</RelativeLayout> 

任何幫助,將不勝感激。

+0

入門中的onclick方法 – Badrinath

+0

例外,你爲什麼用循環從數據庫中添加許多佈局?我想你應該嘗試使用'ListView'。你的'OnClick',我認爲它應該是'新的OnClickListener'(從'android.view.View.OnClickListener'不是'Dialog.OnClickListener'不是'Dialog.OnClickListener' )而不是'new ImageView.OnClickListener()' –

回答

4

我相信你應該是這樣的:

public void onClick(View v) { 
          // TODO Auto-generated method stub 
          Toast.makeText(DoTask.this, 
            "Selected" + v.getId(), 
            Toast.LENGTH_LONG).show(); 
          RelativeLayout im=(RelativeLayout)v; 
          ImageView i=(ImageView)im.findViewById(R.id.tickbox); 
          i.setImageResource(R.drawable.tickbox1b); 
         } 

你試圖調用findViewById上的RelativeLayout,至極是一個視圖時,預計和ID。 或者只是使用您在上面定義的img,不需要創建另一個imageview實例,因爲您已經有一個。

0

活動代碼應該是

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      int id=0; 

       for (int idx = 0; idx < dataFromDb.size(); idx++) { 
        id = idx + 1; 
        rel = (RelativeLayout) inflater.inflate(
          R.layout.newdata, null); 
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT); 
        params.setMargins(0, 50, 0, 0); 
        txt = (TextView) rel.findViewById(R.id.fetchData); 
        txt.setText(dataFromDb.get(idx)); 
        txt.setId(id); 

        img = (ImageView) rel.findViewById(R.id.tickbox); 
        img.setImageResource(R.drawable.tickbox1a); 
        img.setId(id); 

        rel.setOnClickListener(new ImageView.OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          Toast.makeText(DoTask.this, 
            "Selected" + v.getId(), 
            Toast.LENGTH_LONG).show(); 

          img.setImageResource(R.drawable.tickbox1b); 
         } 
        }); 

        content.addView(rel, params); 

       } 
+0

我需要得到textView的值。我怎麼能這樣? – Badrinath

+0

String input = txt.getText()。toString(); 現在輸入將您所需的值作爲字符串 –

相關問題