gpt4 book ai didi

java - 使用 AsyncTask 用多个项目填充 ExpandableListView

转载 作者:行者123 更新时间:2023-11-29 18:37:41 27 4
gpt4 key购买 nike

我想用自定义适配器填充 ExpandableListView。我在网上找到的是小时候只有一个 child (一个 TextView)。就我而言,我想让它在后台任务后填充多个子项和数据。

请帮我找出我做错了什么或者我应该如何处理错误,因为我不断得到

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.put(java.lang.Object, java.lang.Object)' on a null object reference
at com.infy.texpetrol.Activity.CreditReportActivity$GetData.onPostExecute

CreditReportActivity

public class CreditReportActivity extends AppCompatActivity {

private static final String TAG = "CreditBillActivity";

private ConnectionClass connectionClass;
private ProgressDialog progressDialog;

private List<String> cListPetrol, cListDIESEL, cListSPEED, cListOIL;
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_credit_report);
String empCode = getIntent().getStringExtra("empCode");
String shift = getIntent().getStringExtra("shift");
String date = getIntent().getStringExtra("date");
connectionClass = new ConnectionClass();

expListView = findViewById(R.id.lvExpandable);

GetData getData = new GetData();
getData.execute(empCode);

}

@SuppressLint("StaticFieldLeak")
private class GetData extends AsyncTask {

boolean bPetrolRS = false;
boolean bDesiRS = false;
boolean bSpedRS = false;
boolean bOilRS = false;

@Override
protected Object doInBackground(Object[] objects) {
String empCode = objects[0].toString();
try {
Connection con = connectionClass.CONN();

String queryPetrol, queryDIESEL, queryOIL, querySPEED;

queryDIESEL = "Some SQL QUERY";
queryPetrol = "Some SQL QUERY";
queryOIL = "Some SQL QUERY";
querySPEED = "Some SQL QUERY";

if (con != null) {
Statement statement1 = con.createStatement();
Statement statement2 = con.createStatement();
Statement statement3 = con.createStatement();
Statement statement4 = con.createStatement();

ResultSet rsPetrol = statement1.executeQuery(queryPetrol);
ResultSet rsDIESEL = statement2.executeQuery(queryDIESEL);
ResultSet rsOIL = statement3.executeQuery(queryOIL);
ResultSet rsSPEED = statement4.executeQuery(querySPEED);

listDataHeader = new ArrayList<>();
cListPetrol = new ArrayList<>();
cListDIESEL = new ArrayList<>();
cListSPEED = new ArrayList<>();
cListOIL = new ArrayList<>();

Log.d(TAG, "doInBackground: rsPetrol " + rsPetrol.next());

if (rsPetrol.next()) {
while (rsPetrol.next()) {
bPetrolRS = true;
cListPetrol.add(rsPetrol.getString("PartyName")
+ "#.#" + rsPetrol.getString("QTY")
+ "#.#" + rsPetrol.getString("Amount"));
}
} else {
bPetrolRS = false;
cListPetrol.add("NONE");
}

Log.d(TAG, "doInBackground: rsDIESEL " + rsDIESEL.next());

if (rsDIESEL.next()) {
while (rsDIESEL.next()) {
bDesiRS = true;
cListDIESEL.add(rsDIESEL.getString("PartyName")
+ "#.#" + rsDIESEL.getString("QTY")
+ "#.#" + rsDIESEL.getString("Amount"));
}
} else {
bDesiRS = false;
cListDIESEL.add("NONE");

}
Log.d(TAG, "doInBackground: rsSPEED " + rsSPEED.next());

if (rsSPEED.next()) {
while (rsSPEED.next()) {
bSpedRS = true;
cListSPEED.add(rsSPEED.getString("PartyName")
+ "#.#" + rsSPEED.getString("QTY")
+ "#.#" + rsSPEED.getString("Amount"));
}
} else {
bSpedRS = false;
cListSPEED.add("NONE");
}

Log.d(TAG, "doInBackground: rsOIL " + rsOIL.next());

if (rsOIL.next()) {
while (rsOIL.next()) {
bOilRS = true;
cListOIL.add(rsOIL.getString("PartyName")
+ "#.#" + rsOIL.getString("QTY")
+ "#.#" + rsOIL.getString("Amount"));
}
} else {
bSpedRS = false;
cListOIL.add("NONE");
}

} else {
Log.d(TAG, "doInBackground: Error in Connection");
}

} catch (Exception e) {
bPetrolRS = false;
Log.d(TAG, "doInBackground: CATCH " + e.getMessage());
}
return empCode;
}

@Override
protected void onPostExecute(Object o) {
progressDialog.hide();

if (!o.toString().isEmpty()) {

listDataHeader.add("DIESEL");
listDataHeader.add("PETROL");
listDataHeader.add("SPEED");
listDataHeader.add("OIL");

if (bDesiRS) {
Log.d(TAG, "onPostExecute: cListDIESEL " + cListDIESEL.size() + " listDataHeader.get(0) " + listDataHeader.get(0));
listDataChild.put(listDataHeader.get(0), cListDIESEL);
} else {
Toast.makeText(CreditReportActivity.this, "NO DATA IN DIESEL", Toast.LENGTH_SHORT).show();
}

if (bPetrolRS) {
Log.d(TAG, "onPostExecute: cListPetrol " + cListPetrol.size() + " listDataHeader.get(1) " + listDataHeader.get(1));
listDataChild.put(listDataHeader.get(1), cListPetrol); //error gets me here
} else {
Toast.makeText(CreditReportActivity.this, "NO DATA IN PETROL", Toast.LENGTH_SHORT).show();
}

if (bOilRS) {
listDataChild.put(listDataHeader.get(2), cListSPEED);
Log.d(TAG, "onPostExecute: cListSPEED " + cListSPEED.size());
} else {
Toast.makeText(CreditReportActivity.this, "NO DATA IN SPEED", Toast.LENGTH_SHORT).show();
}

if (bSpedRS) {
listDataChild.put(listDataHeader.get(3), cListOIL);
Log.d(TAG, "onPostExecute: cListOIL " + cListOIL.size());
} else {
Toast.makeText(CreditReportActivity.this, "NO DATA IN OIL", Toast.LENGTH_SHORT).show();
}

listAdapter = new ExpandableListAdapter(CreditReportActivity.this, listDataHeader, listDataChild);

expListView.setAdapter(listAdapter);
} else {
Toast.makeText(CreditReportActivity.this, "No Data", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(CreditReportActivity.this);
progressDialog.setMessage("Getting your Report");
progressDialog.show();
}
}
}

ExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

private String[] split;

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) {
return Objects.requireNonNull(this._listDataChild.get(this._listDataHeader.get(groupPosition))).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@SuppressLint("InflateParams")
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

stringSplit(childText);

if (convertView == null) {
LayoutInflater Inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = Inflater.inflate(R.layout.child, null);
}

TextView txtPartyName = convertView.findViewById(R.id.txt_C_partyName);
txtPartyName.setText(split[0]);

TextView product = convertView.findViewById(R.id.txt_C_COUNT);
product.setText(String.valueOf(childPosition));

TextView qty = convertView.findViewById(R.id.txt_C_qty);
qty.setText(split[1]);

TextView amt = convertView.findViewById(R.id.txt_C_amt);
amt.setText(split[2]);

return convertView;
}

private void stringSplit(String childText) {
split = childText.split("#.#");
}

@Override
public int getChildrenCount(int groupPosition) {
return Objects.requireNonNull(this._listDataChild.get(this._listDataHeader.get(groupPosition)))
.size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@SuppressLint("InflateParams")
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.header, null);
}

TextView lblListHeader = convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

最佳答案

您忘记初始化 listDataChild

HashMap<String, List<String>> listDataChild = new HashMap<>(); 

关于java - 使用 AsyncTask 用多个项目填充 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53667401/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com