m0_37686614
烂牌大师9527
采纳率92.9%
2017-07-29 04:58 浏览 951
已采纳

求大神解答unfortunately **has stopped

20

package com.example.lifeview;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class FruitAdapter extends ArrayAdapter{

private int resourceId;

private Fruit getItem(int position) {
// TODO Auto-generated method stub
return null;
}

private Context getContext() {
// TODO Auto-generated method stub
return null;
}

public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objects){
    super();
    resourceId=textViewResourceId;
    }

@Override
public View getView(int position,View convertView,ViewGroup parent){
    Fruit fruit=getItem(position);
    View view =LayoutInflater.from(getContext()).inflate(resourceId,null);
    ImageView fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
    TextView fruitName=(TextView)view.findViewById(R.id.fruit_name);
    fruitImage.setImageResource(fruit.getImageId());
    fruitName.setText(fruit.getName());
    return view;
}

}
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答 默认 最新

  • 已采纳
    m0_37686614 烂牌大师9527 2017-07-29 05:00

    主程序
    package com.example.lifeview;

    import java.util.ArrayList;
    import java.util.List;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;

    public class MainActivity extends Activity {
    private List fruitList=new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initFruits();
    FruitAdapter adapter=new FruitAdapter(MainActivity.this ,R.layout.fruit_item,fruitList);

        ListView listView=(ListView)findViewById(R.id.list_view);
        listView.setAdapter( (ListAdapter) adapter);
    
    }
    private void initFruits(){
        Fruit apple=new Fruit("Apple",R.drawable.apple_pic);
        fruitList.add(apple);
        Fruit banana=new Fruit("Banana",R.drawable.banana_pic);
        fruitList.add(banana);
        Fruit orange=new Fruit("Orange",R.drawable.orange_pic);
        fruitList.add(orange);
        Fruit watermelon=new Fruit("Watermelon",R.drawable.watermelon_pic);
        fruitList.add(watermelon);
        Fruit pear=new Fruit("Pear",R.drawable.pear_pic);
        fruitList.add(pear);
        Fruit grape=new Fruit("Grape",R.drawable.grape_pic);
        fruitList.add(grape);
        Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple_pic);
        fruitList.add(pineapple);
        Fruit strawberrt=new Fruit("Strawberrt",R.drawable.strawberrt_pic);
        fruitList.add(strawberrt);
        Fruit cherry=new Fruit("Cherry",R.drawable.cherry_pic);
        fruitList.add(cherry);
        Fruit mango=new Fruit("Mango",R.drawable.mango_pic);
        fruitList.add(mango);
    
    
    
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    }

    点赞 评论 复制链接分享
  • Gleaming_Old_Boy 小妮子老男孩 2017-07-31 02:22

    把你的ArrayAdapter 换成BaseAdapter试试看

    点赞 评论 复制链接分享
  • zxf_2016 X峰 2017-09-14 01:52

    你的adapter不能强转成ListAdapter。把你的FruitAdapter贴出来看下。所以报了这个错。

    点赞 评论 复制链接分享

相关推荐