在列表视图的Andr​​oid不同行 [英] Android different rows in listview

查看:115
本文介绍了在列表视图的Andr​​oid不同行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从网络下载数据,并显示在列表视图,但我怎么能作出这样的第一排是不同的新闻应用程序,更大的高度,不同的布局和东西?

这是主要的活动源

 公共类家庭延伸活动{
    //所有静态变量
    静态最终字符串URL =htt​​p://dmb.site50.net/application.php?app_access_key=c4ca4238a0b923820dcc509a6f75849b;
    // XML节点键
    静态最后弦乐KEY_SONG =歌; //父节点
    静态最后弦乐KEY_ID =ID;
    静态最后弦乐KEY_TITLE =称号;
    静态最后弦乐KEY_ARTIST =艺术家;
    静态最后弦乐KEY_DURATION =时间;
    静态最后弦乐KEY_THUMB_URL =thumb_url;    公共静态最后弦乐TAG_NEWS =新闻;
    公共静态最后弦乐TAG_ID =ID;
    公共静态最后弦乐TAG_TITLE =称号;
    公共静态最后弦乐TAG_STORY =故事;
    公共静态最后弦乐TAG_SH_STORY =缩短;
    公共静态最后弦乐TAG_DATETIME =日期时间;
    公共静态最后弦乐TAG_AUTHOR =作者;
    公共静态最后弦乐TAG_IMG =img目录;    ListView控件列表;
    LazyAdapter适配器;
    按钮mBtnNaslovnica;
    私人ViewSwitcher viewSwitcher;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)。
        的setContentView(R.layout.main);        mBtnNaslovnica =(按钮)findViewById(R.id.mBtnNaslovnica);
        mBtnNaslovnica.setSelected(真);
        TextView的txtView =(的TextView)findViewById(R.id.scroller);
    txtView.setSelected(真);        的ImageButton mBtnRefresh =(的ImageButton)findViewById(R.id.btnRefresh);
        mBtnRefresh.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                新LoadingTask()执行(URL);
            }
        });
            ArrayList的<&HashMap的LT;字符串,字符串>> homeList =新的ArrayList<&HashMap的LT;字符串,字符串>>();                的JSONObject jsonobj;
                尝试{
                    jsonobj =新的JSONObject(getIntent()getStringExtra(JSON)。);
                    JSONArray新闻= jsonobj.getJSONArray(TAG_NEWS);
                    的for(int i = 0; I< news.length();我++){
                        JSONObject的C = news.getJSONObject(I)                        //存储在变量中的每个JSON项目
                        字符串ID = c.getString(TAG_ID);
                        字符串title = c.getString(TAG_TITLE);
                        字符串的故事= c.getString(TAG_STORY);
                        字符串缩短= c.getString(TAG_SH_STORY);
                        笔者字符串= c.getString(TAG_AUTHOR);
                        字符串日期时间= c.getString(TAG_DATETIME);
                        字符串IMG = c.getString(TAG_IMG);
                        //创建新的HashMap
                        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                        //将每个子节点的HashMap键=>值
                        map.put(TAG_ID,身份证);
                        map.put(TAG_TITLE,职称);
                        map.put(TAG_STORY,故事);
                        map.put(TAG_IMG,IMG);
                        map.put(TAG_DATETIME,日期时间);
                        map.put(TAG_AUTHOR,作家);                        //添加HashList到ArrayList的
                        homeList.add(地图);}
                }赶上(JSONException E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
            名单=(ListView控件)findViewById(R.id.list);            //通过传递XML数据的ArrayList获取适配器
            适配器=新LazyAdapter(这一点,homeList);
            list.setAdapter(适配器);
            // Click事件的单排列表
            list.setOnItemClickListener(新OnItemClickListener(){                @覆盖
                公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                    字符串cur_title =((的TextView)view.findViewById(R.id.title))的getText()的toString()。
                    字符串cur_story =((的TextView)view.findViewById(R.id.einfo2))的getText()的toString()。
                    字符串cur_author =((的TextView)view.findViewById(R.id.einfo1))的getText()的toString()。
                    字符串cur_datetime =((的TextView)view.findViewById(R.id.einfo))的getText()的toString()。
                    ImageView的cur_img =(ImageView的)view.findViewById(R.id.list_image);
                    字符串cur_img_url =(字符串)cur_img.getTag();                    意图I =新意图(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(ⅰ);
                }
            });
        }    公共无效loadPage(){    }    公共无效startNewActivity(){    }
    公共类LoadingTask扩展的AsyncTask<弦乐,对象,对象> {
        XMLParser的解析器=新XMLParser的();
        JSONParser jParser =新JSONParser();
        @覆盖
        保护对象doInBackground(字符串... PARAMS){
            // TODO自动生成方法存根
            字符串URL =参数[0];
            JSONObject的JSON = jParser.getJSONFromUrl(URL);
            //字符串XML = parser.getXmlFromUrl(URL); // URL从XML获得
             //获得DOM元素
            返回JSON;
        }        保护无效onPostExecute(对象结果){
            意向书的startApp =新意图(com.example.androidhive.HOME);
            startApp.putExtra(JSON,result.toString());
            startActivity(的startApp);
        }
    }    }


解决方案

您需要修改 LazyAdapter code为每一行返回正确的布局,这取决于任何你想要的标准。 ListView控件只是使用由适配器给定的视图,所以没有什么特别有layouted不同的每一行。请注意,以使其正常工作,您需要执行 getItemViewType() getViewTypeCount()以及

i have news application that download data from web and displays it in listview ,but how can i make that first row is different ,bigger height ,different layout and stuff ?

this is main activity source

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


    }



    }

解决方案

You need to modify your LazyAdapter code to return right layout for each row, depending on any criteria you want. ListView simply uses view given by adapter, so it is nothing special to have each row layouted differently. Please note that to make it work correctly you need to implement getItemViewType() and getViewTypeCount() as well.

这篇关于在列表视图的Andr​​oid不同行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆