在尝试为android创建listview时,我需要有关下一步操作的帮助

时间:2014-05-07 03:37:46

标签: java android

所以我在这方面相当初学者,但我想要做的是创建一个包含两张图片和三个文本视图的列表视图。我创建了一个名为自定义列表的类作为我的适配器我遇到的问题是在我列出的主要活动中我无法将该适配器设置为实际列表视图我做错了什么。我尝试将listView添加到oncreate时得到的错误是我需要一个局部变量。

类MainActivity

package j.motors.cars;

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

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

// i have alot going on here can't figure out which way i want to use this array adapter 
public class MainActivity extends Activity {
    private List<Craigslist> myCraigs = new ArrayList<Craigslist>();
    String[] make = {
            "Chevy",
            "Ford",
            "Dodge",
            "Hyundai",
            "Jeep",
            "Toyata",
            "Honda",
            "Kia"
    };
    Integer[] imageId = {
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
    };
    String[] price = {
            "$1500",
            "$3000",
            "$1700",
            "$800"
    };
    Integer[] arrow = {
            R.drawable.imgright,
            R.drawable.imgright,
            R.drawable.imgright,
            R.drawable.imgright,
    };
    String[] description = {
            "This car runs well and has all the right parts",
            "This car is a steal at the price since it barely has 15 miles on it",
            "Almost new condition",
            "This car runs well"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        populateCraigslist();
        CustomList adapter = new
                CustomList(MainActivity.this, make, imageId, price, description, arrow);
        lv = findViewById(R.id.listView1);
        lv.setadapter(adapter)


    }

    private void populateCraigslist() {
        // TODO Auto-generated method stub
        //String make, int imageID, String description, String price, int arrow
        myCraigs.add(new Craigslist("Good", R.drawable.ic_launcher, "Great condition", "$1500", R.drawable.imgright));
        myCraigs.add(new Craigslist("Great", R.drawable.ic_launcher, "Has a few hundred miles on it but it runs in good condition", "3000", R.drawable.imgright));
        myCraigs.add(new Craigslist("Alright", R.drawable.ic_launcher, "Steal even at this price", "2000", R.drawable.imgright));
        myCraigs.add(new Craigslist("Excellent", R.drawable.ic_launcher, "Has a few hundred miles on it", "800", R.drawable.imgright));

    }

    @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;
    }

}

class CustomList

public class CustomList extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] make;
    private final Integer[] imageId;
    private final String[] description;
    private final String[] price;
    private final Integer[] arrow;

    public CustomList(Activity context,
                      String[ ] make, 
                      Integer[] imageId, 
                      String[] description, 
                      String[] price, 
                      Integer[] arrow) {

        super(context, R.layout.row, make);

        this.context = context;
        this.make = make;
        this.imageId = imageId;
        this.arrow = arrow;
        this.price = price;
        this.description = description;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.row, null, true);
        TextView txtDesc = (TextView) rowView.findViewById(R.id.descTV);
        TextView txtMake = (TextView) rowView.findViewById(R.id.makeTV);
        TextView txtPrice = (TextView) rowView.findViewById(R.id.priceTV);
        ImageView iv = (ImageView) rowView.findViewById(R.id.carIV);
        ImageView ivArrow = (ImageView) rowView.findViewById(R.id.arrowIV);
        txtMake.setText(make[position]);
        txtPrice.setText(price[position]);
        txtDesc.setText(description[position]);
        iv.setImageResource(imageId[position]);
        ivArrow.setImageResource(arrow[position]);

        return rowView;
    }
}

3 个答案:

答案 0 :(得分:0)

现在你有一个ArrayAdapter,其中String是参数化类型(在尖括号之间),但你的列表项并不是真正的字符串。至少,您应该将每个列表项的属性封装到某种对象中,然后拥有该对象的列表/数组,而不是多个不同类型的列表/数组。

我真的认为制作自己的适配器(从BaseAdapter扩展)比扩展框架适配器更好。无论如何,您应该观察The World of ListView以了解适配器的工作方式以及如何正确编写适配器。

答案 1 :(得分:0)

你可以将它用于绑定列表视图

package j.motors.cars;

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

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

 public class MainActivity extends Activity { 
   String[] make = {
        "Chevy",
          "Ford",
          "Dodge",
          "Hyundai",
          "Jeep",
          "Toyata",
          "Honda",
          "Kia"
      } ;
      Integer[] imageId = {
          R.drawable.ic_launcher,
          R.drawable.ic_launcher,
          R.drawable.ic_launcher,
          R.drawable.ic_launcher,
      };
      String[ ] price = {
              "$1500",
              "$3000",
              "$1700",
              "$800"
      };
      Integer[] arrow = {
              R.drawable.imgright,
              R.drawable.imgright,
              R.drawable.imgright,
              R.drawable.imgright,
          };
      String[ ] description = {
              "This car runs well and has all the right parts",
              "This car is a steal at the price since it barely has 15 miles on it",
              "Almost new condition",
              "This car runs well"
      };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = findViewById(R.id.listView1);
    TestAdapter adapter = new TestAdapter();
    lv.setadapter(adapter)


}


public class TestAdapter extends BaseAdapter {


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return make.length();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater mInflater = LayoutInflater.from(MainActivity.this);
    final ViewHolder holder;
    if (convertView == null) {

        convertView = mInflater.inflate(
                R.layout.row, null);
        holder = new ViewHolder();

        holder.imageView1 = (ImageView) convertView
                .findViewById(R.id.imageview1);
        holder.imageview2 = (ImageView) convertView
                .findViewById(R.id.imageview2);
                    holder.tex1 = (TextView) convertView.findViewById(R.id.textview1);
                    holder.tex2 = (TextView) convertView.findViewById(R.id.textview2);
                    holder.tex3 = (TextView) convertView.findViewById(R.id.textview3);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
     holder.imageview1.setImageResource(imageId[position]);
         holder.imageview2.setImageResource(arrow[position]);
     holder.txt1.setText(make[position]);
         holder.txt2.setText(price[position]);
         holder.txt3.setText(description[position]);


    return convertView;
}

class ViewHolder {
    TextView txt1,txt2,txt3;
    ImageView imageView1,imageview2;


}

}
}

这是一种在android中集成listview的简单方法。

答案 2 :(得分:0)

更改

`lv = findViewById(R.id.listView1);` 
onCreate()中的

代码 到

lv = (ListView)findViewById(R.id.listView1);

声明

ListView lv;

以上

private List<Craigslist> myCraigs = new ArrayList<Craigslist>();

并将自定义列表更改为关注

    class CustomList extends BaseAdapter 
{
     private final Activity context;
    private LayoutInflater layoutInflater;
     private final String[] make;
    private final Integer[] imageId;
    private final String[] description;
    private final String[] price;
    private final Integer[] arrow;

   class ViewHolder 
   {
     TextView tvMake,tvPrice,tvDesc;
     ImageView ivArrow,iv;
   }

    public CustomList(Activity activity,String[ ] make, Integer[] imageId,
                 String[] description, String[] price, Integer[] arrow) 
    {

        layoutInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        context=activity;

        this.context = context;
        this.make = make;
        this.imageId = imageId;
        this.arrow = arrow;
        this.price = price;
        this.description = description;

    }

    @Override
    public int getCount() {

        int count=make.length;// Set the total list item count
        return count ;
    }
    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


           @Override
    public View getView(int position, View convertView, ViewGroup parent) 
            {

         ViewHolde holder=null;
         if(converView==null)
        {
            convertView=layoutInflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.iv = (ImageView)convertView.findViewById(R.id.imageview1);
            holder.ivArrow = (ImageView)convertView.findViewById(R.id.imageview2);
            holder.tvMake = (TextView) convertView.findViewById(R.id.textview1);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.textview2);
            holder.tvDesc = (TextView) convertView.findViewById(R.id.textview3);

        }
        else
        {
           holder=(ViewHolder)convertView.getTag();
        }



        holder.tvMake.setText(make[position]);
        holder.tvPrice.setText(price[position]);
        holder.tvDesc.setText(description[position]);
        holder.iv.setImageResource(imageId[position]);
        holder.ivArrow.setImageResource(arrow[position]);

        convertView.setTag(holder);

        return convertView;
        }
}