如何在自定义列表视图中显示所选项目在Android中使用片段显示在同一活动中?

时间:2012-06-06 07:08:54

标签: android android-listview android-fragments

我使用基本适配器类在自定义列表视图中显示了itemimage和itemname。我从listview中选择一个项目图像意味着当时屏幕分为两半。

所有listview项目的一半,我希望另一半窗口显示所选项目。我怎么能这样做?

用于显示itemimage,itemname,itemdesc,自定义列表视图代码中的费率

public class Appetizerlist 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>();

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 Appetizerlist(Context context, JSONArray imageArrayJson) {
    Context = context;
    // inflater =
    // (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 Appetizerlist() {
    // 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);
        // viewHolder.checkbox = (CheckBox) view.findViewById(R.id.bcheck);

        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.checkbox.setTag(itemnames[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;
}

 }

这是我的活动代码

           public class Appetizers extends Activity {
ImageView back;
String url;
String Qrimage;
Bitmap bmp;
GridView list;
Appetizerlist adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.appetizers);
    url=getIntent().getStringExtra("url");

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    InputStream is = null;
    String result = "";
    JSONObject jArray = null;   
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url+"Appetiser.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());
        Toast.makeText(getApplicationContext(), "Error in Connection",
                Toast.LENGTH_SHORT).show();
        //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");
        list=(GridView)findViewById(R.id.appetiserlist);
         adapter=new Appetizerlist(this, json);
         list.setAdapter(adapter);     
      /*  list.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "prakash", 3000).show();

            }
        });*/

        // ListView g = (ListView) findViewById(R.id.gallery);
          //  g.setAdapter(new ImageAdapter(this, json));


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

上面的代码我可以显示自定义列表视图。我想在自定义列表视图中选择一个项目意味着窗口已分为两半。一个用于相同的自定义列表视图另一半我想显示所选项目详细信息。

1 个答案:

答案 0 :(得分:1)

请参阅此链接

如果不使用不同的手机/平板电脑布局............

http://developer.android.com/guide/topics/fundamentals/fragments.html#Example

void showDetails(int index) {
        mCurCheckPosition = index;


            // We can display everything in-place with fragments, so update
            // the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);

            // Check what fragment is currently shown, replace if needed.
            DetailsFragment details = (DetailsFragment)
                    getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index) {
                // Make new fragment to show this selection.
                details = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details, details);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }


    }