0

我需要的是访问单击项目(布局)的按钮;此代码有效,但仅显示所有按钮,TextView,...不是单击的所有布局。如果您想运行此代码,您可以添加其他带有按钮的布局进行测试,谢谢您的帮助这是我的布局:

这是activity_main.xml

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

        <ListView
            android:id="@+id/listView_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
       <TextView        
          android:id="@+id/myTextView"    
          android:layout_width="fill_parent"    
         android:layout_height="25dp"    
         android:textSize="23sp" />
    </LinearLayout>

然后我的MainActivity:

    package com.example.adapterlist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    Map<String, Integer> layoutIds = new HashMap<String, Integer>();
    ArrayList<String> arrayForArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView listView = (ListView) findViewById(R.id.mylist);
        java.lang.reflect.Field[] ID_Fields = R.layout.class.getFields();
        String []values=new String[ID_Fields.length];
        int[] resArray = new int[ID_Fields.length];
        for(int i = 0; i < ID_Fields.length; i++){
            try {
                resArray[i] = ID_Fields[i].getInt(null);
                values[i]=getResources().getResourceEntryName(resArray[i]);
                Log.v("resArray[i]  " , getResources().getResourceEntryName(resArray[i]));

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);
             listView.setAdapter(adapter);
             listView.setOnItemClickListener(new OnItemClickListener(){







                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    iJustClickedAnItemInTheList((String) listView.getSelectedItem());


                }
             });
private void iJustClickedAnItemInTheList(String idname) {
        setContentView(layoutIds.get(idname));
        ArrayList<Button> allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
    }

    public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
        ArrayList<T> result = new ArrayList<T>();
        for (View view : getAllViewsFromRoots(root)) 
            if (clazz.isInstance(view)) 
                result.add(clazz.cast(view));
        return result;
    }

    public static ArrayList<View> getAllViewsFromRoots(View...roots) {
        ArrayList<View> result = new ArrayList<View>();
        for (View root : roots)
            getAllViews(result, root);
        return result;
    }

    private static void getAllViews(ArrayList<View> allviews, View parent) {
        allviews.add(parent);
        if (parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup)parent;
            for (int i = 0; i < viewGroup.getChildCount(); i++)
                getAllViews(allviews, viewGroup.getChildAt(i));
        }
    }

最终我的班级 MySimpleArrayAdapter:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.troisieme, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.troisieme, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.myTextView);
    //ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // Change the icon for Windows and iPhone
    String s = values[position];


    return rowView;
  }
} 
4

1 回答 1

1

Buttons要从单个布局中获取所有内容:

public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
    ArrayList<T> result = new ArrayList<T>();
    for (View view : getAllViewsFromRoots(root)) 
        if (clazz.isInstance(view)) 
            result.add(clazz.cast(view));
    return result;
}

public static ArrayList<View> getAllViewsFromRoots(View...roots) {
    ArrayList<View> result = new ArrayList<View>();
    for (View root : roots)
        getAllViews(result, root);
    return result;
}

private static void getAllViews(ArrayList<View> allviews, View parent) {
    allviews.add(parent);
    if (parent instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)parent;
        for (int i = 0; i < viewGroup.getChildCount(); i++)
            getAllViews(allviews, viewGroup.getChildAt(i));
    }
}

MainActivity你的电话

ArrayList<Button> allButtonsOnCurrentLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);

现在,您拥有当前布局中的ArrayList所有内容。Buttons您可以迭代列表并使用已显示的反射获取他们的 ID,或显示他们的标签等。

编辑:我最后一次尝试帮助,用你自己的扩展下面的代码

public class MainActivity extends Activity {
    Map<String, Integer> layoutIds = new HashMap<String, Integer>();
    ArrayList<String> arrayForArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(Field field : R.layout.class.getFields()){
            try {
                int id = field.getInt(null);
                String name = getResources().getResourceEntryName(id);
                layoutIds.put(name, id);
                arrayForArrayAdapter.add(name);
            } 
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    private void iJustClickedAnItemInTheList(String idname) {
        setContentView(layoutIds.get(idname));
        ArrayList<Button> allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
    }

    public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
        ArrayList<T> result = new ArrayList<T>();
        for (View view : getAllViewsFromRoots(root)) 
            if (clazz.isInstance(view)) 
                result.add(clazz.cast(view));
        return result;
    }

    public static ArrayList<View> getAllViewsFromRoots(View...roots) {
        ArrayList<View> result = new ArrayList<View>();
        for (View root : roots)
            getAllViews(result, root);
        return result;
    }

    private static void getAllViews(ArrayList<View> allviews, View parent) {
        allviews.add(parent);
        if (parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup)parent;
            for (int i = 0; i < viewGroup.getChildCount(); i++)
                getAllViews(allviews, viewGroup.getChildAt(i));
        }
    }
}

如果这对您没有帮助,我不知道会怎样。现在用于allButtonsInLayout显示Buttonid 或任何您想要对所有buttons.

于 2013-01-24T13:29:44.177 回答