Java 向多级可扩展LisView的每个子项添加Toast
我发现这一点在我的项目中非常准确,但我还想为每三级(视图的最后一层)子项添加一个不同的toast。我试过这个:Java 向多级可扩展LisView的每个子项添加Toast,java,android,expandablelistview,toast,multi-level,Java,Android,Expandablelistview,Toast,Multi Level,我发现这一点在我的项目中非常准确,但我还想为每三级(视图的最后一层)子项添加一个不同的toast。我试过这个: expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int gr
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(thirdLevelq1.put(q1[0] this, "The first child" , Toast.LENGTH_LONG).show();
return false;
}
});
但它给出了错误。我甚至试着对每个childItem使用相同的toast文本,安装了应用程序,但当我打开适配器时崩溃了。那么,如何正确地添加它们呢
爪哇:
请将此侦听器设置为
expandableListView。在expandableListView
初始化之后,即在setUpAdapter()
之后,当您尝试设置侦听器时,expandableListView
仍然为空,因为您的初始化是在之后进行的
这就是产生NullPointerException的原因。因为设置侦听器时,expandableListView
为空
干杯有崩溃日志吗?Toast.makeText(thirdLevelq1.put(q1[0]这是“第一个孩子”,Toast.LENGTH_LONG)。show();
这在语法上是错误的我知道这是错误的,我的意思是我无法在标题中添加正确的主题。我还添加了崩溃日志,用于使用Toast.makeText(getApplicationContext(),“第一个孩子”,Toast.LENGTH_LONG)show()
相反。你能显示一下在MainAcitvity中,expandableListView.setOnChildClickListener
部分的确切位置吗?现在编辑并添加了文本,我刚刚从github粘贴了相同的java文本,我的列表在这里非常长,相同的代码,但是列表中的行太多了。你在t中获得了组位置和子位置他用它来知道哪个孩子或组被调用了。@Cingen你必须分享更多的代码,这样我才能帮助你,在假设的情况下编写任何东西对我来说都太麻烦了。你可以试试。多亏了@I_A_mok.Cingen,如果你仍然无法实现你想要的,让我知道我会尽力帮助你。是的,你可以尝试根据您的需要,您只需利用回调的groupPosition和childPosition来获得正确的消息,并将其用于您的toast中。您能否显示您将在所有三个列表中拥有的数据,我需要将其可视化以给您准确的答案我会哭,非常感谢
package com.bacon.expandablelistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
String[] parent = new String[]{"What is View?", "What is Layout?", "What is Dynamic Views?"};
String[] q1 = new String[]{"List View", "Grid View"};
String[] q2 = new String[]{"Linear Layout", "Relative Layout"};
String[] q3 = new String[]{"Recycle View"};
String[] des1 = new String[]{"A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen."};
String[] des2 = new String[]{"Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent)."};
String[] des3 = new String[]{"This list contains linear layout information"};
String[] des4 = new String[]{"This list contains relative layout information,Displays a scrolling grid of columns and rows"};
String[] des5 = new String[]{"Under the RecyclerView model, several different components work together to display your data. Some of these components can be used in their unmodified form; for example, your app is likely to use the RecyclerView class directly. In other cases, we provide an abstract class, and your app is expected to extend it; for example, every app that uses RecyclerView needs to define its own view holder, which it does by extending the abstract RecyclerView.ViewHolder class."};
LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
* Second level array list
*/
List<String[]> secondLevel = new ArrayList<>();
/**
* Inner level data
*/
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//The problem starts here
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(), "The first child", Toast.LENGTH_LONG).show();
return false;
}
});
setUpAdapter();
}
private void setUpAdapter() {
secondLevel.add(q1);
secondLevel.add(q2);
secondLevel.add(q3);
thirdLevelq1.put(q1[0], des1);
thirdLevelq1.put(q1[1], des2);
thirdLevelq2.put(q2[0], des3);
thirdLevelq2.put(q2[1], des4);
thirdLevelq3.put(q3[0], des5);
data.add(thirdLevelq1);
data.add(thirdLevelq2);
data.add(thirdLevelq3);
expandableListView = (ExpandableListView) findViewById(R.id.expandible_listview);
//passing three level of information to constructor
ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data);
expandableListView.setAdapter(threeLevelListAdapterAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
expandableListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
}
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setOnChildClickListener(android.widget.ExpandableListView$OnChildClickListener)' on a null object reference
@Override
public void onFinalChildClick(int plpos, int slpos, int tlpos) {
String msg = "";
switch (tlpos) {
case 0:
msg = "Shakespear is a good poet";
break;
case 1:
msg = "Earth isn't flat";
break;
default:
msg = "Unknown";
}
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onFinalItemClick(String plItem, String slItem, String tlItem) {
String inMsg = plItem + ", " + slItem + ", " + tlItem;
String outMsg = "";
if (inMsg.equals("group 1, Child Level 1, A")){
outMsg = "Shakespear is a good poet";
} else if (inMsg.equals("group 1, Child Level 1, B")){
outMsg = "Earth isn't flat";
} else {
outMsg = "Unknown";
}
Toast.makeText(this, outMsg, Toast.LENGTH_SHORT).show();
}