问题
what i need is to access the buttons of the clicked item(layout); this code works but just display all buttons,TextView,...of all layouts not the clicked one. if you want to run this code you can add other layouts with buttons for the test thank you for your help here is my layout:
This is 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>
Then my 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));
}
}
And eventually my class 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;
}
}
回答1:
To get all the Buttons
from a single layout:
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));
}
}
from your MainActivity
you call
ArrayList<Button> allButtonsOnCurrentLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
Now you have an ArrayList
with all Buttons
from the current layout. You can iterate the list and fetch their ID's with the reflection you already displayed, or show their tags, et cetera.
Edit: My last attempt to help, extend the code below with your own
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));
}
}
}
If this doens't help you, I do not know what will. Now use allButtonsInLayout
to show the Button
id's, or whatever it is you want to do with all the buttons
.
来源:https://stackoverflow.com/questions/14501835/accessing-buttons-of-every-layout