从另一个 class 实例化的自定义视图中检索 id

Retrieve an id from CustomView instantiate in an other class

基本上我有一个 ExpandableListView 中每个标题的 Textview 列表。

我想将数据库中的 id 放入 TextView 的属性中,以便稍后检索。我用 CustomAttribute idveh 创建了一个 CustomView "ChampText",它是 id。

public class ChampText extends android.support.v7.widget.AppCompatTextView{

public String idveh;

public ChampText(Context context){
    super(context);
}

public ChampText(Context context, AttributeSet attribute) {
    super(context, attribute);
    TypedArray ta = context.obtainStyledAttributes(attribute, R.styleable.ChampText);
    idveh = ta.getString(R.styleable.ChampText_idveh);
    ta.recycle();
}

public String getIdveh() {
    return idveh;
}

public void setIdveh(String idveh) {
    this.idveh = idveh;
}
}

和 attrs.xml :

<resources>
  <declare-styleable name="ChampText">
    <attr name="idveh" format="string"/>
  </declare-styleable>
</resources>

我在方法 getData() 中创建了我的 "ChampText" 列表:

public HashMap<String, List<ChampText>> getData(List<VehiculeModel> list) {

    List<String> expVehicule = new ArrayList<String>();

    HashMap<String, List<ChampText>> expandableListDetail = new HashMap<String, List<ChampText>>();


        for (VehiculeModel vehicule : list) {
            List<ChampText> expTextViewVehicule = new ArrayList<ChampText>();
            ChampText mesPleins = new ChampText(context);
            ChampText supprimer = new ChampText(context);
            String nomVehicule = "";

            String vehid = String.valueOf(vehicule.getId());
            nomVehicule = vehicule.getVehiculeNom() + "\n" + vehicule.getVehiculeType();

            mesPleins.setText("Mes Pleins");
            mesPleins.setId(vehicule.getId());
            mesPleins.setIdveh(vehid);

            expTextViewVehicule.add(mesPleins);

            supprimer.setText("Supprimer");
            supprimer.setId(vehicule.getId());
            supprimer.setIdveh(vehid);

            expTextViewVehicule.add(supprimer);

            expandableListDetail.put(nomVehicule, expTextViewVehicule);

        }

    return expandableListDetail;
}

此刻,我的 id 是好的 id(第一辆车 1,第二辆车 2,等等),我尝试将它分配给我为每辆车创建的每个项目的属性 idveh。

在我的 MainPage 上,我的 Views ChampText 创建得很好,我这样设置它们:

<com.bonobocorp.joker.litrocent.ChampText
    android:id="@+id/expandedListItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:onClick="onClickTextView"
    android:clickable="true"/>

Text 的值已被充分告知,我可以将 id 放在 Text 上,它运行良好,但是如果我将 id 放在我的 CustomView 的属性 idveh 上,当我在我的 MainPage 上恢复我的视图并尝试在我的方法 "onClickTextView" 上打印我的 id 的值,如下所示:

public void onClickTextView (View expandedListItem) {

    ChampText listItem = (ChampText) expandedListItem;
    String idveh = ((ChampText) expandedListItem).getIdveh();
    System.out.println(idveh);
}

idveh 的值总是 "null"。

如何在我的 ChampText(或 TextView)列表的所有项目上检索我的 ID 以触发基于该 ID 的操作?

尝试使用

context.getTheme().obtainStyledAttributes(attribute, R.styleable.ChampText);

唯一的区别是您将使用上下文中的 getTheme 方法

我解决了我的问题。

我有一个 class 生成了我的视图(尤其是我的 child 视图)。

我在 :

中得到了这个方法
@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final ChampText expandedListText = (ChampText) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
    ChampText expandedListTextView = (ChampText) convertView
            .findViewById(R.id.expandedListItem);
    expandedListTextView.setText(expandedListText.getText());
    expandedListTextView.setIdveh(expandedListText.getIdveh());
    return convertView;
}

我在末尾添加了行:expandedListTextView.setIdveh(expandedListText.getIdveh());

很明显,它有效..

好吧,谢谢大家抽出时间!!!