بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
ساخت لیست موارد علاقه مندی (Favourites)
سلام
دوستان اگه کسی آموزش یا سورس کد اضافه کردن موارد به لیست علاقه مندی داره ممنون میشم لینک بذاره
توضیح:
یک اکتیویتوی داریم که داخلش چنتا تکست ویو هست
حالا می خوایم با زدن یک دکمه متن داخل یکی از این تکست ویو ها رو بگیره و درون یک آرایه ذخیره کنه
و دفعه بعد که برنامه رو باز می کنیم این متن رو چک کنه که در آرایه وجود داره یا خیر
اگر داشت دکمه علاقه مندی روشن باشه و اگه نبود خاموش باشه
برای این سوال 2 پاسخ وجود دارد.
پاسخ به سوال
h_r_v
12 سال پیش
+1
0
من از دیتابیس استفاده میکنم! توی صفحه ای که مطالب خونده میشن یه دکمه گذاشتم که اینکار رو میکنه :
fav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String name2 = null;
MyDatabase my;
long id = i.getLongExtra("id", 0);
SQLiteDatabase sq;
my = new MyDatabase(getApplicationContext());
sq = my.getWritableDatabase();
Cursor cursor = sq.rawQuery("SELECT * FROM item WHERE item_id="+id, null);
while(cursor.moveToNext()){
name2 = cursor.getString(cursor.getColumnIndex("item_fav"));
}
Log.i("name2", name2);
if(name2.equals("0")){
ContentValues cv = new ContentValues();
cv.put("item_fav", "1");
String strLong = Long.toString(id);
String where = "item_id=?";
String[] value = { strLong };
sq.update("item", cv, where, value);
fav.setBackgroundResource(R.drawable.favorite1);
Cursor cursor1 = sq.rawQuery("SELECT * FROM item WHERE item_id="+id, null);
Toast.makeText(getApplicationContext(), "بستنی به لیست مورد علاقه ها اضافه شد", Toast.LENGTH_LONG).show();
while(cursor1.moveToNext()){
String name1 = cursor1.getString(cursor1.getColumnIndex("item_fav"));
Log.i("LOG", name1);
}}
if(name2.equals("1")){
ContentValues cv1 = new ContentValues();
cv1.put("item_fav", "0");
String strLong1 = Long.toString(id);
String where1 = "item_id=?";
String[] value1 = { strLong1 };
sq.update("item", cv1, where1, value1);
fav.setBackgroundResource(R.drawable.favorite);
Toast.makeText(getApplicationContext(), "بستنی از لیست مورد علاقه ها حذف شد", Toast.LENGTH_LONG).show();
Cursor cursor2 = sq.rawQuery("SELECT * FROM item WHERE item_id="+id, null);
while(cursor2.moveToNext()){
String name1 = cursor2.getString(cursor2.getColumnIndex("item_fav"));
Log.i("LOG", name1);
}
}
}
});
بعد توی صفحه ای لیست علاقه مندی ها با این کد برای اونایی که item_fav 1 دارن یه تکست ویو با لینک میسازم
MyDatabase my;
String id = null;
String name=null;
SQLiteDatabase sq;
my = new MyDatabase(getApplicationContext());
sq = my.getWritableDatabase();
String query = "SELECT * FROM item WHERE item_fav=1";
Cursor mCursor = sq.rawQuery(query, null);
if (mCursor != null) {
while(mCursor.moveToNext()){
name = mCursor.getString(mCursor.getColumnIndex("item_title"));
id = mCursor.getString(mCursor.getColumnIndex("item_id"));
final long id1 = Long.valueOf(id).longValue() ;
final String name1 = name;
Log.i("mCursor", name);
TextView tv1 = new TextView(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(10, 30, 10, 30);
tv1.setLayoutParams(llp);
Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/btitr.ttf");
tv1.setTypeface(tf2);
tv1.setTextSize(18);
tv1.setTextColor(Color.parseColor("#F61E4F"));
tv1.setText(name);
tv1.setGravity(Gravity.CENTER);
layFav.addView(tv1);
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
i.putExtra("product", name1);
i.putExtra("id", id1);
startActivity(i);
}
});
}
}
پاسخ به سوال
javad-PSH
12 سال پیش
0
0
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jpdesign.minedic;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.content.Intent;
import android.graphics.Typeface;
/**
* Displays a word and its definition.
*/
public class WordActivity extends Activity {
ToggleButton AddtoFav;
private TextView mWord;
public static TextView mDefinition;
public static TextView nosearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean lights=G.prefrences.getBoolean("LIGHTS", true);
if(lights)getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
String font = G.prefrences.getString("Font_selector", "Nazanin");
String path = "font/"+font+".ttf";
Typeface myfont=Typeface.createFromAsset(getAssets(),path);
setContentView(R.layout.word);
mWord = (TextView) findViewById(R.id.word);
mDefinition = (TextView) findViewById(R.id.definition);
nosearch=(TextView)findViewById(R.id.nosearch);
nosearch.setTypeface(myfont);
mDefinition.setTypeface(myfont);
//add to favorite
AddtoFav = (ToggleButton) findViewById(R.id.AddToFav);
AddtoFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (AddtoFav.isChecked()) {
Toast.makeText(WordActivity.this, "به موارد علاقه مندی اضافه شد", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(WordActivity.this, "از موارد علاقه مندی حذف شد", Toast.LENGTH_SHORT).show();
}
}
});
//Button addFav=(Button)findViewById(R.id.addFav);
Button BackB = (Button)findViewById(R.id.BackB);
BackB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
overridePendingTransition(R.anim.push_right_in,
R.anim.push_right_out);
}
});
//// end of font set
Intent intent = getIntent();
String word = intent.getStringExtra("word");
String definition = intent.getStringExtra("definition");
mWord.setText(word);
mDefinition.setText(definition);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.push_right_in,
R.anim.push_right_out); }
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .