我只想确定从expandableListView中单击了哪个groupView。因为我想将点击的groupView的文本发送到另一个活动。我也看过这个链接,但它没有解决我的问题。 Identifying the group that has been clicked in an expandableListView 我也试过发送groupPosition但它仍然没有解决我的问题。 这是我的代码。
CategoryList.java:
package com.remindme.sayem.remindme;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class CategoryList extends AppCompatActivity {
private ExpandableListView categoryListView;
private CategoryListAdapter categoryListAdapter;
List<String> listDataHeader;
HashMap<String, List<String> > listDataChild;
int group_size, child_size;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_list);
categoryListView = (ExpandableListView) findViewById(R.id.categoryListView);
prepareListData();
categoryListAdapter = new CategoryListAdapter(this, listDataHeader, listDataChild);
categoryListView.setAdapter(categoryListAdapter);
categoryListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if(categoryListAdapter.getChildrenCount(groupPosition) == 0){
Intent intent = new Intent(CategoryList.this, ItemEditorActivity.class);
intent.putExtra("GROUP_POSITION", groupPosition);
startActivity(intent);
}
return false;
}
});
}
private void prepareListData(){
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String> >();
// Adding group data
listDataHeader.add("Pharmacy");
listDataHeader.add("Super Shop");
listDataHeader.add("Stationery Shop");
listDataHeader.add("Market");
listDataHeader.add("Hotel");
listDataHeader.add("Hardware shop");
listDataHeader.add("Computer Accessories Shop");
listDataHeader.add("Others");
// Adding child data
List<String> pharmacy = new ArrayList<String>();
pharmacy.add("a");
pharmacy.add("a");
pharmacy.add("a");
pharmacy.add("a");
pharmacy.add("a");
List<String> super_shop = new ArrayList<String>();
List<String> stationery_shop = new ArrayList<String>();
List<String> market = new ArrayList<String>();
List<String> hotel = new ArrayList<String>();
List<String> hardware_shop = new ArrayList<String>();
List<String> computer_accessories_shop = new ArrayList<String>();
List<String> others = new ArrayList<String>();
listDataChild.put(listDataHeader.get(0), pharmacy);
listDataChild.put(listDataHeader.get(1), super_shop);
listDataChild.put(listDataHeader.get(2), stationery_shop);
listDataChild.put(listDataHeader.get(3), market);
listDataChild.put(listDataHeader.get(4), hotel);
listDataChild.put(listDataHeader.get(5), hardware_shop);
listDataChild.put(listDataHeader.get(6), computer_accessories_shop);
listDataChild.put(listDataHeader.get(7), others);
}
public class CategoryListAdapter extends BaseExpandableListAdapter{
private Context _context;
private List<String> _listDataHeader; // Header titles
private HashMap<String, List<String> > _listDataChild; // Child Items
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public CategoryListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
}
@Override
public int getGroupCount() {
group_size = this._listDataHeader.size();
return group_size;
}
@Override
public int getChildrenCount(int groupPosition) {
child_size = this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
//CategoryList.this.group_size = groupPosition;
return child_size;
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = layoutInflater.inflate(R.layout.category_item, null);
}
TextView categoryItem = (TextView) convertView.findViewById(R.id.categoryItem);
categoryItem.setText(getGroup(groupPosition).toString());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = layoutInflater.inflate(R.layout.item_name, null);
}
TextView itemName = (TextView) convertView.findViewById(R.id.itemName);
itemName.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
ItemEditorActivity.java:
package com.remindme.sayem.remindme;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ItemEditorActivity extends AppCompatActivity implements View.OnClickListener{
EditText itemEditorEditText;
Button doneButton;
Button addDataButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_editor);
itemEditorEditText = (EditText) findViewById(R.id.itemEditorEditText);
doneButton = (Button) findViewById(R.id.doneButton);
addDataButton = (Button) findViewById(R.id.addDataButton);
addDataButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addDataButton:
String itemEditorString = itemEditorEditText.getText().toString();
ItemListDatabase itemListDatabase = new ItemListDatabase(ItemEditorActivity.this);
itemListDatabase.open();
if (getIntent().getIntExtra("GROUP_POSITION", 0) == 0)
{
itemListDatabase.insertDataIntoPharmacy(itemEditorString);
Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();
itemEditorEditText.setText("");
}
else if (getIntent().getIntExtra("GROUP_POSITION", 0) == 1){
itemListDatabase.insertDataIntoSuperShop(itemEditorString);
Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();
itemEditorEditText.setText("");
}
else if (getIntent().getIntExtra("GROUP_POSITION", 0) == 2){
itemListDatabase.insertDataIntoStationeryShop(itemEditorString);
Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();
itemEditorEditText.setText("");
}
else if (getIntent().getIntExtra("GROUP_POSITION", 0) == 3){
itemListDatabase.insertDataIntoMarket(itemEditorString);
Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();
itemEditorEditText.setText("");
}
else if (getIntent().getIntExtra("GROUP_POSITION", 0) == 4){
itemListDatabase.insertDataIntoHotel(itemEditorString);
Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();
itemEditorEditText.setText("");
}
else if (getIntent().getIntExtra("GROUP_POSITION", 0) == 5){
itemListDatabase.insertDataIntoHardwareShop(itemEditorString);
Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();
itemEditorEditText.setText("");
}
else if (getIntent().getIntExtra("GROUP_POSITION", 0) == 6){
itemListDatabase.insertDataIntoComputerAccessoriesShop(itemEditorString);
Toast.makeText(this, "Added", Toast.LENGTH_SHORT).show();
itemEditorEditText.setText("");
}
itemListDatabase.close();
break;
case R.id.doneButton:
Intent intent = new Intent(ItemEditorActivity.this, CategoryList.class);
startActivity(intent);
break;
}
}
}
activity_category_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.remindme.sayem.remindme.CategoryList"
>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/categoryListView"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:dividerHeight="0.5dp"
android:divider="#7AA410"
/>
</RelativeLayout>
activity_item_editor.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.remindme.sayem.remindme.ItemEditorActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/itemEditorEditText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Add Data"
android:id="@+id/addDataButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:text="DONE"
android:id="@+id/doneButton" />
</LinearLayout>
</RelativeLayout>
category_item.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="wrap_content"
android:padding="15dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAllCaps="false"
android:textColor="#7AA410"
android:text="Large Text"
android:paddingLeft="30dp"
android:id="@+id/categoryItem" />
</LinearLayout>
item_name.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="wrap_content"
android:padding="15dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAllCaps="false"
android:text="Medium Text"
android:id="@+id/itemName" />
</LinearLayout>
答案 0 :(得分:2)
<强> EDIT_CODE 强>
我在 CategoryList.java 中更改了categoryListView.setOnGroupClickListener()方法。
categoryListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if(categoryListAdapter.getChildrenCount(groupPosition) == 0){
Intent intent = new Intent(CategoryList.this, ItemEditorActivity.class);
String text = categoryListAdapter.getGroupText(groupPosition);
intent.putExtra("GROUP_POSITION", text);
Toast.makeText(CategoryList.this, "Group Position: " + text, Toast.LENGTH_LONG).show();
startActivity(intent);
}
return false;
}
});
我还在 CategoryList.java 中的CategoryListAdapter类中添加了一个新函数getGroupText()。
public String getGroupText(int groupPosition){
return this._listDataHeader.get(groupPosition).toString();
}
之后我像这样编辑了 ItemEditorActivity.java 。
package com.remindme.sayem.remindme;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ItemEditorActivity extends AppCompatActivity implements View.OnClickListener{
EditText itemEditorEditText;
Button doneButton;
Button addDataButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_editor);
itemEditorEditText = (EditText) findViewById(R.id.itemEditorEditText);
doneButton = (Button) findViewById(R.id.doneButton);
addDataButton = (Button) findViewById(R.id.addDataButton);
addDataButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
@Override
protected void onPause() {
super.onPause();
finish();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addDataButton:
ItemListDatabase itemListDatabase = new ItemListDatabase(this);
String itemEditorString = itemEditorEditText.getText().toString();
ItemNameClass itemNameClass = new ItemNameClass(itemEditorString);
String group_position = getIntent().getStringExtra("GROUP_POSITION");
if (group_position.equals("Pharmacy"))
{
itemListDatabase.insertDataIntoPharmacy(itemNameClass);
Toast.makeText(getApplicationContext(), "Added to " + group_position, Toast.LENGTH_LONG).show();
itemEditorEditText.setText("");
}
else if (group_position.equals("Super Shop")) {
itemListDatabase.insertDataIntoSuperShop(itemNameClass);
Toast.makeText(getApplicationContext(), "Added to " + group_position, Toast.LENGTH_LONG).show();
itemEditorEditText.setText("");
}
else if (group_position.equals("Stationery Shop")) {
itemListDatabase.insertDataIntoStationeryShop(itemNameClass);
Toast.makeText(getApplicationContext(), "Added to " + group_position, Toast.LENGTH_LONG).show();
itemEditorEditText.setText("");
}
else if (group_position.equals("Market")) {
itemListDatabase.insertDataIntoMarket(itemNameClass);
Toast.makeText(getApplicationContext(), "Added to " + group_position, Toast.LENGTH_LONG).show();
itemEditorEditText.setText("");
}
else if (group_position.equals("Hotel")) {
itemListDatabase.insertDataIntoHotel(itemNameClass);
Toast.makeText(getApplicationContext(), "Added to " + group_position, Toast.LENGTH_LONG).show();
itemEditorEditText.setText("");
}
else if (group_position.equals("Hardware Shop")) {
itemListDatabase.insertDataIntoHardwareShop(itemNameClass);
Toast.makeText(getApplicationContext(), "Added to " + group_position, Toast.LENGTH_LONG).show();
itemEditorEditText.setText("");
}
else if (group_position.equals("Computer Accessories Shop")){
itemListDatabase.insertDataIntoComputerAccessoriesShop(itemNameClass);
Toast.makeText(getApplicationContext(), "Added to " + group_position, Toast.LENGTH_LONG).show();
itemEditorEditText.setText("");
}
break;
case R.id.doneButton:
Intent intent = new Intent(ItemEditorActivity.this, CategoryList.class);
startActivity(intent);
break;
}
}
}