我想在我的应用程序中有一个收藏夹列表,但我不知道该怎么做。基本上,当在活动的菜单栏中按下星形按钮时,我希望将自定义链接/按钮添加到另一个活动的收藏夹菜单中。
任何帮助都很棒。
提前致谢!
编辑这里是我所在的位置:
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;
}
这是我的列表视图适配器 .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);