将EditText中的项添加到RecyclerView

时间:2016-06-11 09:22:45

标签: android

我正在为我的大学做一个项目。它有一个通知页面,任何人都可以发布通知。通知页面具有recyclerview,因此所有使用该应用程序的用户都可以查看发布的所有通知,就像文本永久保留在页面上一样。通知成功保存在数据库中,但我不想显示来自数据库的通知,它仅用于项目目的。但是,随着点击发布按钮的时间,应用程序强制关闭。代码有什么问题。请帮忙。以下是我的文件。

activity_notice.xml

<?php
    include("dbconn.php");
    $artid =$_GET['ID'];
    $temp=oci_parse($conn, "SELECT * FROM artwork where ARTW_ID = '$artid'");
    oci_execute ($temp);


$p = oci_fetch_assoc($temp);
    $ids = $_POST['ARTW_ID'];
    $title = $_POST['ARTW_TITLE'];
    $yy = $_POST['ARTW_YEAR'];


$price = $_POST['ARTW_PRICE'];
    $sql = oci_parse($conn, "INSERT INTO SALE values ('".$ids."', '".$title."', '".$yy."', '".$price."')");
    header("location: checkout.php");
    exit();

?>

noticeview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

<RelativeLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="RECENT NOTICES"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ff0612"
        android:textSize="30dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:id="@+id/noticeText"
        android:hint="Post Notice"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/sndnoticeBut"
        android:src="@drawable/send1"
        android:background="@null"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:onClick="addItem"/>
</RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="155dp"
        android:scrollbars="vertical"
        android:layout_marginTop="70dp"
        android:onClick="addItem" />

</RelativeLayout>

Notice.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="155dp"
        android:id="@+id/noticetext"
        android:layout_marginTop="70dp"/>
</LinearLayout>

Data.java

package com.salesiancollege.www.salesiani_smart;


import android.app.Activity;
import android.app.TaskStackBuilder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.graphics.Typeface;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.support.v7.app.NotificationCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

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

import static java.lang.System.currentTimeMillis;

public class Notice extends Activity
{
    ImageButton notbut;
    EditText nottxt;
    String Post;
    Context ctx=this;
    private RecyclerView mRecyclerView;
    private CustomRecyclerAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<Data> mData = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);

        notbut = (ImageButton) findViewById(R.id.sndnoticeBut);
        nottxt = (EditText) findViewById(R.id.noticeText);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
        // If the size of views will not change as the data changes.
        mRecyclerView.setHasFixedSize(true);
        // Setting the LayoutManager.
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

    }
            public void addItem(View v)
            {

               Post = nottxt.getText().toString();
                // Add data locally to the list.
                Data dataToAdd = new Data(nottxt.getText().toString());
                mData.add(dataToAdd);

                // Update adapter.
                mAdapter.addItem(mData.size()-1, dataToAdd);
               BackGround b = new BackGround();
               b.execute(Post);
            }


    class BackGround extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            String nottxt = params[0];
            String data="";
            int tmp;

            try {
                URL url = new URL("http://10.0.2.2/salesiancollege/notice.php");
                String urlParams = "nottxt="+nottxt;

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                os.write(urlParams.getBytes());
                os.flush();
                os.close();
                InputStream is = httpURLConnection.getInputStream();
                while((tmp=is.read())!=-1){
                    data+= (char)tmp;
                }
                is.close();
                httpURLConnection.disconnect();

                return data;

            } catch (MalformedURLException e) {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String s) {
            if(s.equals("")){
                s="Notice Posted.";
            }
            Toast.makeText(ctx, s, Toast.LENGTH_LONG).show();
        }
    }
}

CustomRecyclerAdapter.java

package com.salesiancollege.www.salesiani_smart;

import android.app.Activity;

/**
 * Created by PARTHO on 11-06-2016.
 */
public class Data
{
    public String text;
    public Data(String text)
    {
        this.text = text;

    }

}

RecyclerViewHolder.java

package com.salesiancollege.www.salesiani_smart;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Collections;
import java.util.List;


public class CustomRecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder>
{
    private List<Data> mData = Collections.emptyList();

    public CustomRecyclerAdapter()
    {
        // Pass context or other static stuff that will be needed.
    }
    public void updateList(List<Data> data)
    {
        mData = data;
        notifyDataSetChanged();
    }
    public void addItem(int position, Data data)
    {
        mData.add(position, data);
        notifyItemInserted(position);
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int position)
    {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(R.layout.noticeview, viewGroup, false);
        return new RecyclerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder viewHolder, int position)
    {
        viewHolder.notice.setText(mData.get(position).text);
    }

    @Override
    public int getItemCount()
    {
        return mData.size();
    }


}

logcat的:

package com.salesiancollege.www.salesiani_smart;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;


public class RecyclerViewHolder extends RecyclerView.ViewHolder
{
    public TextView notice;


    public RecyclerViewHolder(View itemView)
    {
        super(itemView);
        notice = (TextView) itemView.findViewById(R.id.noticetext);

    }
}

0 个答案:

没有答案