gpt4 book ai didi

java - 使用 BaseAdapter 在 ListView 中显示 ArrayList

转载 作者:行者123 更新时间:2023-12-01 11:04:19 25 4
gpt4 key购买 nike

我很难在 ListView 中显示 ArrayList 中的数据。我使用 BaseAdapter 来显示数据。我不知道为什么它不起作用。当这部分执行时,我的应用程序停止工作。这是我的代码:

ItemAnalysisRecord.java

package com.checkaidev1;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ItemAnalysisRecord extends Activity {

Context ctx = this;
ListView listview1;
DatabaseOperations dbOperations;
List<ItemAnalysis> itemAnalysisList;


BaseAdapter myAdapter = new BaseAdapter() {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

if (convertView != null)
return convertView;

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item_analysis_row, parent, false);

TextView itemNumberTextView = (TextView) rowView.findViewById(R.id.itemNumberTextView);
itemNumberTextView.setText(getItemAnalysisData().get(position).getItemNumber());
Log.i("Database Operations", "Evaluation: " + itemNumberTextView);

TextView correctKeyTextView = (TextView) rowView.findViewById(R.id.correctKeyTextView);
correctKeyTextView.setText(getItemAnalysisData().get(position).getCorrectKey());
Log.i("Database Operations", "Evaluation: " + correctKeyTextView);

TextView itemDifficultyTextView= (TextView) rowView.findViewById(R.id.itemDifficultyTextView);
itemDifficultyTextView.setText(Double.toString(getItemAnalysisData().get(position).getItemDifficulty()));
Log.i("Database Operations", "Evaluation: " + itemDifficultyTextView);

TextView itemEvaluationTextView= (TextView) rowView.findViewById(R.id.itemEvaluationTextView);
itemEvaluationTextView.setText(getItemAnalysisData().get(position).getEvaluation());
Log.i("Database Operations", "Evaluation: " + itemEvaluationTextView);

return rowView;

}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
itemAnalysisList = getItemAnalysisData();
return itemAnalysisList.size();


}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_analysis_record);
Log.i("CheckAide", "OnCreate Method Called");

dbOperations = new DatabaseOperations(ctx);

TextView examNameTextView = (TextView)findViewById(R.id.examNameTextView);
examNameTextView.setText("Exam: " + dbOperations.getExam(ExamRecord.listviewIndex).getExamName());
TextView examDateTextView = (TextView)findViewById(R.id.examDateTextView);
examDateTextView.setText("Exam Date: " + dbOperations.getExam(ExamRecord.listviewIndex).getExamDate());


listview1 = (ListView) findViewById(R.id.listview1);

View header = (View)getLayoutInflater().inflate(R.layout.item_analysis_header, null);
listview1.addHeaderView(header);
listview1.setAdapter(myAdapter);
}

public List<ItemAnalysis> getItemAnalysisData()
{
int correctStudent = 0;
String evaluation = null;

List<ItemAnalysis> itemAnalysisList = new ArrayList<ItemAnalysis>();

int itemNumberCtr = dbOperations.getItemCount(ExamRecord.listviewIndex);
int studentIDctr = dbOperations.getStudentCount(ExamRecord.listviewIndex);


for(int x = 1; x <= itemNumberCtr; x++)
{
ItemAnalysis itemAnalysis = new ItemAnalysis();
itemAnalysis.setItemNumber(x);
Log.i("Database Operations", "Item Number: " + x);
String correctKey = dbOperations.getCorrectKey(ExamRecord.listviewIndex, x);
itemAnalysis.setCorrectKey(correctKey);
Log.i("Database Operations", "Correct Key: " + correctKey);
for(int y = 1 ; y <= studentIDctr; y++)
{
String answeredKey = dbOperations.getAnsweredKey(x, y);
Log.i("Database Operations", "Answered Key: " + answeredKey);

if (answeredKey.equalsIgnoreCase(correctKey))
{
correctStudent++;
Log.i("Database Operations", "CorrectStudent: " + correctStudent);

}

}
DecimalFormat df = new DecimalFormat("0.00");
double itemDifficulty = Double.valueOf(df.format((double)correctStudent / (double)studentIDctr));

itemAnalysis.setItemDifficulty(itemDifficulty);
Log.i("Database Operations", "Correct Student: " + correctStudent + "StudentIDCtr:" + studentIDctr + "Item Difficulty " + itemDifficulty);
correctStudent = 0;
if (itemDifficulty <= 0.20)
{
evaluation = "VD";
}
else if (itemDifficulty <= 0.40 && itemDifficulty >= 0.21)
{
evaluation = "D";
}
else if (itemDifficulty <= 0.60 && itemDifficulty >= 0.41)
{
evaluation = "MD";
}
else if (itemDifficulty <= 0.80 && itemDifficulty >= 0.61)
{
evaluation = "E";
}
else if (itemDifficulty <= 1.00 && itemDifficulty >= 0.81)
{
evaluation = "VE";
}

itemAnalysis.setEvaluation(evaluation);
Log.i("Database Operations", "Evaluation: " + evaluation);

itemAnalysisList.add(itemAnalysis);
Log.i("Database Operations", "data added ");

}
return itemAnalysisList;

}


}

项目分析.java

package com.checkaidev1;

public class ItemAnalysis {
public int itemNumber;
public String correctKey;
public double itemDifficulty;
public String evaluation;

// Empty constructor
public ItemAnalysis(){

}

public ItemAnalysis(int itemNumber, String correctKey, double itemDifficulty, String evaluation){
this.itemNumber = itemNumber;
this.correctKey = correctKey;
this.itemDifficulty = itemDifficulty;
this.evaluation = evaluation;

}

// getting name
public int getItemNumber(){
return this.itemNumber;
}

// setting name
public void setItemNumber(int itemNumber){
this.itemNumber = itemNumber;
}

// getting name
public String getCorrectKey(){
return this.correctKey;
}

// setting name
public void setCorrectKey(String correctKey){
this.correctKey = correctKey;
}

public double getItemDifficulty(){
return this.itemDifficulty;
}

// setting name
public void setItemDifficulty(double itemDifficulty){
this.itemDifficulty = itemDifficulty;
}

public String getEvaluation(){
return this.evaluation;
}

// setting name
public void setEvaluation(String evaluation){
this.evaluation = evaluation;
}

}

希望有人能帮我解决这个问题。预先感谢您的帮助。

最佳答案

您的适配器实例定位字段区域。就我而言,当适配器实例使用空(只是'new ArrayList()')arrayList创建时,它不起作用。

所以我在 arrayList 填充数据后将适配器实例化为局部变量。

关于java - 使用 BaseAdapter 在 ListView 中显示 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33096167/

25 4 0