当我点击我的应用上的子视图时,它不响应点击。我几乎没有任何成功地用Google搜索。我尝试调试它,当我触摸孩子时,它不会进入onclick
方法
活动类
public class LetestNews extends ExpandableListActivity{
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is not really necessary as ExpandableListActivity contains an ExpandableList
//setContentView(R.layout.main);
ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
ExpandableAdapter adapter = new ExpandableAdapter(parentItems, childItems);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
//expandableList.setOnChildClickListener(this);
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
/* You must make use of the View v, find the view by id and extract the text as below*/
Toast.makeText(getBaseContext(), "Shaan is a monkey",
Toast.LENGTH_SHORT).show();
return true; // i missed this
}
});
}
public void setGroupParents() {
try {
FileInputStream newsIn = openFileInput("news.txt");
String newsObj = "";
String objEnc="";
Gson gson=new Gson();
BufferedReader inputNews = new BufferedReader(new InputStreamReader(newsIn, "UTF-8"));
StringBuilder sbuilderNews = new StringBuilder();
newsObj = inputNews.readLine();
while (newsObj != null) {
sbuilderNews.append(newsObj);
newsObj = inputNews.readLine();
if (newsObj != null) {
// sbuilder.append("\n");
}
}
newsIn.close();
newsObj = sbuilderNews.toString();
SubredditNews objNews = gson.fromJson(newsObj, SubredditNews.class);
ArrayList<SubredditIN>subredditObjects=objNews.subreddits;
for(int i=0;i<subredditObjects.size();i++){
parentItems.add(subredditObjects.get(i).name);
}
}catch(Exception e){
}
}
public void setChildData() {
// Android
ArrayList<String> child = new ArrayList<String>();
String a="";
/*child.add("Core");
child.add("Games");
childItems.add(child);*/
try {
FileInputStream newsIn = openFileInput("news.txt");
String newsObj = "";
String objEnc="";
Gson gson=new Gson();
BufferedReader inputNews = new BufferedReader(new InputStreamReader(newsIn, "UTF-8"));
StringBuilder sbuilderNews = new StringBuilder();
newsObj = inputNews.readLine();
while (newsObj != null) {
sbuilderNews.append(newsObj);
newsObj = inputNews.readLine();
if (newsObj != null) {
// sbuilder.append("\n");
}
}
newsIn.close();
newsObj = sbuilderNews.toString();
SubredditNews objNews = gson.fromJson(newsObj, SubredditNews.class);
ArrayList<SubredditIN>subredditObjects=objNews.subreddits;
for(int i=0;i<objNews.subreddits.size();i++){
child = new ArrayList<String>();
for(int j=0;j<objNews.subreddits.get(i).posts.size();j++) {
subredditObjects.get(i).setURL();
a=subredditObjects.get(i).getPostLink();
child.add(objNews.subreddits.get(i).posts.get(j));
Log.i("this is the link", a);
}
childItems.add(child);
}
}catch(Exception e){
String dd="";
}
}
}
适配器类
public class ExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public ExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group, null);
}
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
//textView.setText(Html.fromHtml("<a href=http://www.stackoverflow.com> STACK OVERFLOW "));
// textView.setMovementMethod(LinkMovementMethod.getInstance());
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.child, null);
}
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return parentItems.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}