使用 BaseAdapter 在 ListView 中显示 ArrayList

Posted

技术标签:

【中文标题】使用 BaseAdapter 在 ListView 中显示 ArrayList【英文标题】:Showing ArrayList in ListView using BaseAdapter 【发布时间】:2016-01-10 19:41:00 【问题描述】:

我很难在 ArrayList 中的 ListView 中显示数据。我使用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;

    



ItemAnalysis.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;
    

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

【问题讨论】:

能否请您发布 logcat 错误报告 【参考方案1】:

您的适配器实例定位字段区域。 在我的情况下,适配器实例何时使用空(只是'new ArrayList()')arrayList创建,它不起作用。

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

【讨论】:

【参考方案2】:

在你的活动中删除那个baseadapter并做如下操作

首先创建单独的适配器类,如下所示

public class ItemsAdapter extends BaseAdapter

    List<ItemAnalysis> itemAnalysisList;
    Context mContext;
    public ItemsAdapter(Context context, List<ItemAnalysis> itemAnalysisList1)
    
        this.mContext = context;
        this.itemAnalysisList = itemAnalysisList1;
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        // TODO Auto-generated method stub

        if (convertView != null)
            return convertView;

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

        ItemAnalysis itemDomain = itemAnalysisList.get(position);

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

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

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

        TextView itemEvaluationTextView= (TextView) rowView.findViewById(R.id.itemEvaluationTextView);
        itemEvaluationTextView.setText(itemDomain.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
        return itemAnalysisList.size();


    

然后将Adapter类全局声明为

private ItemsAdapter adapter;

将此getItemAnalysisData() 方法的返回类型更改为void

现在将getItemAnalysisData()方法中的适配器类初始化为

adapter = new ItemsAdapter(this,your_arraylist);
list.setadapter(adapter);

【讨论】:

【参考方案3】:

不要初始化你的ArrayList..否则我在你的代码中没有发现任何问题..

是的,将条件放在getView() 中,如下所示..

试试这个..

  @Override
  public View getView(int position, View convertView, ViewGroup parent) 
    View rowView = convertView;
    if (rowView == null)
      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      rowView = inflater.inflate(R.layout.main, parent, false);
                    //---- your code----
    
    return rowView;
  

【讨论】:

以上是关于使用 BaseAdapter 在 ListView 中显示 ArrayList的主要内容,如果未能解决你的问题,请参考以下文章

BaseAdapter Listview过滤Android

带有 BaseAdapter 的空白 ListView

Android使用BaseAdapter实现复杂的ListView

使用 CardView BaseAdapter OnItemClick 自定义 ListView

带有 BaseAdapter 的 Android ListView 内的 ImageButton

ListView BaseAdapter 中的 EditText 奇怪行为