如何使用编程

时间:2017-03-22 13:48:23

标签: android xml-parsing android-recyclerview android-adapter

我希望看到或隐藏XML文件项...我正在使用XML解析数据并在图像视图,视频视图和文本视图中放置值...我想在我的应用程序中显示的内容是,如果有只是"文字"在xml项目中可用,然后隐藏其他itemViews,如果text + image可用,则显示带有文本的图像并隐藏其余部分..

This is my XML FILE DATA
 <items>
    <item id="1">
   <MessageID>1</MessageID>
   <PublishDateTime>xxxxxxxx</PublishDateTime>
   <MessageText>xxxxxxxxx</MessageText>
   <MediaURL></MediaURL>
   <MediaMIMEType></MediaMIMEType>
 <MediaSize>0</MediaSize>
 <Likes>10</Likes>
 <Shares>5</Shares>
 <PublishedBy>xxxxxxxxxxxx</PublishedBy>
</item>
<item id="2">
<MessageID>2</MessageID>
<PublishDateTime>xxxxxxx</PublishDateTime>
<MessageText>xxxxxxxx xxx  x<br>xxxxxx.</br>xxxxxxx</MessageText>
<MediaURL>https://www.xxxx.org/wp-content/xxx/xxx/xx/xxxxx.jpg</MediaURL>
<MediaMIMEType>image/jpeg</MediaMIMEType>
<MediaSize>111649</MediaSize>
<Likes>100</Likes>
<Shares>95</Shares>
<PublishedBy>xxxx xxxxx xxxxx</PublishedBy>
</item>
<item id="3">
 <MessageID>3</MessageID>
 <PublishDateTime>xxxxxxxx</PublishDateTime>
 <MessageText>xxxxxxx xxxxxxx xx https://www.xxxxxxx.org/</MessageText>
 <MediaURL>https://xxxxxxx xxxxx.org/wp-content/xxxx/xxxx/xx/xxxxx.jpg</MediaURL>
 <MediaMIMEType>image/jpeg</MediaMIMEType>
 <MediaSize>76602</MediaSize>
 <Likes>64</Likes>
 <Shares>42</Shares>
 <PublishedByxxxxxxxxxxxx</PublishedBy>
 </item>
 <item id="4">
 <MessageID>4</MessageID>
 <PublishDateTime>1482227547</PublishDateTime>
 <MessageText></MessageText>
 <MediaURL>https://www.youtube.com/watch?v=xxxxxxxxx</MediaURL>
 <MediaMIMEType>video/mpeg</MediaMIMEType>
 <MediaSize></MediaSize>
 <Likes>6100</Likes>
 <Shares>550</Shares>
 <PublishedBy>xxxxxxxxxxxxx</PublishedBy>
 </item>
 <item id="5">
 <MessageID>5</MessageID>
 <PublishDateTime>xxxxxxxx</PublishDateTime>
 <MessageText>xxxxxxxxxxxxxxxxxxxxx xx x x xxxxx xxxx</MessageText>
 <MediaURL></MediaURL>
 <MediaMIMEType></MediaMIMEType>
 <MediaSize></MediaSize>
 <Likes></Likes>
 <Shares></Shares>
 <PublishedBy>xxxxxxxx</PublishedBy>
 </item>
 <item id="6">
 <MessageID>6</MessageID>
 <PublishDateTime>xxxxxxxx</PublishDateTime>
 <MessageText></MessageText>
 <MediaURL>https://www.youtube.com/watch?v=xxxxxx xx</MediaURL>
 <MediaMIMEType>video/mpeg</MediaMIMEType>
 <MediaSize></MediaSize>
 <Likes>10110</Likes>
 <Shares>8011</Shares>
 <PublishedBy>xxxxx xxxxxx</PublishedBy>
  </item>
  <item id="7">
  <MessageID>7</MessageID>
  <PublishDateTime>xxxxxx</PublishDateTime>
  <MessageText>xxx xxx xxx</MessageText>
  <MediaURL>https://xxxxxxxx.org/wp-content/uploads/media/xxxxxx.mp3</MediaURL>
  <MediaMIMEType>audio/mp3</MediaMIMEType>
  <MediaSize></MediaSize>
  <Likes></Likes>
  <Shares></Shares>
  <PublishedBy>xxxxxxxxxx</PublishedBy>
  </item>
  <item id="8">
  <MessageID>8</MessageID>
  <PublishDateTime>xxxx</PublishDateTime>
  <MessageText>xxxxxxxxxxxx x xxxxx xxxxx.<br>xxxxx.</br></MessageText>
  <MediaURL></MediaURL>
  <MediaMIMEType></MediaMIMEType>
  <MediaSize></MediaSize>
  <Likes></Likes>
  <Shares></Shares>
  <PublishedBy>xxxxxxxxxx &lt;tel:+xxxxxxxxxxx&gt;</PublishedBy>
  </item>

ReadRss.java

  public class ReadRss extends AsyncTask<Void, Void, Void> {
     Context context;

String address="https://xxxxx xxxxx.org/xxxxxxx/feed/";

ProgressDialog progressDialog;
ArrayList<FeedItem> feedItems;
RecyclerView recyclerView;
VideoView videoView;
View view;
MyAdapter adapter;
MyAdapter.MyViewHolder myViewHolder;

   URL url;
   Uri uri;


public ReadRss(Context context, RecyclerView recyclerView) {
    this.recyclerView=recyclerView;
    this.context = context;
    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage("Loading...");

}

@Override
protected void onPreExecute() {
    progressDialog.show();
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    adapter=new MyAdapter(context,feedItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.addItemDecoration(new VerticalSpace(50));
    recyclerView.setAdapter(adapter);
  //  videoView.setVideoURI(uri);
}

@Override
protected Void doInBackground(Void... params) {
    ProcessXml(Getdata());

    return null;
}

private void ProcessXml(Document data) {
    if (data != null) {
        feedItems=new ArrayList<>();
        Element root = data.getDocumentElement();
        //Node channel = root.getChildNodes().item(1);
        NodeList items = root.getChildNodes();
        for (int i = 0; i < items.getLength(); i++) {
            Node cureentchild = items.item(i);
            if (cureentchild.getNodeName().equalsIgnoreCase("item")) {

                FeedItem item=new FeedItem();
                NodeList itemchilds = cureentchild.getChildNodes();

                for (int j = 0; j < itemchilds.getLength(); j++) {

                    Node cureent = itemchilds.item(j);
                    if (cureent.getNodeName().equalsIgnoreCase("title")) {
                        item.setTitle(cureent.getTextContent());
                    } else if (cureent.getNodeName().equalsIgnoreCase("MessageText")) {
                        item.setDescription(cureent.getTextContent());
                    } else if (cureent.getNodeName().equalsIgnoreCase("PublishDateTime")) {
                        item.setPubDate(cureent.getTextContent());
                    } else if (cureent.getNodeName().equalsIgnoreCase("link")) {
                        item.setLink(cureent.getTextContent());
                    } else if (cureent.getNodeName().equalsIgnoreCase("MediaURL")) {
                        item.setMediaURL(cureent.getTextContent());
                    } else if (cureent.getNodeName().equalsIgnoreCase("MediaMIMEType")) {
                        item.setMediaType(cureent.getTextContent());
                    } else {
                        //Nothing here, ignore other occurances of XML childern
                    }
                }

                Log.e("MediaType: ",item.getMediaType());
                Log.e("Media URL",item.getMediaURL());
                Log.e("Message text",item.getDescription());


                switch (item.getMediaType())
                   {

                       case "image/jpeg":

                           if(item.getDescription().isEmpty()){

                               Log.e("Message Text Empty","Only Media is Present");
                                    myViewHolder.Thumbnail.setVisibility(View.VISIBLE);
                               myViewHolder.Description.setVisibility(View.INVISIBLE);
                                   myViewHolder.Thumbnail.getParent()).setVisibility(View.VISIBLE);

                           }
                           if(item.getMediaURL().isEmpty()){
                               myViewHolder.Description.setVisibility(View.VISIBLE);
                               myViewHolder.Thumbnail.setVisibility(View.INVISIBLE);
                           }

                           break;
                       case "video/mpeg":

                           break;
                       default:

                   }
                feedItems.add(item);
            }

        }
    }
}



public Document Getdata() {
    try {
        url = new URL(address);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        InputStream inputStream = connection.getInputStream();
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        return xmlDoc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
       }
    }
}

custum_row_news_item.xml

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/cv"
app:cardBackgroundColor="#FFFFFF"
app:cardElevation="4dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/date_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/description_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/thumb_img"
        android:autoLink="web"
        android:linksClickable="true"
        android:visibility="visible"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/thumb_img"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/test"
        android:visibility="visible"
        android:layout_below="@+id/date_text"
        android:layout_alignParentStart="true"
        android:layout_marginTop="17dp" />

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/videoView"
        android:visibility="invisible"
        android:layout_above="@+id/title_text"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#90f79a20"
        android:gravity="center"
        android:lines="1"
        android:text="Large Text"
        android:visibility="visible"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_above="@+id/description_text"
        android:layout_alignParentStart="true" />

      </RelativeLayout>
    </android.support.v7.widget.CardView>

MyAdapter.java

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
ArrayList<FeedItem> feedItems;
Context context;
ReadRss readRss;
int USER=0,IMAGE=1;
public MyAdapter(Context context, ArrayList<FeedItem> feedItems){
    this.feedItems=feedItems;
    this.context=context;
}
@Override
public int getItemCount()
{
    return feedItems.size();
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
   /* if(feedItems.get(position) instanceof String ){
        ((TextView) feedItems.get(position));


    }*/
}

    public void bindDataToAdapter(){

}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflator=LayoutInflater.from(parent.getContext());
    View view= layoutInflator.inflate(R.layout.custum_row_news_item,parent,false);
    View messageTextOnly=layoutInflator.inflate(R.layout.messageonly,parent,false);
    MyViewHolder holder=new MyViewHolder(view);
    //MyViewHolder holder1=new MyViewHolder(messageTextOnly);
    return holder;
}


@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    YoYo.with(Techniques.FadeInLeft).playOn(holder.cardView);
    final FeedItem current=feedItems.get(position);
    holder.Title.setText(current.getTitle());
    holder.Description.setText(current.getDescription());
    holder.Date.setText(current.getPubDate());
   if(current.getMediaURL().isEmpty()){
    holder.Thumbnail.setImageResource(R.drawable.butterfly);
   }else {
        Picasso.with(context).load(current.getMediaURL()).into(holder.Thumbnail);
    }
    //Picasso.with(context).load(current.getVideoUrl()).into((Target) holder.videoView);
   /* holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent( context,NewsDetail.class);
            intent.putExtra("Link",current.getLink());
            context.startActivity(intent);
        }
    });*/

}


public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView Title,Description,Date;
    ImageView Thumbnail;
    CardView cardView;
    VideoView videoView;

    public MyViewHolder(View itemView) {
        super(itemView);
        Title= (TextView) itemView.findViewById(R.id.title_text);
        Description= (TextView) itemView.findViewById(R.id.description_text);
        Date= (TextView) itemView.findViewById(R.id.date_text);
        Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
        cardView= (CardView) itemView.findViewById(R.id.cv);
       // videoView=(VideoView)itemView.findViewById(R.id.videoView);
        }
     }
 }

MainActivity.java

  public class MainActivity extends AppCompatActivity {

private Toolbar toolBar;
private Menu menu;

ProgressBar mnuProgressBar;
RecyclerView recyclerView;
VideoView videoView;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolBar=(Toolbar)findViewById(R.id.tool_bar);
    setSupportActionBar(toolBar);
    toolBar.setTitle(R.string.app_name);
    mnuProgressBar= (ProgressBar) findViewById(R.id.progressBar);
    recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
   // videoView=(VideoView)findViewById(R.id.videoView);
    ReadRss readRss=new ReadRss(this,recyclerView);
    readRss.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu=menu;
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return super.onCreateOptionsMenu(menu);
    }
    }

FeedItem.java

public class FeedItem {
String title;
String link;
String description;
String pubDate;
String mediaUrl;
String mediaType;
String vtViewType;

public String getMediaURL() { return mediaUrl;     }

public void setMediaURL(String mediaUrl) { this.mediaUrl = mediaUrl; }

public String getTitle() {
    return title;
}

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

public String getLink() {
    return link;
}

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

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getPubDate() {
    return pubDate;
}

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

public String getMediaType() {
    return mediaType;
}

public void setMediaType(String mediaType) {
    this.mediaType = mediaType;
}

public String getViewType() {
    return vtViewType;
}

public void setViewType(String viewType) {
    this.vtViewType = viewType;
    }
 }

我在xml文件中的所有链接都工作正常..请检查代码并给我解决方法如何在我的活动中显示项目和显示项目... thanx in advace

1 个答案:

答案 0 :(得分:0)

您只需要有条件地设置视图的可见性。例如,如果某个文本视图的值不为null,则将其可见性设置为可见,否则为/不可见。 setvisiblity是您需要的属性。