我想在我的应用程序中有一个收藏夹列表,但我不确定该怎么做。基本上,当在 Activity 的菜单栏中按下星号按钮时,我希望将自定义链接/按钮添加到另一个 Activity 的收藏夹菜单中。
任何帮助都很棒。
提前致谢!
编辑这里是我所在的地方:
public class MainActivity extends Activity implements OnItemClickListener {
ListView lv;
List<ListViewItem> items;
CustomListViewAdapter adapter;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
ListViewItem item = items.get(position);
items.remove(item);
adapter = new CustomListViewAdapter(this, items);
lv.setAdapter(adapter);
}
public static final String PREFS = "examplePrefs";
String LINK = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.ListView);
items = new ArrayList<MainActivity.ListViewItem>();
items.add(new ListViewItem()
{{
ThumbnailResource = R.drawable.ic_launcher;
Title = "Item1";
SubTitle = "Item1 desciption";
}});
items.add(new ListViewItem()
{{
ThumbnailResource = R.drawable.ic_launcher;
Title = "Item2";
SubTitle = "Item2 desciption";
}});
adapter = new CustomListViewAdapter(this,items);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
class ListViewItem
{
public int ThumbnailResource;
public String Title;
public String SubTitle;
}
这是我的 ListView 适配器.java
public class CustomListViewAdapter extends BaseAdapter
LayoutInflater inflater;
List<ListViewItem> items;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ListViewItem item = items.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);
ImageView imgThumbnail = (ImageView) vi.findViewById(R.id.imgThumbnail);
TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);
imgThumbnail.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.SubTitle);
return vi;
}
这是我的项目行.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dip">
<ImageView
android:layout_width="78dip"
android:layout_height="78dip"
android:id="@+id/imgThumbnail"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="-3dip"
android:scaleType="centerInside">
</ImageView>
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/txtTitle"
android:layout_toRightOf="@+id/imgThumbnail"
android:layout_marginTop="6dip"
android:layout_marginLeft="6dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/txtSubTitle"
android:layout_toRightOf="@+id/imgThumbnail"
android:layout_below="@+id/txtTitle"
android:layout_marginTop="3dip"
android:layout_marginLeft="6dip">
</TextView>
我一直在尝试获取所有必要的信息,以从我添加到要收藏的 Activity 中的操作栏项目中填充 ListView 项目。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favourite, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
//respond to menu item selection
switch (item.getItemId()) {
case R.id.favourite1:
//this is where shared prefrences is created?
return true;
default:
return super.onOptionsItemSelected(item);
最佳答案
我的建议..
点击收藏图标:
- 获取当前 Activity 的名称并将其持久化。(db、sharedpref .. 您的选择)。
为它创建一个 listView 和一个适配器,它从数据库或 sharedPref 中获取 Activity 名称。
为 ListView 设置点击监听器。
- 获取点击的 ListView 的名称。
- 使用选定的 val 调用 intent。
我的建议..
点击收藏图标:
- 获取当前 Activity 的名称并将其持久化。(db、sharedpref .. 您的选择)。
为它创建一个 listView 和一个适配器,它从数据库或 sharedPref 中获取 Activity 名称。
为 ListView 设置点击监听器。
- 获取点击的 ListView 的名称。
- 使用选定的 val 调用 intent。
编辑:
Sharedpreference 是 android 中持久化数据的方式之一。(其他是数据库、文件等)。 sharedPreference 文件以键值格式存储。
获取 sharedpreference 类和编辑器类的实例:
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = wmbPreference.edit();
在 sharedPrefrence 中添加值:
editor.putBoolean("key", value); editor.putFloat("key1", value); editor.putInt("key2", value); editor.putLong("key3", value); editor.putString("key4", value); editor.putStringSet("key5", values);
保留这些插入的值:
editor.commit();
现在,可以在任何 Activity 中使用这些键值对:
获取实例:
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
通过右键获取值:
boolean boolValue= wmbPreference.getBoolean("key", true);
就是这样,就 sharedprefrence 而言。
关于android - 如何将 Activity 添加到收藏夹列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17627125/