我需要的是访问所点击项目的按钮(布局);此代码可以工作,但只显示所有布局的所有按钮,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;
}
}
答案 0 :(得分: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
显示Button
个ID,或者您想要对所有buttons
做什么。