我在Android中创建了一个可扩展的列表视图,但我不知道如何使用Intent将childPosition设置为打开Activity。 我的可扩展阵列表代码是Bellow:
package com.am.expandablelist2;
import java.util.HashMap;
import java.util.List;
import android.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader; //Header Title
//Child Title
private HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listDataChild){
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listDataChild;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null){
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(com.am.expandablelist2.R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(com.am.expandablelist2.R.id.tvListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this.listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(com.am.expandablelist2.R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(com.am.expandablelist2.R.id.tvListHeader);
lblListHeader.setTypeface(null,Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
和我的主要活动如下:
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the listView
expListView = (ExpandableListView) findViewById(R.id.lvExp);
//Preparing List Data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
//Setting List Adapter
expListView.setAdapter(listAdapter);
//List View Group Click Listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
long id) {
//Toast.makeText(getApplicationContext(), "Group Clicked " + listDataHeader.get(groupPosition), Toast.LENGTH_SHORT).show();
return false;
}
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show();
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " Collapsed", Toast.LENGTH_SHORT).show();
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + ":" + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void prepareListData() {
// TODO Auto-generated method stub
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
//Adding Group Data
listDataHeader.add("گروه E");
listDataHeader.add("گروه F");
listDataHeader.add("گروه C");
listDataHeader.add("گروه D");
//Adding Child Data
List<String> groupE = new ArrayList<String>();
groupE.add("EXW");
List<String> groupF = new ArrayList<String>();
groupF.add("FCA");
groupF.add("FAS");
groupF.add("FOB");
List<String> groupC = new ArrayList<String>();
groupC.add("CFR");
groupC.add("CIF");
groupC.add("CPT");
groupC.add("CIP");
List<String> groupD = new ArrayList<String>();
groupD.add("DAF");
groupD.add("DES");
groupD.add("DEQ");
groupD.add("DDU");
groupD.add("DDP");
listDataChild.put(listDataHeader.get(0), groupE);
listDataChild.put(listDataHeader.get(1), groupF);
listDataChild.put(listDataHeader.get(2), groupC);
listDataChild.put(listDataHeader.get(3), groupD);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
如果有人可以帮助我在主要活动中添加意图,我感到很高兴。
答案 0 :(得分:0)
如果我理解你的问题,请告诉我:
您希望在选择/单击子列表项时启动活动。 ?
答案 1 :(得分:0)
最后我找到了我的问题的答案,但如果有人能提供更好的解决方案,我们将不胜感激。
实际上我在onChildClick(ExpandableListView ...)方法中添加了以下代码:
String ldc = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);
if (ldc == "EXW") {
Intent intent = new Intent(getApplicationContext(), EXW.class);
startActivity(intent);
}
if (ldc == "FCA") {
Intent intent = new Intent(getApplicationContext(), FCA.class);
startActivity(intent);
}
请注意我创建了两个名为EXW和FCA的Activity。它们的名称与子列表视图中的子项名称完全相同。现在,当我选择每个EXW或FCA子项时,它们的相关活动将打开。我可以为每个子项创建一个活动项目与上面相同。