所以我有一个带有图像按钮的回收视图,我想知道的是当我点击图像按钮时如何通知recyclerview更新。
背景:Imagebutton OnClick将添加一个点,TextView将更新点。类似于reddit点系统。
这是我的代码,任何帮助都会很棒。
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);
}
}
}
你可以看到我在onPostExecute方法中抛出mAdapter.notify,就像许多人所倡导的那样,但仍然没有运气。我必须通过再次生成视图以查看结果更改来物理刷新recyclelerview。
这是我的适配器。
private 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();
}
}
}
在我的适配器类中,在myHolder类中,有onClicks处理该进程。 mAdapter.notify应该位于onClick吗?我想到了,但我无法做到,因为没有任何mAdapter声明,它是在FragmentGlobalFeed类中声明的。
我该怎么办?谢谢你的时间。
答案 0 :(得分:0)
data.add(new Profile());
mAdapter.notifyItemInserted(data.size()-1);