2015-04-23 63 views
0

这是从以前的问题的后续:ImageButton within row of ListView android not working 但经过来自SO大师的建议,有人建议我发布一个新问题。 问题是我有一个不显示任何数据的自定义适配器。我已经研究过其他问题,但没有提供解决方案。自定义适配器没有显示任何项目

在我的主要活动我有几个按钮,其中的一个:待办事项,应该创建一个从SQLite数据库显示一排数据,并根据某些因素(主要是日期),它显示了一个类型的红绿灯这是存储为可绘制的。

这一行中的项目的一部分是一个图像按钮,我希望用户能够点击和图像应该改变。用户应该也可以点击实际的行并开始一个新的活动。

我的问题是没有数据显示。

所以,这里是我的代码:

public class MainActivity extends Activity { 
    // definitions etc ... 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // definitions etc ... 
    } 

    public void ToDo(View v){ // the user has clicked in the ToDo button 
    IgroDatabaseHelper helper = new IgroDatabaseHelper(getBaseContext()); // create instance of SQLIte database 
    numRows = helper.NumEntries("ToDo"); // Get the number of rows in table 
    int i = 1; 
    ArrayList<RowItem> rowItems = new ArrayList<>(); 
    RowItem myItem1; 
    while (i <= numRows){ 
     // get items from database 
     // depending on value select different drawable 
     // put data into List Array of RowItem 
     myItem1 = new RowItem(TheWhat, R.drawable.teamworka, R.drawable.redtrafficlight, R.drawable.checkbox, TheWhenBy); 
        rowItems.add(myItem1); 
     // 
     i = i+ 1; 
    } 

    ListView yourListView = (ListView) findViewById(R.id.list); 
    CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, R.layout.todo_row, rowItems); 
    yourListView.setAdapter(customAdapter); 
} 

的CustomListViewAdapter看起来是这样的:

public class CustomListViewAdapter extends ArrayAdapter<RowItem> { 

Context context; 
ArrayList<RowItem> _rowItems; 

public CustomListViewAdapter(Context context, int resourceId, 
     ArrayList<RowItem> rowItems) { 

    super(context, resourceId); 
    this.context = context; 
    _rowItems = rowItems; 
    System.out.println("I am in the custom Adapter class "+ _rowItems); 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    System.out.println("This is the get view"); 
    View row = convertView; 
    RowItem item = _rowItems.get(position); 

    // you can now get your string and drawable from the item 
    // which you can use however you want in your list 
    String columnName = item.getColumnName(); 
    int drawable = item.getDrawable(); 
    if (row == null) { 
     LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = mInflater.inflate(R.layout.todo_row, parent, false); 

    } 

    ImageButton chkDone = (ImageButton) row.findViewById(R.id.chkDone); 
    chkDone.setOnClickListener(new View.OnClickListener() {    
      @Override 
      public void onClick(View v) { 
       View parentRow = (View) v.getParent(); 
       ListView listView = (ListView) parentRow.getParent(); 
       final int position = listView.getPositionForView(parentRow); 
       System.out.println("I am in position "+ position); 
      } 
    }); 

    return row; 
} 
} 

的RowItem类的样子:

public class RowItem { 
    private String _heading; 
    private int _icon; 
    private int _lights; 
    private int _chkdone; 
    private String _date; 


    public RowItem(String heading, int icon, int lights, int chkDone, String date) { 
     _heading = heading; 
     _icon = icon; 
     _lights = lights; 
     _chkdone = chkDone; 
     _date = date; 

     System.out.println("adding stuff to my rows"); 
     System.out.println("my column Name is " + heading); 
     System.out.println("My drawable int is "+ icon); 

    } 

    public String getColumnName() { 
     System.out.println("column Names is "+ _heading); 
     return _heading; 
    } 

    public int getDrawable() { 
     return _icon; 
    } 

    public int getLights(){ 
     return _lights; 
    } 

    public int getchkDone(){ 
     return _chkdone; 
    } 

    public String getDate(){ 
     return _date; 
    } 
} 

我显然缺少的东西,正如我前面提到的,没有数据显示。我知道有2行项目被传递给CustomListViewAdapter。但是我也知道CustomListViewAdapter中的View getView实际上并没有被调用。

我希望我已经提供了足够的信息/代码,但是如果您觉得我需要进一步解释某些内容,请说。

非常感谢提前!

+1

你的getCount()做什么? – r2DoesInc

+0

我不认为我有一个getCount()... – user3079872

+1

尝试将它添加到您的自定义适配器,然后:“@Override public int getCount(){ return _rowItems.size(); } @Override public Object getItem(int i){ return _rowItems.get(i); (int i){ } return i; }'不知道是否需要其他的,但不应该伤害他们以及:) – Klotor

回答

1

我没有看到getCount()方法。你应该覆盖这样的:

@Override 
    public int getCount() { 
     return _rowItems.getCount(); 
    } 

另外,调用super(context, resourceId, rowItems);也应该修复它。

+0

您最近一次关于调用'super(context,resourceId,rowItems)'的评论;''意味着它不会崩溃。我想我需要发布另外一个问题,因为现在发生的事情是,当我点击一行时,它认为它是从调用类时构建的数组中获得的一行。所以主活动有一些ListArrays被创建,其中一个是在创建主活动时创建的。 – user3079872

+0

谢谢,现在一切都按预期工作! – user3079872

0

您的ListView认为没有要显示的项目。如果您使用自己的数组,则必须重写getCount()方法以指示要显示的项目数。

+0

非常感谢,是的,我错过了getCount()函数。它现在正在工作。非常感谢! – user3079872

相关问题