我在Android App中更新TextView时出现问题。我做的是,当按下“+”按钮时,RecycleView打开,然后我点击Kinvey从那里重新检索的项目。然后我将项目的名称传递给了主要活动。从主要活动我可以在运行应用程序时记录该数据的时间(第一次值为null,因为我没有点击RecycleView中的项目),但是我无法使用该值设置TextView(TextView保持为空)?第三次运行应用程序时,重试值再次为空。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Testiraj";
private RecyclerView rv;
public Context context;
private TextView TextViewGymName;
private TextView TextViewVisitorsNumber;
private List<AllGyms> mVisitors;
public static final String MY_PREFS_NAME = "MyPrefsFile";
public String mRVPickedItem;
public String restoredText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextViewGymName = (TextView) findViewById(R.id.gymName);
TextViewVisitorsNumber = (TextView) findViewById(R.id.gymVisitors);
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
restoredText = prefs.getString("name", "nothing");
if(restoredText!=null){
Log.v(TAG,restoredText);
TextViewGymName.setText(restoredText);
}
else{
Log.v(TAG, "null" +" "+ restoredText);
}
}
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Intent intenti=getIntent();
Bundle b = intenti.getExtras();
if(b!=null){
mRVPickedItem = (String) b.get("NAME_OF_THE_STRING");
final Client mKinveyClient = new Client.Builder("KEY", "KEY_APP"
, this.getApplicationContext()).build();
final Query query = new Query();
query.equals("name", mRVPickedItem);
query.equals("visitors");
AsyncAppData<AllGyms> searchedEvents = mKinveyClient.appData("allGyms", AllGyms.class);
searchedEvents.get(query, new KinveyListCallback<AllGyms>() {
@Override
public void onSuccess(AllGyms[] event) {
ArrayList<AllGyms> arrayList = new ArrayList<AllGyms>(Arrays.asList(event));
String[] array = new String[arrayList.size()];
mVisitors = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
array[i] = arrayList.get(i).getVisitors();
//allGymses.add(new AllGyms(test[i].getName()));
//Log.v(TAG, "received " + array[i]);
TextViewVisitorsNumber.setText(array[i]);
}
}
@Override
public void onFailure(Throwable error) {
Log.e(TAG, "failed to query ", error);
}
});
}
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", mRVPickedItem);
editor.apply();
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
restoredText = prefs.getString("name", mRVPickedItem);
TextViewGymName.setText(restoredText);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, `RecyclerViewActivity.class);`
startActivity(intent);
}
});
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass method first
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
RecycleView活动:
package com.fittoloc.uros.gymloced;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.kinvey.android.AsyncAppData;
import com.kinvey.android.Client;
import com.kinvey.android.callback.KinveyListCallback;
import com.kinvey.android.callback.KinveyUserCallback;
import com.kinvey.java.Query;
import com.kinvey.java.User;
import com.kinvey.java.core.KinveyClientCallback;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RecyclerViewActivity extends AppCompatActivity {
private List<AllGyms> allGymses;
private RecyclerView rv;
public Context context;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
rv=(RecyclerView)findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
initializeRecycleView();
rv.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
}
})
);
}
private void initializeRecycleView() {
//ovde resiti problem!!!!
final Client mKinveyClient = new Client.Builder("KEY", "KEY"
, this.getApplicationContext()).build();
mKinveyClient.user().login(new KinveyUserCallback() {
@Override
public void onFailure(Throwable t) {
CharSequence text = "Login error.";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(User u) {
CharSequence text = "Welcome back!";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
final AllGyms teretane = new AllGyms();
Query myQuery = mKinveyClient.query();
myQuery.equals ("name");
AsyncAppData<AllGyms> myGyms = mKinveyClient.appData("allGyms", AllGyms.class);
myGyms.get(myQuery, new KinveyListCallback<AllGyms>() {
@Override
public void onSuccess(AllGyms[] results) {
ArrayList <AllGyms> arrayList = new ArrayList<AllGyms>(Arrays.asList(results));
String [] array = new String[arrayList.size()];
allGymses = new ArrayList<>();
for(int i = 0 ; i < arrayList.size() ; i++){
array[i] = arrayList.get(i).getName();
//allGymses.add(new AllGyms(test[i].getName()));
allGymses.add((new AllGyms(array[i])));
//Log.v(TAG, "received " + array[i]);
initializeAdapter();
}
//Log.v(TAG, "received " + results + " allGyms");
}
@Override
public void onFailure(Throwable error) {
Log.e(TAG, "failed to fetchByFilterCriteria", error);
}
});
//
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(allGymses,context);
rv.setAdapter(adapter);
}
}
RecyleItemClickListener:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
private static final String TAG = "MyActivity";
private String position;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}