当我克隆列表项时,如何将此活动更改为其他活动。 在我的Android应用程序中,我有两个活动类。我有一个自定义列表视图,在点击的第一个项目上显示第二个活动: 我在onitemcliklisten上写的内容 **这是我的FragmentList.java文件** 谢谢
FragmentList.java
package com.kbhtech.sharedpreferencedemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class FragmentList extends Fragment {
private String getActivity;
public static FragmentList newInstance() {
FragmentList fragment = new FragmentList();
return fragment;
}
public FragmentList() {
// Required empty public constructor
}
Context activity;
ListView postsListView;
PostsListAdapter postsListAdapter;
ArrayList<BeanSampleList> postsBeanSampleList =new ArrayList<BeanSampleList>();
SharedPreference sharedPreference;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
activity = getActivity();
postsBeanSampleList=new ArrayList<BeanSampleList> ( );
sharedPreference = new SharedPreference();
postsBeanSampleList.add( new BeanSampleList( 1, "Chapter 1", "Sec 1-5" ) );
postsBeanSampleList.add( new BeanSampleList( 2, "Chapter 2", "Sec 6-25A " ) );
postsBeanSampleList.add( new BeanSampleList( 3, "Chapter 3", "Sec 26-35" ) );
postsBeanSampleList.add( new BeanSampleList( 4, "Chapter 4", "Moscow" ) );
postsBeanSampleList.add( new BeanSampleList( 5, "Chapter 5", "Brasília" ) );
postsBeanSampleList.add( new BeanSampleList( 6, "Chapter 6", "Berlin" ) );
postsBeanSampleList.add( new BeanSampleList( 7, "Chapter 7", "Paris" ) );
postsBeanSampleList.add( new BeanSampleList( 8, "Chapter 8", "Dublin" ) );
postsBeanSampleList.add( new BeanSampleList( 9, "Chapter 9", "Rome" ) );
postsBeanSampleList.add( new BeanSampleList( 10, "Chapter 10", "Tokyo" ) );
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container,false);
postsListView = (ListView) view.findViewById(R.id.posts_list);
postsListAdapter = new PostsListAdapter(activity, postsBeanSampleList);
postsListView.setAdapter(postsListAdapter);
postsListView.setAdapter(postsListAdapter);
postsListView.setAdapter( postsListAdapter );
postsListView.setOnItemClickListener ( new AdapterView.OnItemClickListener () {
@Override
public void onItemClick(AdapterView <?> adapterView , View view , int i , long l) {
if (i == 0) {
Intent intent = new Intent(activity, Chapter1.class);
startActivity(intent);
} else if (i == 1) {
Intent intent = new Intent(activity, Chapter2.class);
startActivity(intent);
}
}
} );
return view;
}
@Override
public void onResume() {
super.onResume();
postsListAdapter.notifyDataSetChanged();
}
public class PostsListAdapter extends BaseAdapter {
private Context context;
ArrayList<BeanSampleList> postBeanSampleList;
SharedPreference sharedPreference;
public PostsListAdapter(Context context, ArrayList<BeanSampleList> postBeanSampleList) {
this.context = context;
this.postBeanSampleList = postBeanSampleList;
sharedPreference = new SharedPreference();
}
private class ViewHolder {
TextView txtTitle,txtSubTitle;
ImageView btnFavourite;
}
@Override
public int getCount() {
return postBeanSampleList.size();
}
@Override
public Object getItem(int position) {
return postBeanSampleList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_posts_list, parent, false);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView
.findViewById(R.id.txtPostTitle);
holder.txtSubTitle = (TextView) convertView
.findViewById(R.id.txtPostSubTitle);
holder.btnFavourite = (ImageView) convertView
.findViewById(R.id.favouritesToggle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BeanSampleList beanSampleList = (BeanSampleList) getItem(position);
holder.txtTitle.setText(beanSampleList.getTitle());
holder.txtSubTitle.setText(beanSampleList.getSubTitle());
if (checkFavoriteItem(beanSampleList)) {
holder.btnFavourite.setImageResource(R.drawable.ic_favorite);
holder.btnFavourite.setTag("active");
} else {
holder.btnFavourite.setImageResource(R.drawable.ic_favorite_outline);
holder.btnFavourite.setTag("deactive");
}
holder.btnFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String tag = holder.btnFavourite.getTag().toString();
if (tag.equalsIgnoreCase("deactive")) {
sharedPreference.addFavorite(context, postBeanSampleList.get(position));
holder.btnFavourite.setTag("active");
holder.btnFavourite.setImageResource(R.drawable.ic_favorite);
} else {
sharedPreference.removeFavorite(context, postBeanSampleList.get(position));
holder.btnFavourite.setTag("deactive");
holder.btnFavourite.setImageResource(R.drawable.ic_favorite_outline);
}
}
});
return convertView;
}
public boolean checkFavoriteItem(BeanSampleList checkProduct) {
boolean check = false;
List<BeanSampleList> favorites = sharedPreference.loadFavorites(context);
if (favorites != null) {
for (BeanSampleList product : favorites) {
if (product.equals(checkProduct)) {
check = true;
break;
}
}
}
return check;
}
}
}
ListActivity.java(MainAct ...)
package com.kbhtech.sharedpreferencedemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
public class ListActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my );
txtHeader.setText("Posts List");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
FragmentList fragmentList = FragmentList.newInstance();
if (manager.findFragmentByTag("fragment_list") == null) {
ft.replace(R.id.main_content, fragmentList, "fragment_list").commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favourites:
Intent i=new Intent(ListActivity.this,FavouriteListActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
}
FragmentFavourite.java
package com.kbhtech.sharedpreferencedemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class FragmentFavourite extends Fragment {
private ListView favouriteListView;
public static FavouritesListAdapter favouritsListAdapter;
private ArrayList<BeanSampleList> favouritesBeanSampleList;
SharedPreference sharedPreference;
public static FragmentFavourite newInstance() {
FragmentFavourite fragment = new FragmentFavourite();
return fragment;
}
public FragmentFavourite() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreference=new SharedPreference();
try {
favouritesBeanSampleList = sharedPreference.loadFavorites(getActivity());
} catch (NullPointerException e){
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView= inflater.inflate(R.layout.fragment_favourite, container, false);
favouriteListView =(ListView)rootView.findViewById(R.id.favourits_list);
return rootView;
}
@Override
public void onResume() {
super.onResume();
Log.e("onResume", "onResume Called");
if(favouritesBeanSampleList != null ) {
try {
favouritsListAdapter = new FavouritesListAdapter(getActivity(), favouritesBeanSampleList);
favouriteListView.setAdapter(favouritsListAdapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
favouritsListAdapter.notifyDataSetChanged();
}
}
public class FavouritesListAdapter extends BaseAdapter {
Context context;
List<BeanSampleList> modellist;
LayoutInflater inflater;
ArrayList<BeanSampleList> favouritesBeanSampleList;
public FavouritesListAdapter(Context context, ArrayList<BeanSampleList> favouritesBeanSampleList) {
this.context = context;
this.favouritesBeanSampleList = favouritesBeanSampleList;
this.favouritesBeanSampleList=new ArrayList <BeanSampleList> ( );
this.modellist= favouritesBeanSampleList;
inflater=LayoutInflater.from ( context );
}
public int getCount() {
return favouritesBeanSampleList.size();
}
public Object getItem(int position) {
return favouritesBeanSampleList.get(position);
}
public long getItemId(int position) {
return position;
}
class ViewHolder {
TextView txtTitle,txtSubTitle;
ImageView btnFavourite;
}
public View getView(final int position, View convertView,ViewGroup parent) {
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_favourites_list, parent, false);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtFavtitle);
holder.txtSubTitle = (TextView) convertView.findViewById(R.id.txtFavSubTitle);
BeanSampleList selectfavouritesBeanSampleList =favouritesBeanSampleList.get( position );
holder.btnFavourite = (ImageView) convertView
.findViewById(R.id.favouritesToggleImg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(favouritesBeanSampleList.get(position).getTitle());
holder.txtSubTitle.setText(favouritesBeanSampleList.get(position).getSubTitle());
holder.btnFavourite.setImageResource(R.drawable.ic_favorite);
holder.btnFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sharedPreference.removeFavorite(context, favouritesBeanSampleList.get(position));
favouritesBeanSampleList.remove(favouritesBeanSampleList.get(position));
holder.btnFavourite.setImageResource(R.drawable.ic_favorite_outline);
notifyDataSetChanged();
}
});
convertView.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View view) {
}
} );
return convertView;
}
}
}
第1章课
package com.kbhtech.crpc4jse;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Chapter1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_chapter1 );
}
}
activity_chapter1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Chapter1">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="30sp"
android:layout_marginTop="20dp"
android:text="Chapter 1"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/txtch1s1"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="25sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="प्रारंभिक( preliminary)"/>
<TextView
android:id="@+id/txtch1s2"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="25sp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Sec 1-5 " />
<TextView
android:id="@+id/txtch1d1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textStyle="normal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="23sp"
android:text="@string/chapter" />
</LinearLayout>
</ScrollView>
</RelativeLayout>