点击listview打开android中的详细视图

时间:2015-02-16 09:09:18

标签: javascript android xml

我有一个XML文件,包含标题,图像链接和说明。

浓淡

<?xml ?>
<wrap>
 <content>
  <title>Title</title>
  <img>link of image</img>
  <desc>Descrioption</desc>
 </content>
 <content>
  <title>Title</title>
  <img>link of image</img>
  <desc>Descrioption</desc>
 </content>
 <content>
  <title>Title</title>
  <img>link of image</img>
  <desc>Description</desc>
 </content>
</wrap>

我希望Listview上显示的标题在点击时会打开一个包含标题,图片和说明的详细信息页面。

以下链接中的示例:Google的开发者:

http://developer.android.com/design/material/videos/ContactsAnim.mp4

这是我的完整代码

Anggra.java(这是我项目的主要活动)

package com.tolhedo.akew.anggra;

import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.viewpagerindicator.TitlePageIndicator;


public class Anggra extends ActionBarActivity {
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    TitlePageIndicator titlePageIndicator;

    private static final int numPages = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anggra);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        titlePageIndicator = (TitlePageIndicator)findViewById(R.id.titles);
        titlePageIndicator.setViewPager(mViewPager);

    }

    @Override
    public void onBackPressed() {
        if (mViewPager.getCurrentItem() == 0){
            super.onBackPressed();
        }
        else {
            mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_anggra, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = null;
            switch (position){
                case 0:
                    fragment = new Welcome();
                    getPageTitle(position);
                    break;
                case 1:
                    fragment = new ViewContent();
                    getPageTitle(position);
                    break;
                case 2:
                    fragment = new ItemFragment();
                    getPageTitle(position);
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return numPages;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_anggra, container, false);
            return view;
        }
    }

}

ItemFragment.java

package com.tolhedo.akew.anggra;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.tolhedo.akew.anggra.dummy.ResourcesAdapter;

public class ItemFragment extends Fragment{

    public ItemFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item, container, false);
        listView = (AbsListView) view.findViewById(R.id.list_item);
        textView = (TextView) view.findViewById(R.id.name);

        Resources resources = getActivity().getResources();
        ResourcesAdapter resourcesAdapter = new ResourcesAdapter(resources, view, getActivity());
        resourcesAdapter.stringResource();
        return view;
    }
}

ViewContent.java

package com.tolhedo.akew.anggra;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.tolhedo.akew.anggra.dummy.CustomView;
import com.tolhedo.akew.anggra.dummy.FullView;
import com.tolhedo.akew.anggra.dummy.ParsedDataSet;
import com.tolhedo.akew.anggra.dummy.XmlContentHandler;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class ViewContent extends Fragment{
    private TextView textView;
    private AbsListView listView;
    private ArrayList<ParsedDataSet> itemList;
    private ProgressDialog pDialog;
    private XmlContentHandler xmlContentHandler;
    private String url = "http://oscom.meximas.com/imagelistview.xml";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_view, container, false);
        textView = (TextView) view.findViewById(R.id.content);
        listView = (AbsListView) view.findViewById(R.id.listView1);

        doParsing(view);

        itemList = new ArrayList<>();

        setListViewClick(listView);

        return view;
    }

    public void doParsing(View view){
        if (isNetworkAvailable()){
            textView.setText("Loading...");
            new AsyncData().execute(url);
        }
        else
        {
            showToast("No Network Connection!!!");
        }
    }

    private void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE);
        if (connectivity == null)
        {
            return false;
        }
        else
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
            {
                for (int i = 0; i < info.length; i++)
                {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private class AsyncData extends AsyncTask<String, Void, Void> {
        @Override
        protected void onPreExecute()
        {
            pDialog = new ProgressDialog(getActivity());
            pDialog.setTitle("Loading....");
            pDialog.setMessage("Please wait...");
            pDialog.show();
            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(String... params) {
            try
            {
                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                SAXParser saxParser = saxParserFactory.newSAXParser();
                XMLReader xmlReader = saxParser.getXMLReader();

                xmlContentHandler = new XmlContentHandler();
                xmlReader.setContentHandler(xmlContentHandler);

                URL _url = new URL(params[0]);

                xmlReader.parse(new InputSource(_url.openStream()));

            }
            catch (ParserConfigurationException e) {
                e.printStackTrace();
            }
            catch (MalformedURLException e) {
                e.printStackTrace();
            }
            catch (SAXException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            textView.setText("Read all epic episode of Anggra");

            if (pDialog != null && pDialog.isShowing()){
                pDialog.dismiss();
            }

            itemList = xmlContentHandler.getListItem();
            if (null != itemList && itemList.size() != 0){
                for (int index = 0; index < itemList.size(); index++){
                    ParsedDataSet parsedDataSet = itemList.get(index);
                }
            }

            itemList = xmlContentHandler.getListItem();
            listView.setAdapter(new CustomView(getActivity(),itemList));
        }
    }

    private void setListViewClick (AbsListView listView){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                itemList = xmlContentHandler.getListItem();
                if (null != itemList && itemList.size() != 0){
                    for (int index = 0; index < itemList.size(); index++){
                        ParsedDataSet parsedDataSet = itemList.get(index);

                        textView.setText(parsedDataSet.getDesc());

                        Intent intent = new Intent(getActivity(), FullView.class);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    public void loadImg(String url, ImageView img){

        try{
            URL url1 = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
            connection.setDoInput(true);
            connection.connect();

            InputStream inputStream = connection.getInputStream();
            img.setImageBitmap(BitmapFactory.decodeStream(inputStream));
            return;

        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

CustomView.java

package com.tolhedo.akew.anggra.dummy;

import android.widget.BaseAdapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tolhedo.akew.anggra.R;



public class CustomView extends BaseAdapter {
    Context mContext;
    ArrayList<ParsedDataSet> dataSets;
    LayoutInflater mInflater;
    public CustomView(Context context, ArrayList<ParsedDataSet> itemList)
    {
        mContext = context;
        dataSets = itemList;
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return dataSets.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder vh;

        if(convertView == null){
            vh= new ViewHolder();
            convertView = mInflater.inflate(R.layout.custom_view, parent, false);
            vh.tv1 = (TextView) convertView.findViewById(R.id.textView1);
            vh.tv2 = (TextView) convertView.findViewById(R.id.textView2);
            vh.tv3 = (TextView) convertView.findViewById(R.id.textView3);
            vh.tv4 = (TextView) convertView.findViewById(R.id.textView4);
            vh.img = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(vh);
        }
        else {
            vh = (ViewHolder) convertView.getTag();
        }

        ParsedDataSet parsedDataSet = dataSets.get(position);


        vh.tv1.setText(parsedDataSet.getTitle());
        vh.tv2.setText(parsedDataSet.getDesc());
        vh.tv3.setText(parsedDataSet.getPubDate());
        vh.tv4.setText(parsedDataSet.getLink());

        return convertView;
    }

    static class ViewHolder
    {
        TextView tv1,tv2,tv3, tv4;
        ImageView img;
    }


}

ParseDataSet.java

package com.tolhedo.akew.anggra.dummy;

/**
 * Created by akew on 1/14/2015.
 */
public class ParsedDataSet {
    private String id;
    private String title;
    private String desc;
    private String pubDate;
    private String link;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }
}

ResourcesAdapter.java

package com.tolhedo.akew.anggra.dummy;

import android.app.Activity;
import android.content.res.Resources;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.tolhedo.akew.anggra.R;

/**
 * Created by akew on 1/8/2015.
 */
public class ResourcesAdapter {
    Resources mResources;
    View mView;
    Activity mActivity;
    String[] strings;
    AbsListView mListView;
    ListAdapter mAdapter;
    TextView mTextView;


    public ResourcesAdapter(Resources resources, View view, Activity activity){
        mActivity = activity;
        mResources = resources;
        mView = view;
        mListView = (AbsListView) view.findViewById(android.R.id.list);
    }

    public void stringResource(){
        strings = mResources.getStringArray(R.array.list_title);
        mAdapter = new ArrayAdapter<>(mActivity
        , android.R.layout.simple_list_item_1
        , android.R.id.text1
        , strings);
        mListView.setAdapter(mAdapter);
        setListViewClick(mListView);
    }

    private void setListViewClick(AbsListView mListView){
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                mTextView = (TextView) view;
                Toast.makeText(mActivity
                , mTextView.getText()
                , Toast.LENGTH_SHORT).show();
            }
        });
    }
}

XmlContentHandler.java

package com.tolhedo.akew.anggra.dummy;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;



/**
 * Created by akew on 1/14/2015.
 */
public class XmlContentHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = "";
    ParsedDataSet parsedDataSet = null;

    private ArrayList<ParsedDataSet> itemList = new ArrayList<>();

    public ArrayList<ParsedDataSet> getListItem() {
        return itemList;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        currentElement = true;
        currentValue = "";
        if (localName.equals("item")){
            parsedDataSet = new ParsedDataSet();
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        currentElement = false;

        if (localName.equals("id")){
            parsedDataSet.setId(currentValue);
        }

        else if (localName.equals("title")){
            parsedDataSet.setTitle(currentValue);
        }
        else if (localName.equals("desc")){
            parsedDataSet.setDesc(currentValue);
        }
        else if (localName.equals("pubDate")){
            parsedDataSet.setPubDate(currentValue);
        }
        else if (localName.equals("link")){
            parsedDataSet.setLink(currentValue);
        }
        else if (localName.equals("item")){
            itemList.add(parsedDataSet);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentElement){
            currentValue = currentValue + new String(ch, start, length);
        }
    }
}

谢谢你的帮助

0 个答案:

没有答案