6

我已經將我的android屏幕劃分爲2個片段。在第一個片段中,我將從服務器數據庫製作listview到android mobile。問題是我無法調用getview方法基礎適配器類。請幫我出如何在Android中使用列表片段顯示自定義列表視圖

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) { 
    return inflater.inflate(R.layout.applistviewfragment, container, false); 

} 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    // back=(ImageView)findViewById(R.id.backfoodmenu); 
    /* 
    * back.setOnClickListener(new OnClickListener() { 
    * 
    * public void onClick(View v) { // TODO Auto-generated method stub 
    * Intent intent=new Intent(getApplicationContext(),FoodMenu.class); 
    * intent.putExtra("url", url); startActivity(intent); } }); 
    */ 
    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 + "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"); 

     Context context = null; 
     context = this.getActivity().getApplicationContext(); 
        //(Here i want give lisid from xml) 
        list=(Listview)findViewById(R.id.list);//i type here it shows an error 
     adapter = new AppetiserFragment(context, json); 

     context = getActivity().getApplicationContext(); 
     list.setAdapter(adapter); 

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

}

AppetiserFragment.java

  public class AppetiserFragment extends BaseAdapter { 

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);//here i can print 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); 
     System.out.println("prakash"); 
     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; 

} 

} 

我可以能夠打印在基地adpater類JSON數據。現在我想將json數據傳遞給文本,圖像。我認爲getview方法沒有調用。請幫我

回答

0

我覺得你使用自定義列表與你ID「R.id.list」,所以不需要擴展你的片段與listFragment擴展與片段只。

其他明智的if you want use the listFragment然後在XML

<ListView android:id="@id/android:list" android:layout_height="fill_parent" 
     android:layout_width="fill_parent" android:layout_weight="1.0" 
     android:layout_marginTop="2dip" android:scrollingCache="false" 
     android:clickable="true" android:cacheColorHint="#00000000" 
     android:focusable="true" 
     android:background="@drawable/listfocused" 
     android:dividerHeight="1dip" xmlns:android="http://schemas.android.com/apk/res/android"/> 

在java中獲取列表使用像作爲

ListView list = this.getListView(); 
+0

我一直列表,像你給我的XML。然後我改變了Java代碼,就像ListView list = this.getListView();但是我得到了與data.lang.null指針異常相同的異常 – naveen

+0

看到這段代碼http://developer.android.com/guide/topics/ fundamentals/fragments.html不知道你錯過了代碼..... –

+0

請看我更新的代碼。我想從xml調用列表ID到Java。如果我打電話顯示一個錯誤 – naveen

0
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) {  
... 
    list=(Listview)findViewById(R.id.list);//i type here it shows an error 
... 
} 

這是因爲你正在努力尋找在onCreate()方法的意見;在這個時間點,片段的佈局尚未膨脹,因此所有findViewById()調用都將返回null。

您只能在佈局充氣後找到視圖,位於onCreateView()。因此,請覆蓋onCreateView()方法並呼叫findViewById()並在其中設置列表的適配器!

1

您還可以使用ListActivity而不是ListFragment。 它會表現得像正常的活動,你可以很容易地找到所有列表相關的方法。

這樣的..,

public class MainActivity extends ListActivity { 
oncreate() { 
** code 
ListView list = getListView(); 

**code 
} 
} 
相關問題