我有一个OnItemClickListener
并不想解雇:
我有一项活动,其中ViewPager
设置了adapter
和TabLayout
,以便制作滑动标签。
这是:
public class HierarchieActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hierarchie);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SampleFragmentPagerAdapter pagerAdapter = new SampleFragmentPagerAdapter(getSupportFragmentManager(), HierarchieActivity.this);
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
Toolbar toolbar = (Toolbar)findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
}
在适配器(SampleFragmentPagerAdapter
)中,我有两个fragments
,其中包含包含TextViews和ListViews的LinearLayouts。
这是一个片段及其布局:
public class PageFragmentHierarchie extends android.support.v4.app.Fragment {
public static FileDAO dao = null;
public static int etageMax = 1;
public static int etageCourant = 1;
public ListView listDossier;
public ListView listDeck;
public static PageFragmentHierarchie newInstance(int page) {
Bundle args = new Bundle();
PageFragmentHierarchie fragment = new PageFragmentHierarchie();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page_hierarchie, container, false);
listDossier = (ListView)view.findViewById(R.id.listDossier);
listDeck = (ListView)view.findViewById(R.id.listDeck);
dao = new FileDAO(getContext());
final SQLiteDatabase mDb = dao.open();
// Here I'm making the adapter from a database
Cursor cursorDossier = mDb.rawQuery("SELECT " + KEY + ", " + DOSSIER + " FROM " + TABLE_NAME + " WHERE " + DOSSIER +" IS NOT NULL AND " + ETAGE + "= ?;", new String[]{"1"});
Cursor cursorDeck = mDb.rawQuery("SELECT " + KEY + ", " + DECK + " FROM " + TABLE_NAME + " WHERE " + DECK+" IS NOT NULL AND " + ETAGE + "= ?;", new String[]{"1"});
SimpleCursorAdapter adapterDossier = new SimpleCursorAdapter(getContext(), R.layout.layout_dossiers, cursorDossier, new String[]{DOSSIER}, new int[]{R.id.text_dossier});
SimpleCursorAdapter adapterDeck = new SimpleCursorAdapter(getContext(), R.layout.layout_decks, cursorDeck, new String[]{DECK}, new int[]{R.id.text_deck},0);
listDossier.setAdapter(adapterDossier);
listDeck.setAdapter(adapterDeck);
listDossier.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.v(null,"CA MARCHE");
// The problem cant be in the following part, I tried to put it as notes
/*TextView textView = (TextView)view.findViewById(R.id.text_dossier);
String text = textView.getText().toString();
Cursor c = mDb.query(TABLE_NAME, new String[]{ETAGE}, "WHERE " + DOSSIER + "= ?", new String[]{text}, null,null,null);
etageCourant = c.getInt(0);
Cursor cursorDossier = mDb.rawQuery("SELECT " + KEY + ", " + DOSSIER + " FROM " + TABLE_NAME + " WHERE " + DOSSIER +" IS NOT NULL AND " + ETAGE + "= ?;", new String[]{String.valueOf(etageCourant)});
Cursor cursorDeck = mDb.rawQuery("SELECT " + KEY + ", " + DECK + " FROM " + TABLE_NAME + " WHERE " + DECK +" IS NOT NULL AND " + ETAGE + "= ?;", new String[]{String.valueOf(etageCourant)});
afficherHierarchie(cursorDossier, cursorDeck, listDossier, listDeck);
getActivity().finish();
startActivity(getActivity().getIntent());*/
}
});
return view;
}
public void afficherHierarchie(Cursor cursorDossier, Cursor cursorDeck, ListView listDossier, ListView listDeck){
SimpleCursorAdapter adapterDossier = new SimpleCursorAdapter(getContext(), R.layout.layout_dossiers, cursorDossier, new String[]{DOSSIER}, new int[]{R.id.text_dossier},0);
SimpleCursorAdapter adapterDeck = new SimpleCursorAdapter(getContext(), R.layout.layout_decks, cursorDeck, new String[]{DECK}, new int[]{R.id.text_deck},0);
listDossier.setAdapter(adapterDossier);
listDeck.setAdapter(adapterDeck);
}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:id="@+id/FragmentHierarchie">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dossiers :" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aléatoire :"
android:layout_marginLeft="270dp"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="270dp"
android:id="@+id/listDossier"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decks :" />
<ListView
android:layout_width="match_parent"
android:layout_height="270dp"
android:id="@+id/listDeck" />
</LinearLayout>
我的问题是:我尝试在其中一个OnItemClickListener
上设置ListView
,我在OnCreateView
的{{1}}方法中设置了fragment
(我认为是通常已经完成但是然后监听器根本不起作用:当我点击列表的项目时没有任何反应(尝试使用Log.v)
如果你花些时间帮助我,我会非常感谢。