如何在listfragment中使用baseadapter显示自定义列表视图?

时间:2012-06-08 13:41:22

标签: android listview android-listfragment

我想在listfragment中使用基本适配器进行自定义列表视图

我试试这段代码:

public class MyListFragment1 extends ListFragment {

ImageView back;
String url = Main.url;
String Qrimage;
Bitmap bmp;
ListView list;

AppetiserFragment adapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View tmp_view = inflater.inflate(R.layout.applistviewfragment, container, false);
    ListView list = (ListView) tmp_view.findViewById(R.id.list);
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url + "test.php3");
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("Log", "Error in Connection" + e.toString());

        // Intent intent = new Intent(ViewQRCode.this, PimCarder.class);

        // startActivity(intent);

    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        jArray = new JSONObject(result);
        JSONArray json = jArray.getJSONArray("appetiser");

        adapter = new AppetiserFragment(getActivity(), json);
        list.setAdapter(adapter);


    } catch (Exception e) {
        // TODO: handle exception
        Log.e("log", "Error in Passing data" + e.toString());
    }
    return tmp_view;

}


   }

AppetiserFragment.java

public class AppetiserFragment extends BaseAdapter {
public static ArrayList<String> arr = new ArrayList<String>();
public static ArrayList<String> itemprice = new ArrayList<String>();
public static ArrayList<Bitmap> image = new ArrayList<Bitmap>();
String url = Main.url;
public Context Context;
String qrimage;
Bitmap bmp, resizedbitmap;
Bitmap[] bmps;
Activity activity = null;
private LayoutInflater inflater;

private ImageView[] mImages;
String[] itemimage;
TextView[] tv;
String itemname, price, desc, itemno;
String[] itemnames, checkeditems, itemnos;
String[] prices;
String[] descs;
HashMap<String, String> map = new HashMap<String, String>();

public AppetiserFragment(Context context, JSONArray imageArrayJson) {
    Context = context;
    // inflater =

    System.out.println(imageArrayJson);
    // (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // imageLoader=new ImageLoader(activity);
    inflater = LayoutInflater.from(context);
    this.mImages = new ImageView[imageArrayJson.length()];
    this.bmps = new Bitmap[imageArrayJson.length()];
    this.itemnames = new String[imageArrayJson.length()];
    this.prices = new String[imageArrayJson.length()];
    this.descs = new String[imageArrayJson.length()];
    this.itemnos = new String[imageArrayJson.length()];
    try {

        for (int i = 0; i < imageArrayJson.length(); i++) {
            JSONObject image = imageArrayJson.getJSONObject(i);

            qrimage = image.getString("itemimage");
            itemname = image.getString("itemname");
            itemno = new Integer(i + 1).toString();
            price = image.getString("price");
            desc = image.getString("itemdesc");
            **System.out.println(price);**
            itemnames[i] = itemname;
            prices[i] = price;
            descs[i] = desc;
            itemnos[i] = itemno;

            byte[] qrimageBytes = Base64.decode(qrimage.getBytes());

            bmp = BitmapFactory.decodeByteArray(qrimageBytes, 0,
                    qrimageBytes.length);
            int width = 100;
            int height = 100;
            resizedbitmap = Bitmap.createScaledBitmap(bmp, width, height,
                    true);
            bmps[i] = bmp;

            mImages[i] = new ImageView(context);
            mImages[i].setImageBitmap(resizedbitmap);

            mImages[i].setScaleType(ImageView.ScaleType.FIT_START);

            // tv[i].setText(itemname);
        }
        System.out.println(map);

    } catch (Exception e) {
        // TODO: handle exception
    }
}

public AppetiserFragment() {
    // TODO Auto-generated constructor stub
}

public int getCount() {
    return mImages.length;
}

public Object getItem(int position) {
    return position;
}

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

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

    View view = convertView;
    final ViewHolder viewHolder;
    if (view == null) {
        view = inflater.inflate(R.layout.appetiserlistview, null);

        viewHolder = new ViewHolder();
        viewHolder.image = (ImageView) view
                .findViewById(R.id.appetiserimage);
        viewHolder.text = (TextView) view.findViewById(R.id.appetisertext);
        viewHolder.desc = (TextView) view.findViewById(R.id.appetiserdesc);
        viewHolder.price = (TextView) view
                .findViewById(R.id.appetiserprice);
        viewHolder.appitemnum = (TextView) view
                .findViewById(R.id.appitemno);

        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }
    viewHolder.image.setImageBitmap(bmps[position]);
    viewHolder.appitemnum.setText(itemnos[position]);
    viewHolder.price.setText(prices[position]);
    viewHolder.desc.setText(descs[position]);
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(itemnames[position]);

    return view;
}

static class ViewHolder {

    protected TextView text, price, desc, appitemnum;
    protected ImageView image;
    public static CheckBox checkbox = null;
}

 }

我可以打印价格值。如果我单独作为自定义列表视图,我可以图像,文本,desc,价格。但是在MyFragmentlist1中扩展了listfragment意味着我无法在自定义列表视图中查看。我认为这个班级中的MyListFragment1只有我有问题。

2 个答案:

答案 0 :(得分:1)

首先创建一个单独的布局layout_txtview。我只是使用textview。您可以根据要在单行中显示的项目创建布局。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall" />

现在使用此布局在自定义适配器的getView()方法中进行扩充。 现在在MyListFragment1类的onCreate()方法中,只需将适配器设置为自定义适配器

setAdapter(new MyListFragment1 (<apss your constructor argument>));

及其完成。

答案 1 :(得分:0)

尝试以这种方式移动setAdapter回调中的onActivityCreated

 @Override   
 public void onActivityCreated(Bundle savedInstanceState) {
                ListView list = getListView();
                list.setAdapter(adapter);
                adapter.notifyDataSetChanged();
        }