2012-09-01 31 views
2

我有新聞應用程序,從網上下載數據並將其顯示在列表視圖中,但我怎樣才能使第一行是不同的,更大的高度,不同的佈局和東西?安卓在列表視圖中的不同行

這是主要的活動源

public class Home extends Activity { 
    // All static variables 
    static final String URL = "http://dmb.site50.net/application.php?app_access_key=c4ca4238a0b923820dcc509a6f75849b"; 
    // XML node keys 
    static final String KEY_SONG = "song"; // parent node 
    static final String KEY_ID = "id"; 
    static final String KEY_TITLE = "title"; 
    static final String KEY_ARTIST = "artist"; 
    static final String KEY_DURATION = "duration"; 
    static final String KEY_THUMB_URL = "thumb_url"; 

    public static final String TAG_NEWS = "news"; 
    public static final String TAG_ID = "id"; 
    public static final String TAG_TITLE = "title"; 
    public static final String TAG_STORY = "story"; 
    public static final String TAG_SH_STORY = "shorten"; 
    public static final String TAG_DATETIME = "datetime"; 
    public static final String TAG_AUTHOR = "author"; 
    public static final String TAG_IMG = "img"; 

    ListView list; 
    LazyAdapter adapter; 
    Button mBtnNaslovnica; 
    private ViewSwitcher viewSwitcher; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.main); 

     mBtnNaslovnica = (Button) findViewById(R.id.mBtnNaslovnica); 
     mBtnNaslovnica.setSelected(true); 
     TextView txtView=(TextView) findViewById(R.id.scroller); 
    txtView.setSelected(true); 

     ImageButton mBtnRefresh = (ImageButton) findViewById(R.id.btnRefresh); 
     mBtnRefresh.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       new LoadingTask().execute(URL); 
      } 
     }); 


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



       JSONObject jsonobj; 
       try { 
        jsonobj = new JSONObject(getIntent().getStringExtra("json")); 
        JSONArray news = jsonobj.getJSONArray(TAG_NEWS); 
        for(int i = 0; i < news.length(); i++){ 
         JSONObject c = news.getJSONObject(i); 

         // Storing each json item in variable 
         String id = c.getString(TAG_ID); 
         String title = c.getString(TAG_TITLE); 
         String story = c.getString(TAG_STORY); 
         String shorten = c.getString(TAG_SH_STORY); 
         String author = c.getString(TAG_AUTHOR); 
         String datetime = c.getString(TAG_DATETIME); 
         String img = c.getString(TAG_IMG); 


         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_ID, id); 
         map.put(TAG_TITLE, title); 
         map.put(TAG_STORY, story); 
         map.put(TAG_IMG, img); 
         map.put(TAG_DATETIME, datetime); 
         map.put(TAG_AUTHOR, author); 

         // adding HashList to ArrayList 
         homeList.add(map);} 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 






      list=(ListView)findViewById(R.id.list); 

      // Getting adapter by passing xml data ArrayList 
      adapter=new LazyAdapter(this, homeList);   
      list.setAdapter(adapter); 


      // Click event for single list row 
      list.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
        String cur_title = ((TextView) view.findViewById(R.id.title)).getText().toString(); 
        String cur_story = ((TextView) view.findViewById(R.id.einfo2)).getText().toString(); 
        String cur_author = ((TextView) view.findViewById(R.id.einfo1)).getText().toString(); 
        String cur_datetime = ((TextView) view.findViewById(R.id.einfo)).getText().toString(); 
        ImageView cur_img = (ImageView) view.findViewById(R.id.list_image); 
        String cur_img_url = (String) cur_img.getTag(); 

        Intent i = new Intent("com.example.androidhive.CURENTNEWS"); 
        i.putExtra("CUR_TITLE", cur_title); 
        i.putExtra("CUR_STORY", cur_story); 
        i.putExtra("CUR_AUTHOR", cur_author); 
        i.putExtra("CUR_DATETIME", cur_datetime); 
        i.putExtra("CUR_IMG_URL", cur_img_url); 
        startActivity(i); 
       } 
      });  
     } 

    public void loadPage(){ 

    } 

    public void startNewActivity(){ 

    } 
    public class LoadingTask extends AsyncTask<String, Object, Object>{ 
     XMLParser parser = new XMLParser(); 
     JSONParser jParser = new JSONParser(); 
     @Override 
     protected Object doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      String URL = params[0]; 
      JSONObject json = jParser.getJSONFromUrl(URL); 
      //String xml = parser.getXmlFromUrl(URL); // getting XML from URL 
      // getting DOM element 
      return json; 
     } 

     protected void onPostExecute(Object result){ 
      Intent startApp = new Intent("com.example.androidhive.HOME"); 
      startApp.putExtra("json", result.toString()); 
      startActivity(startApp); 
     } 


    } 



    } 
+0

**我怎樣才能讓第一行是不同的,更大的高度,不同的佈局和東西?**如果它只適用於First Row,那麼使用帶有ListView的Header View並在您的自定義適配器的getView中跳過第一行(view) )..簡單.. :-) – user370305

回答

2

您需要修改LazyAdapter代碼爲每一行返回正確的佈局,這取決於你想要的任何標準。 ListView只是使用適配器給出的視圖,所以沒有什麼特別的要讓每行的佈局不同。請注意,要使其正常工作,您還需要實施getItemViewType()getViewTypeCount()