我可以将Serializable文本添加到textview吗?

时间:2016-08-04 08:44:45

标签: java android serializable

我可以Serialize从我的数据库到文本视图的文本吗?我正在尝试这样做,但它仍然没有从我的数据库中获取userID,它只是打印Large Text,就像我在我的应用程序中拖动一样。可能吗?这是我的代码谢谢。

ownerhome.class

package com.example.kun.carkila;

 import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class ownerhome extends AppCompatActivity  {
final String TAG = this.getClass().getName();
int from_Where_I_Am_Coming = 0;

SharedPreferences pref;
SharedPreferences.Editor editor;
ListView lvPosts;
TextView userID1;
String userID;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ownerhome);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    User User = (User)getIntent().getSerializableExtra("user");

    userID1 = (TextView) findViewById(R.id.tvUserID);

    pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE);

    Log.d(TAG, pref.getString("username", ""));
    Log.d(TAG, pref.getString("password", ""));


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(ownerhome.this, InsertActivity.class);
            startActivity(in);
        }
    });
try {
if (User != null) {
    userID1.setText(User.userID);
}
}catch (Exception e){
System.out.println("Error in details " + e.toString());
}
}

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

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.action_logout){
        editor = pref.edit();
        editor.clear();
        editor.commit();
        Intent in = new Intent(ownerhome.this, MainActivity.class);
        startActivity(in);
        finish();
    }
    return super.onOptionsItemSelected(item);
}


}

User.class

package com.example.kun.carkila;

import com.google.gson.annotations.SerializedName;

public class User {
@SerializedName("userID")
public int userID;
}

谢谢大家的帮助:)

0 个答案:

没有答案