Android 单击recyclerview内的按钮通知数据集已更改isn';t更新我的视图
所以我有一个里面有imagebutton的recyclerview,我想弄清楚的是,当我点击imagebutton时,我如何通知recyclerview进行更新 背景:单击Imagebutton将添加一个点,点文本视图将更新。类似reddit point系统的东西 这是我的密码,任何帮助都会很棒Android 单击recyclerview内的按钮通知数据集已更改isn';t更新我的视图,android,android-recyclerview,notifydatasetchanged,Android,Android Recyclerview,Notifydatasetchanged,所以我有一个里面有imagebutton的recyclerview,我想弄清楚的是,当我点击imagebutton时,我如何通知recyclerview进行更新 背景:单击Imagebutton将添加一个点,点文本视图将更新。类似reddit point系统的东西 这是我的密码,任何帮助都会很棒 public static final int CONNECTION_TIMEOUT = 10000; public static final int READ_TIMEOUT = 15000; pri
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private android.support.v7.widget.RecyclerView mRVProfile;
private com.example.admin.quoteme.AdapterProfile mAdapter;
private android.widget.LinearLayout llLayout;
@Override
public void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public android.view.View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, android.os.Bundle savedInstanceState) {
// Inflate the layout for this fragment
android.view.View view = inflater.inflate(com.example.admin.quoteme.R.layout.fragmentglobalfeed, null);
java.util.List<Profile> data = new java.util.ArrayList<>();
//recyclerview
new com.example.admin.quoteme.FragmentGlobalfeed.AsyncFeed().execute("feed");
return view;
}
public class AsyncFeed extends android.os.AsyncTask<String, String, String> {
java.net.HttpURLConnection conn;
java.net.URL url = null;
String quote_url = "http://192.168.0.100/Quoteme/quotepointV2.php";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String method = params[0];
if (method.equals("feed")) {
try {
//enter url address where ur json file is
url = new java.net.URL("http://192.168.0.100/Quoteme/updated_getfeed.php");
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
return e.toString();
}
try {
conn = (java.net.HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
} catch (java.io.IOException e1) {
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
if (response_code == java.net.HttpURLConnection.HTTP_OK) {
//read data
java.io.InputStream input = conn.getInputStream();
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (java.io.IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
} else if (method.equals("quotepoint")) {
try {
String quoteid, userid;
quoteid = params[1];
userid = params[2];
java.net.URL url = new java.net.URL(quote_url);
java.net.HttpURLConnection httpURLConnection = (java.net.HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
java.io.OutputStream outputStream = httpURLConnection.getOutputStream();
java.io.BufferedWriter bufferedWriter = new java.io.BufferedWriter(new java.io.OutputStreamWriter((outputStream), "UTF-8"));
String data = java.net.URLEncoder.encode("quoteid", "UTF-8") + "=" + java.net.URLEncoder.encode(quoteid, "UTF-8")
+ "&" + java.net.URLEncoder.encode("userid", "UTF-8") + "=" + java.net.URLEncoder.encode(userid, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
java.io.InputStream inputStream = httpURLConnection.getInputStream();
java.io.BufferedReader bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append((line + "\n"));
}
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
} catch (java.net.ProtocolException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
return method;
}
@Override
protected void onPostExecute(String result) {
java.util.List<Profile> data = new java.util.ArrayList<>();
try {
org.json.JSONArray jArray = new org.json.JSONArray(result);
//extraction
for (int i = 0; i < jArray.length(); i++) {
org.json.JSONObject json_data = jArray.getJSONObject(i);
Profile profile = new Profile();
profile.setUser_id(json_data.getString("username"));
profile.setQuote_points(json_data.getInt("quote_points"));
profile.setQuote(json_data.getString("quote_description"));
data.add(profile);
}
//setup and hand data over to rv // adapter
mRVProfile = (android.support.v7.widget.RecyclerView) getView().findViewById(com.example.admin.quoteme.R.id.recyclerViewGlobal);
mAdapter = new AdapterProfile(getActivity(), data);
mRVProfile.setAdapter(mAdapter);
mRVProfile.setLayoutManager(new android.support.v7.widget.LinearLayoutManager(getActivity()));
mAdapter.notifyDataSetChanged();
} catch (org.json.JSONException e) {
//android.widget.Toast.makeText(getActivity(),e.toString(), android.widget.Toast.LENGTH_LONG);
}
}
}
公共静态最终int连接\u超时=10000;
公共静态最终整型读取超时=15000;
私有android.support.v7.widget.RecyclerView mRVProfile;
private com.example.admin.quoteme.AdapterProfile mAdapter;
私有android.widget.LinearLayout llLayout;
@凌驾
创建时的公共void(android.os.Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
public android.view.view onCreateView(android.view.LayoutInflater充气机、android.view.ViewGroup容器、android.os.Bundle savedInstanceState){
//为该碎片膨胀布局
android.view.view=inflater.inflate(com.example.admin.quoteme.R.layout.fragmentglobalfeed,null);
java.util.List data=new java.util.ArrayList();
//回收视图
新建com.example.admin.quoteme.FragmentGlobalfeed.AsyncFeed().execute(“feed”);
返回视图;
}
公共类AsyncFeed扩展了android.os.AsyncTask{
java.net.HttpURLConnection;
java.net.URL=null;
字符串quote_url=”http://192.168.0.100/Quoteme/quotepointV2.php";
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串方法=参数[0];
if(方法等于(“提要”)){
试一试{
//输入ur json文件所在的url地址
url=新的java.net.url(“http://192.168.0.100/Quoteme/updated_getfeed.php");
}catch(java.net.MalformedURLException){
e、 printStackTrace();
返回e.toString();
}
试一试{
conn=(java.net.HttpURLConnection)url.openConnection();
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“GET”);
连接设置输出(真);
}捕获(java.io.IOException e1){
e1.printStackTrace();
返回e1.toString();
}
试一试{
int response_code=conn.getResponseCode();
if(response_code==java.net.HttpURLConnection.HTTP_OK){
//读取数据
java.io.InputStream输入=conn.getInputStream();
java.io.BufferedReader reader=new java.io.BufferedReader(new java.io.InputStreamReader(input));
StringBuilder结果=新建StringBuilder();
弦线;
而((line=reader.readLine())!=null){
结果。追加(行);
}
返回(result.toString());
}否则{
返回(“未成功”);
}
}捕获(java.io.ioe异常){
e、 printStackTrace();
返回e.toString();
}最后{
连接断开();
}
}else if(方法等于(“quotepoint”)){
试一试{
字符串quoteid,userid;
quoteid=参数[1];
userid=params[2];
java.net.URL=新的java.net.URL(quote_URL);
java.net.HttpURLConnection HttpURLConnection=(java.net.HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
java.io.OutputStream OutputStream=httpURLConnection.getOutputStream();
java.io.BufferedWriter BufferedWriter=new java.io.BufferedWriter(new java.io.OutputStreamWriter((outputStream),“UTF-8”);
字符串数据=java.net.URLEncoder.encode(“quoteid”,“UTF-8”)+“=”+java.net.URLEncoder.encode(quoteid,“UTF-8”)
+“&”+java.net.URLEncoder.encode(“userid”,“UTF-8”)+“=”+java.net.URLEncoder.encode(userid,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
java.io.InputStream InputStream=httpURLConnection.getInputStream();
java.io.BufferedReader BufferedReader=new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
StringBuilder StringBuilder=新的StringBuilder();
字符串行=”;
而((line=bufferedReader.readLine())!=null){
stringBuilder.append((行+“\n”));
}
httpURLConnection.disconnect();
返回stringBuilder.toString().trim();
}catch(java.net.MalformedURLException){
e、 printStackTrace();
}捕获(java.io.UnsupportedEncodingException){
e、 printStackTrace();
}catch(java.net.ProtocolException e){
e、 printStackTrace();
}捕获(java.io.ioe异常){
e、 printStackTrace();
}
}
返回法;
}
@凌驾
受保护的void onPostExecute(字符串结果){
java.util.List data=new java.util.ArrayList();
试一试{
org.json.JSONArray jArray=new org.json.JSONArray(结果);
//提取
for(int i=0;iprivate android.content.Context context;
private android.view.LayoutInflater inflater;
java.util.List<Profile> data = java.util.Collections.emptyList();
public AdapterProfile(android.content.Context context, java.util.List<com.example.admin.quoteme.Profile> data){
this.context=context;
inflater= android.view.LayoutInflater.from(context);
this.data=data;
}
@Override
public android.support.v7.widget.RecyclerView.ViewHolder onCreateViewHolder(android.view.ViewGroup parent, int viewType){
android.view.View view = inflater.inflate(com.example.admin.quoteme.R.layout.feed_layout_v2, parent, false);
MyHolder holder = new com.example.admin.quoteme.AdapterProfile.MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(android.support.v7.widget.RecyclerView.ViewHolder holder, int position) {
// Get current position of item in recyclerview to bind data and assign values from list
MyHolder myHolder= (MyHolder) holder;
com.example.admin.quoteme.Profile current=data.get(position);
myHolder.name.setText(current.getUser_id());
myHolder.quote.setText(current.getQuote());
myHolder.points.setText(current.getQuote_points() + " Points");
// load image into imageview using glide
/*Glide.with(context).load("http://192.168.1.7/test/images/" + current.fishImage)
.placeholder(R.drawable.ic_img_error)
.error(R.drawable.ic_img_error)
.into(myHolder.ivFish);*/
}
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends android.support.v7.widget.RecyclerView.ViewHolder implements android.view.View.OnClickListener{
private android.widget.TextView name,quote,points,comment;
private android.widget.ImageButton btnPoints;
// create constructor to get widget reference
public MyHolder(android.view.View itemView) {
super(itemView);
name = (android.widget.TextView)itemView.findViewById(com.example.admin.quoteme.R.id.name);
quote = (android.widget.TextView)itemView.findViewById(com.example.admin.quoteme.R.id.quote);
points = (android.widget.TextView)itemView.findViewById(com.example.admin.quoteme.R.id.points);
comment = (android.widget.TextView)itemView.findViewById(com.example.admin.quoteme.R.id.tvComment);
btnPoints = (android.widget.ImageButton)itemView.findViewById(com.example.admin.quoteme.R.id.imageButton1);
itemView.setOnClickListener(this);
comment.setOnClickListener(this);
btnPoints.setOnClickListener(this);
}
@Override
public void onClick(android.view.View v) {
android.content.SharedPreferences sharedPreferences = context.getSharedPreferences(context.getString(com.example.admin.quoteme.R.string.PREF_FILE), android.content.Context.MODE_PRIVATE);
final String userid = sharedPreferences.getString(context.getString(com.example.admin.quoteme.R.string.USER_ID), "");
if (v.getId() == btnPoints.getId()){
//String quoteid = String.valueOf(getAdapterPosition());
android.widget.Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), android.widget.Toast.LENGTH_SHORT).show();
//BackgroundTask backgroundTask = new BackgroundTask(context);
//backgroundTask.execute("quotepoint", String.valueOf(getAdapterPosition() + 1 ) + "", userid);
FragmentGlobalfeed fragmentGlobalfeed = new FragmentGlobalfeed();
fragmentGlobalfeed.new AsyncFeed().execute("quotepoint", String.valueOf(getAdapterPosition() + 1 ) + "", userid);
}
else {
android.widget.Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), android.widget.Toast.LENGTH_SHORT).show();
}
}
}
data.add(new Profile());
mAdapter.notifyItemInserted(data.size()-1);