2015-05-25 61 views
1

我想要創建一個ListView保存具有CheckBoxRadioGroup與2 RadioButtons列表項目。ListView與RadioGroup不顯示

我不知道爲什麼,但TextView的和CheckBox顯示,而RadioGroup不是。

我查過這個問題,但找不到這個確切的問題。誰能幫我?

這是XML代碼:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
> 
<View 
    android:layout_width="match_parent" 
    android:layout_height="1dip" 
    android:id="@+id/hlInteraction" 
    android:layout_alignParentBottom="true" 
    android:background="@android:color/darker_gray" /> 

<RadioGroup 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/hlInteraction" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:id="@+id/rgInteraction" 
    > 
    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:text="@string/rb_on" 
     android:id="@+id/rbTurnOff"/> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingRight="16dp" 
     android:paddingLeft="16dp" 
     android:text="@string/rb_off" 
     android:id="@+id/rbTurnOn" 
     /> 
    </RadioGroup> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tvDeviceNameInteraction" 
    android:layout_alignParentLeft="true" 
    android:paddingLeft="16dp" 
    android:textSize="24sp" 
    android:layout_above="@id/rgInteraction" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tvDeviceDistanceInteraction" 
    android:layout_toRightOf="@id/tvDeviceNameInteraction" 
    android:paddingLeft="16dp" 
    android:textSize="24sp" 
    android:layout_above="@id/rgInteraction" 
    /> 
<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:paddingRight="4dp" 
    android:id="@+id/cbInteraction" 
    android:layout_above="@id/rgInteraction" 
    /> 

這裏是習俗adatper類:

public class InteractionAdapter extends ArrayAdapter<Interaction> 
{ 
    private ArrayList<Interaction> mList; 
    private Context mContext; 

    /* 
    * Creates the Adapter for the list 
    */ 
    public InteractionAdapter(Context context, int layoutId, ArrayList<Interaction> list) 
    { 
     super(context,layoutId,list); 
     mList = list; 
     mContext = context; 

    } 
    /** 
    * inflate view for each row 
    */ 
    @SuppressLint("InflateParams") 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     Interaction current = mList.get(position); 
     /* if the given channel row view is not being updated*/ 
     if (v == null) { 
      /* inflate layout */ 
      LayoutInflater vi = LayoutInflater.from(getContext()); 
      v = vi.inflate(R.layout.list_item_interaction, null, false); 
     } 

     /*get the component pointers */ 
     TextView tvInteractionDeviceName = (TextView) v.findViewById(R.id.tvDeviceNameInteraction); 
     TextView tvInteractionDeviceDistance = (TextView) v.findViewById(R.id.tvDeviceDistanceInteraction); 


     RadioButton rbOn = (RadioButton) v.findViewById(R.id.rbTurnOn); 
     RadioButton rbOff = (RadioButton) v.findViewById(R.id.rbTurnOff); 
     rbOn.setChecked(true); 

     CheckBox cbInteraction = (CheckBox) v.findViewById(R.id.cbInteraction); 

     Device d = current.getDevice();//Gets Device object and then BluettothDevice object 

     tvInteractionDeviceName.setText(d.getDevice().getName()); 
     tvInteractionDeviceDistance.setText(String.valueOf(d.getRSSI())); 

     return v; 
    } 

另外,如果我想獲得RadioButton SI的狀態基本上需要爲他們添加標籤作爲標籤的一種形式,以便我可以區分彼此。對?

編輯
佈局似乎是在我剛剛發佈了XML代碼給你看預覽是工作的罰款。我懷疑這部分代碼中有任何錯誤。

這裏有一個畫面:

enter image description here

+0

我已經發布了一個新的答案,這段代碼正在運行。 –

回答

1

我已經嘗試了一切自己。代碼的這一部分工作正常,問題出現在你的佈局中。隨着你使用的視圖佈局。這是整個代碼。

MainActivity.java

package com.example.helppeople; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ListView; 

public class MainActivity extends Activity { 

ListView mLists; 

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

    mLists = (ListView) findViewById(R.id.list); 
    ArrayList<Interaction> mList = new ArrayList<Interaction>(); 

    mList.add(new Interaction("123")); 
    mList.add(new Interaction("456")); 
    mList.add(new Interaction("789")); 
    mList.add(new Interaction("012")); 

    InteractionAdapter mAdapter = new InteractionAdapter(
      getApplicationContext(), mList); 

    mLists.setAdapter(mAdapter); 

    } 
} 

activity_main。XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

list_item_interaction.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

<RadioGroup 
    android:id="@+id/rgInteraction" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" > 

    <RadioButton 
     android:id="@+id/rbTurnOff" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="asdasd" /> 

    <RadioButton 
     android:id="@+id/rbTurnOn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="asdas" /> 
</RadioGroup> 

<TextView 
    android:id="@+id/tvDeviceNameInteraction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/rgInteraction" 
    android:layout_alignParentLeft="true" 
    android:paddingLeft="16dp" 
    android:textSize="24sp" /> 

<TextView 
    android:id="@+id/tvDeviceDistanceInteraction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/rgInteraction" 
    android:layout_toRightOf="@id/tvDeviceNameInteraction" 
    android:paddingLeft="16dp" 
    android:textSize="24sp" /> 

<CheckBox 
    android:id="@+id/cbInteraction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/rgInteraction" 
    android:layout_alignParentRight="true" 
    android:paddingRight="4dp" /> 

</RelativeLayout> 

InteractionAdapter.java

package com.example.helppeople; 

import java.util.ArrayList; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.RadioButton; 
import android.widget.TextView; 

public class InteractionAdapter extends BaseAdapter { 
private ArrayList<Interaction> mList; 
private Context mContext; 

/* 
* Creates the Adapter for the list 
*/ 
public InteractionAdapter(Context context, ArrayList<Interaction> list) { 
    mList = list; 
    mContext = context; 

} 

/** 
* inflate view for each row 
*/ 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    Interaction current = mList.get(position); 
    /* if the given channel row view is not being updated */ 
    if (v == null) { 
     /* inflate layout */ 
     LayoutInflater vi = LayoutInflater.from(mContext); 
     v = vi.inflate(R.layout.list_item_interaction, null, false); 
    } 

    /* get the component pointers */ 
    TextView tvInteractionDeviceName = (TextView) v 
      .findViewById(R.id.tvDeviceNameInteraction); 
    TextView tvInteractionDeviceDistance = (TextView) v 
      .findViewById(R.id.tvDeviceDistanceInteraction); 

    RadioButton rbOn = (RadioButton) v.findViewById(R.id.rbTurnOn); 
    RadioButton rbOff = (RadioButton) v.findViewById(R.id.rbTurnOff); 
    rbOn.setChecked(true); 

    CheckBox cbInteraction = (CheckBox) v.findViewById(R.id.cbInteraction); 

    // Gets Device object and then BluettothDevice object 

    tvInteractionDeviceName.setText("abc"); 
    tvInteractionDeviceDistance.setText("xyxz"); 

    return v; 
} 

@Override 
public int getCount() { 
    return mList.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    return mList.get(arg0); 
} 

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

Interaction.java

package com.example.helppeople; 

public class Interaction { 

String name; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Interaction(String name) { 
    super(); 
    this.name = name; 
    } 

} 
+0

感謝您的努力,您的解決方案解決了我的問題。我很感激! :) –

+0

@PavlePavlov高興地幫助 –

0

首先我提高了你的佈局有點

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" > 

<View 
    android:id="@+id/hlInteraction" 
    android:layout_width="match_parent" 
    android:layout_height="1dip" 
    android:layout_alignParentBottom="true" 
    android:background="@android:color/darker_gray" /> 

<RadioGroup 
    android:id="@+id/rgInteraction" 
    android:layout_width="match_parent" 
    android:id="@+id/radioGroup" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/hlInteraction" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" > 

    <RadioButton 
     android:id="@+id/rbTurnOff" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="asdasd" /> 

    <RadioButton 
     android:id="@+id/rbTurnOn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="asdas" /> 
</RadioGroup> 

<TextView 
    android:id="@+id/tvDeviceNameInteraction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/rgInteraction" 
    android:layout_alignParentLeft="true" 
    android:paddingLeft="16dp" 
    android:textSize="24sp" /> 

<TextView 
    android:id="@+id/tvDeviceDistanceInteraction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/rgInteraction" 
    android:layout_toRightOf="@id/tvDeviceNameInteraction" 
    android:paddingLeft="16dp" 
    android:textSize="24sp" /> 

<CheckBox 
    android:id="@+id/cbInteraction" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/rgInteraction" 
    android:layout_alignParentRight="true" 
    android:paddingRight="4dp" /> 

但單選按鈕的顯示無關與此,我認爲你必須使用radiogroup

RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroup); 

     radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       // Radio button 
       RadioButton rb1 = (RadioButton) findViewById(R.id.radioButtonID); 
       if (rb1.isChecked()) { 
        Toast.makeText(MainActivity.this, "1 option selected", Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(MainActivity.this, "2 option selected", Toast.LENGTH_LONG).show(); 

       } 
      } 
     }); 

試試這個,不要使用@SuppressLint(「InflateParams」),使用

convertView = inflater.inflate(R.layout.list_item_interaction, parent, false); 
+0

告訴我它是否有幫助,希望它有幫助...我沒有測試運行此代碼,但我認爲這是問題 –

+0

更正後的XML代碼沒有幫助。並不知道如何第二部分的代碼可以幫助...還試過,不工作... –

+0

@PavlePavlov看看它是否有幫助,並讓我知道。我編輯了我的答案,不要使用@SuppressLint(「InflateParams」),而是使用convertView = inflater.inflate(R.layout.list_item_interaction,parent,false);而不是 –

0

帕夫萊, 首先,請檢查您的XML預覽模式下,如果你的單選按鈕顯示特定行最多或不,如果不是那麼肯定你的佈局設計是不正確的。 如果是這樣,那麼我們可以在佈局上制定出來。

+0

請爲此添加一些代碼。這不會被接受爲您需要爲此添加一些代碼的答案。 – Piyush

0

請嘗試這樣的:

RadioGroup radioGroup = (RadioGroup)findViewById(r.id.rgInteraction); 
// This will get the radiobutton in the radiogroup that is checked 
RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()); 

使用聽者這樣的:

// This overrides the radiogroup onCheckListener 

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(RadioGroup radioGroup, int checkedId) 
     { 
      // This will get the radiobutton that has changed in its check state 
      RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId); 
      // This puts the value (true/false) into the variable 
      boolean isChecked = checkedRadioButton.isChecked(); 
      // If the radiobutton that has changed in check state is now checked... 

      if (isChecked){ 
       Toast.makeText(InteractionAdapter.this, "Checked", Toast.LENGTH_LONG).show(); 
      } else{ 
       Toast.makeText(InteractionAdapter.this, "Unchecked", Toast.LENGTH_LONG).show(); 
      } 
     } 
    });