您好我正在製作一個字典應用程序,我希望當我從一個列表視圖中打開一個單詞,然後在單詞頁面(word.class)(這裏的單詞含義解釋) ,我想將這個單詞添加到listview形狀的favorite.class活動中,稍後我可以從列表視圖中檢索該單詞。通過sharedpreferences保存在列表視圖中的.class名稱
我想在一個java類中點擊最喜歡的按鈕,並在favorite.class的anathor活動中保存該單詞(字符串,它是該單詞的類名稱)。最喜歡的按鈕實際上是在操作欄上可見的菜單項。請解釋我所有的代碼和方式,我該如何做到這一點。請給一些代碼,以實現它。
public class Atherosclerosis extends Activity {
// declare variables for the table of content and paragraph heading here//
ScrollView scrollView;
TextView sign_atherosclerosis;
TextView sign_id;
TextView def_id;
TextView def_atherosclerosis;
TextView riskfac_id;
TextView riskfac_atherosclerosis;
TextView pathophy_id;
TextView pathophy_atherosclerosis;
TextView Dx_id;
TextView Dx_atherosclerosis;
TextView Rx_id;
TextView Rx_atherosclerosis;
TextView prevent_id;
TextView prevent_atherosclerosis;
TextView compl_id;
TextView compl_atherosclerosis;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atherosclerosis);
//here relate the variable for clickonlistener activity and direct scrolldown activity //
final ScrollView scrollView=(ScrollView)findViewById(R.id.scrollatherosclerosis);
TextView sign_id=(TextView)findViewById(R.id.Signandsymptomps_id);
final TextView sign_atherosclerosis=(TextView)findViewById(R.id.Signandsymptoms_atherosclerosis);
TextView def_id=(TextView)findViewById(R.id.definition_id);
final TextView def_atherosclerosis=(TextView)findViewById(R.id.definition_atherosclerosis);
TextView riskfac_id=(TextView)findViewById(R.id.riskfactor_id);
final TextView riskfac_atherosclerosis=(TextView)findViewById(R.id.riskfactor_atherosclerosis);
TextView pathophy_id=(TextView)findViewById(R.id.pathophysiology_id);
final TextView pathophy_atherosclerosis=(TextView)findViewById(R.id.pathophysiology_atherosclerosis);
TextView Dx_id=(TextView)findViewById(R.id.Diagnosis_id);
final TextView Dx_atherosclerosis=(TextView)findViewById(R.id.diagnosis_atherosclerosis);
TextView Rx_id=(TextView)findViewById(R.id.treatment_id);
final TextView Rx_atherosclerosis=(TextView)findViewById(R.id.treatment_atherosclerosis);
TextView prevent_id=(TextView)findViewById(R.id.prevention_id);
final TextView prevent_atherosclerosis=(TextView)findViewById(R.id.prevention_atherosclerosis);
TextView compl_id=(TextView)findViewById(R.id.complication_id);
final TextView compl_atherosclerosis=(TextView)findViewById(R.id.complication_atherosclerosis);
// this code is used for the action bar color change//
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// this is the code for jumping from the table of content to the paragraph//
sign_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (sign_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
def_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (def_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
riskfac_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (riskfac_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
pathophy_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (pathophy_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
Dx_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (Dx_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
Rx_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (Rx_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
prevent_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (prevent_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
compl_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
@Override
public void run() {
new CountDownTimer(300, 20) {
@Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (compl_atherosclerosis.getBottom()-millisUntilFinished));
}
@Override
public void onFinish() {
}
}.start();
}
}
);
}
});
}
// this is for the options selected from the menu button of mobile//
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.atherosclerosis, menu);
return true;
}
// for starting activity from the option or menu//
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.id_search:
Intent newActivity0 = new Intent(this,Search.class);
startActivity(newActivity0);
return true;
case R.id.id_favorit:
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("favorite", "com.kmcpesh.shortreviewofcardiology.Favorite");
editor.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
這是我favorite.class活動
public class Favorite extends Activity {
private TextView mEmptyText;
private LinearLayout mBookmarkLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
mEmptyText = (TextView) findViewById(R.id.empty_textview);
mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point);
getAllKeys();
}
private void getAllKeys()
{
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Map<String,?> keys = sp.getAll();
int count = 0;
for(Map.Entry<String,?> entry : keys.entrySet())
{
String value = entry.getValue().toString();
System.out.println("!!!!!!!!!!!!!!!!!value = "+value);
String delimiter = ",";
String[] values_array = value.split(delimiter);
addBookmark(values_array);
count++; //keep track of the number of bookmarks
}
//if there are no bookmarks, display a text view saying so. Otherwise, make the text view go away
if (count == 0)
{
mEmptyText.setVisibility(View.VISIBLE);
mEmptyText.setText(getString(R.string.no_bookmark));
}
else
mEmptyText.setVisibility(View.GONE);
}
@SuppressWarnings("deprecation")
private void addBookmark(String[] values_array)
{
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.favorite, null);
TextView text = (TextView) v.findViewById(R.id.bookmark_text);
text.setText(values_array[1]);
// insert into main view
mBookmarkLayout.addView(v, 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
System.out.println("!!!!!!!!!!!!!!!!!!!!Just added a view");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favorite, menu);
return true;
}
}
您需要對java進行自己的研究。試着弄清楚自己,然後在這裏問一個具體的問題。 – cbrulak
你使用數據庫?你可以發佈一些代碼嗎? –
不,我不使用數據庫...我希望它通過sharedpreferences存儲 – user2592807