2012-09-02 12 views
8

मेरे पास एक ऐप है जो सूचीदृश्य में डेटा दिखा रहा है, लेकिन मुझे पहले पंक्ति को अलग-अलग लेआउट द्वारा फुलाया जाना चाहिए, और मैंने ऐसा किया लेकिन चूंकि बड़ी संख्या में लिस्टिटम्स हैं, इसलिए मैं सूचीदृश्य को अनुकूलित करना चाहता हूं और मुद्दा है। मैं नहीं कर सकते सूचीदृश्य अनुकूलन जब कि रास्ते में iam भरने सूचीदृश्य, तो मैं कैसे सामग्री कि सूचीदृश्य हैडर डायन अंदर मुट्ठी पंक्ति में जाना चाहिए कुछ लेआउट द्वारा फुलाया जाता है डाल सकते हैं, यहाँ एडाप्टरएंड्रॉइड लिस्टव्यू हेडर

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); 

    LinearLayout content = (LinearLayout) findViewById(R.id.content); 
    LinearLayout refLayout = (LinearLayout) findViewById(R.id.refLayout); 

    refLayout.setVisibility(View.GONE); 

    mBtnNaslovnica = (Button) findViewById(R.id.mBtnNaslovnica); 
    mBtnNaslovnica.setSelected(true); 
    TextView txtView=(TextView) findViewById(R.id.scroller); 
txtView.setSelected(true); 
    loadPage(); 
    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); 
     } 
    }); 

} 

public void loadPage(){ 

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



     JSONObject jsonobj; 
     try { 
      jsonobj = new JSONObject(getIntent().getStringExtra("json")); 
      JSONObject datajson = jsonobj.getJSONObject("data"); 
      JSONArray news = datajson.getJSONArray(TAG_NEWS); 
      JSONArray actual = datajson.getJSONArray("actual"); 
      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); 
       String big_img = c.getString(TAG_BIG_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_BIG_IMG, big_img); 
       map.put(TAG_DATETIME, datetime); 
       map.put(TAG_AUTHOR, author); 

       // adding HashList to ArrayList 
       homeList.add(map);} 
      for(int i = 0; i < actual.length(); i++){ 
       JSONObject c = actual.getJSONObject(i); 

       // Storing each json item in variable 
       String id = c.getString(TAG_ACT_TIME); 
       String body = c.getString(TAG_ACT_BODY); 
       String anews = " | "+ id+ " " + body; 
       String cur_anews = ((TextView) findViewById(R.id.scroller)).getText().toString(); 
       String complete = anews + cur_anews; 
       TextView anewstv = (TextView) findViewById(R.id.scroller); 
       anewstv.setText(complete); 

       } 
     } 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.tVdatetime)).getText().toString(); 
      String cur_actual = ((TextView) findViewById(R.id.scroller)).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); 
      i.putExtra("CUR_ACTUAL", cur_actual); 
      startActivity(i); 
     } 
    });  
} 

public void reloadPage(String jsonstring){ 
    ArrayList<HashMap<String, String>> homeList = new ArrayList<HashMap<String, String>>(); 



    JSONObject jsonobj; 
    try { 
     jsonobj = new JSONObject(jsonstring); 
     JSONObject datajson = jsonobj.getJSONObject("data"); 
     JSONArray news = datajson.getJSONArray(TAG_NEWS); 
     JSONArray actual = datajson.getJSONArray("actual"); 
     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); 
      String big_img = c.getString(TAG_BIG_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_BIG_IMG, big_img); 
      map.put(TAG_DATETIME, datetime); 
      map.put(TAG_AUTHOR, author); 

      // adding HashList to ArrayList 
      homeList.add(map);} 
     for(int i = 0; i < actual.length(); i++){ 
      JSONObject c = actual.getJSONObject(i); 

      // Storing each json item in variable 
      String id = c.getString(TAG_ACT_TIME); 
      String body = c.getString(TAG_ACT_BODY); 
      String anews = " | "+ id+ " " + body; 
      String cur_anews = ((TextView) findViewById(R.id.scroller)).getText().toString(); 
      String complete = anews + cur_anews; 
      TextView anewstv = (TextView) findViewById(R.id.scroller); 
      anewstv.setText(complete); 

      } 
    } 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.tVdatetime)).getText().toString(); 
     String cur_actual = ((TextView) findViewById(R.id.scroller)).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); 
     i.putExtra("CUR_ACTUAL", cur_actual); 
     startActivity(i); 
    } 
}); 
} 

public void startNewActivity(){ 

} 
public class LoadingTask extends AsyncTask<String, Object, Object>{ 
    XMLParser parser = new XMLParser(); 
    JSONParser jParser = new JSONParser(); 
    LinearLayout content = (LinearLayout) findViewById(R.id.content); 
    LinearLayout refLayout = (LinearLayout) findViewById(R.id.refLayout); 

    protected void onPreExecute(){ 
     content.setClickable(false); 
     refLayout.setVisibility(View.VISIBLE); 
    } 

    @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){ 
     String json; 
     json = result.toString(); 
     reloadPage(json); 
     refLayout.setVisibility(View.GONE); 
    } 


} 



} 

उत्तर

18

अगर मैं मिल गया का कोड है आपका बिंदु - आप 2 दृष्टिकोणों के साथ जा सकते हैं:

  1. शीर्षलेख जोड़ें सूची में देखें। यह आपके दृश्य को बढ़ाने के लिए बस इतना आसान है और इसे अपनी सूची के addHeaderView(View) पर पास करें। नोट: एडाप्टर सेट करने से पहले आपको यह दृश्य जोड़ना होगा, या यह अपवाद फेंक देगा।

  2. हालांकि, जैसा कि आपका 'हेडर' अन्य सभी वस्तुओं के समान डेटा का प्रतिनिधित्व कर रहा है, लेकिन इसमें अलग-अलग लेआउट हैं - मेरा सुझाव है कि यहां हेडर का उपयोग न करें। इसके बजाय, अपने एडाप्टर में getItemViewType() को लागू करने का प्रयास करें। http://developer.android.com/reference/android/widget/BaseAdapter.html#getItemViewType(int) यदि आप करेंगे - आपको यह जांचने की क्षमता होगी कि getView() विधि में किस प्रकार का लेआउट वापस आना है। और एंड्रॉइड आपके लिए फुले हुए दृश्यों को अनुकूलित और पुन: उपयोग करने का ख्याल रखेगा, ताकि आप सुनिश्चित हो सकें कि convertView, आपके getView पर सही प्रकार और लेआउट का होगा।

अगर मुझे अधिक जानकारी के साथ समझाया जाए तो कृपया मुझे बताएं।

+0

यदि आपके पास पर्याप्त :) THX समझाने था –

0

आप इस तरह यह कर सकते हैं:

... 
convertView.setOnClickListener(new OnItemClick(position)); 
... 
public class OnItemClick implements OnClickListener { 
    private final int index; 
    public OnItemClick(int _index) { 
      this.index = _index; 
     } 
    @Override 
    public void onClick(View v) { 
     //dosomething 
    } 
} 
संबंधित मुद्दे