我想在我的应用中有一个收藏夹列表,但我不知道该怎么做。基本上,当在活动的菜单栏中按下星形按钮时,我希望将自定义链接/按钮添加到另一个活动的收藏夹菜单中。
任何帮助都很棒。
提前感谢!
这里的编辑是我所在的地方:
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>
我一直试图获取所有必要的信息,以填充我在我的活动中添加的动作栏项目中的列表视图项目。
@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);
答案 0 :(得分:1)
我的建议..
点击收藏夹图标:
为它创建一个listView和一个适配器,它从db或sharedPref中获取活动名称。
为列表视图设置单击侦听器。
我的建议..
点击收藏夹图标:
为它创建一个listView和一个适配器,它从db或sharedPref中获取活动名称。
为列表视图设置单击侦听器。
修改强>
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();
现在,可以从任何活动中使用这些键值对:
获取实例:
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
通过传递右键获取值:
boolean boolValue = wmbPreference.getBoolean(“key”,true);
就共享先例而言,这就是它。