我正在android studio中构建一个应用程序。当我点击查看第二个活动没有启动的列表项的详细信息时启动主要活动。我已经在名为Onlistitemclick的方法中指定了popuplistfragment.java中的意图并指定了滑动视图中的oncreate方法中的包。可以帮忙吗?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public class PopupListFragment extends ListFragment implements View.OnClickListener {
//String pageData[];
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<String> items = new ArrayList<String>();
for (int i = 0, z = Cheeses.QUOTES.length ; i < z ; i++) {
items.add(Cheeses.QUOTES[i]);
}
setListAdapter(new PopupAdapter(items));
}
@Override
public void onListItemClick(ListView listView, View v, int position, long id) {
String item = (String) listView.getItemAtPosition(position);
Intent myIntent = new Intent(PopupListFragment.this.getActivity(), SwipeViews.class);
myIntent.putExtra("key", item);
startActivity(myIntent);
// Show a toast if the user clicks on an item
// Toast.makeText(getActivity(), "Item Clicked: " + item, Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
}
/**
* A simple array adapter that creates a list of cheeses.
*/
class PopupAdapter extends ArrayAdapter<String> {
PopupAdapter(ArrayList<String> items) {
super(getActivity(), R.layout.list_item, android.R.id.text1, items);
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
// Let ArrayAdapter inflate the layout and set the text
View view = super.getView(position, convertView, container);
return view;
}
}
}
public class SwipeViews extends Activity {
String pageData[]; //Stores the text to swipe.
LayoutInflater inflater; //Used to create individual pages
ViewPager vp; //Reference to class to swipe views
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.swipe_view);
//Get the data to be swiped through
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
String item = extras.getString("key");
textview.setText(item);
}
pageData=getResources().getStringArray(R.array.quotes);
//get an inflater to be used to create single pages
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Reference ViewPager defined in activity
vp=(ViewPager)findViewById(R.id.viewPager);
//set the adapter that will create the individual pages
vp.setAdapter(new MyPagesAdapter());
}
//Implement PagerAdapter Class to handle individual page creation
class MyPagesAdapter extends PagerAdapter {
@Override
public int getCount() {
//Return total pages, here one for each data item
return pageData.length;
}
//Create the given page (indicated by position)
@Override
public Object instantiateItem(ViewGroup container, int position) {
View page = inflater.inflate(R.layout.page, null);
((TextView)page.findViewById(R.id.textMessage)).setText(pageData[position]);
//Add the page to the front of the queue
((ViewPager) container).addView(page, 0);
return page;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//See if object from instantiateItem is related to the given view
//required by API
return arg0==(View)arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
object=null;
}
}
}
这是清单文件。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="SuccessQuotes" >
<intent-filter>
<action android:name="com.hacktechbd.successquotes.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SwipeViews"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
确保您能够检测到点击事件。 使用像
这样的东西 Log.v("check","item is clicked");
在你的代码中。
和
尝试使用
Intent myIntent = new Intent(getActivity(), SwipeViews.class);
Intent myIntent = new Intent(PopupListFragment.this.getActivity(), SwipeViews.class);
它可能有用。