onItemClickListener不适用于导航抽屉中的列表视图

时间:2016-04-30 16:58:35

标签: java android xml android-layout android-fragments

我有一个导航抽屉,它是一个片段。现在我想为navDrawer的ListView中的项设置onitemclickListener。但我不能这样做。 Java类:

public class Welcome extends AppCompatActivity {
private drawer_navigation dn;
private ListView lv;
private List<HashMap<String,Object>> list= new ArrayList<>();

private final String get_cat="http://advertapp.siavoshdjazmi.ir/get_cat.php";
private final String get_ads="http://advertapp.siavoshdjazmi.ir/get_data.php?page=";
private final String get_ads_by_cat="http://advertapp.siavoshdjazmi.ir/get_data_by_cat.php?cat=";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    Toolbar toolbar= (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    dn = (drawer_navigation) getSupportFragmentManager().findFragmentById(R.id.drawer_navigation);
    dn.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher);
    lv= (ListView)findViewById(R.id.category_lv);

    makeCategoryList();
    View v= findViewById(R.id.category_lv);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         Intent i= new Intent(getApplicationContext(),Advertisment_list.class);
           i.putExtra("url_data_by_id",get_ads_by_cat+list.get(position).get("id").toString());
           i.putExtra("flag","1");
                          startActivity(i);
         Log.i("app_error","Click Listener");
           my_alert("test","test",true);
       }
   });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater= getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id=item.getItemId();
    if(id==R.id.settings){
        Toast.makeText(getApplicationContext(),"تنظیمات برنامه",Toast.LENGTH_LONG).show();
    }
    return true;
}
/* Category List Of Navigation Drawer */
public void makeCategoryList(){
    DownloaderCategory dc= new DownloaderCategory();
    dc.execute(get_cat);

}
private class DownloaderCategory extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String... params) {
        String temp="";
        try{
        JSONDownloader jd= new JSONDownloader();
         temp= jd.downloadUrl(params[0]);}
        catch (Exception e){
            Log.i("app_error", "Error in Download Category Task Class//////" + e.toString());
        }
        return temp;
    }

    @Override
    protected void onPostExecute(String s) {
        LoadCategoryList lc= new LoadCategoryList();
        lc.execute(s);
    }
}
private class LoadCategoryList extends  AsyncTask<String, Void, SimpleAdapter>{

    @Override
    protected SimpleAdapter doInBackground(String... params) {
        try {
            ParseCat pc = new ParseCat();

            list.addAll(pc.parse(params[0]));
        }
        catch (Exception e){
            Log.i("app_error", "Error in Download Category Task Class//////" + e.toString());
        }
        String[] from={"name","count"};
        int[] to ={R.id.cat_name,R.id.cat_count};
       // myAdapter adapter = new myAdapter(getBaseContext(),list,R.layout.category_list_drawer,from,to);
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),list,R.layout.category_list_drawer,from,to);

        return adapter;
    }

    @Override
    protected void onPostExecute(SimpleAdapter simpleAdapter) {

            lv.setAdapter(simpleAdapter);

        }

}

XML文件:`

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="192dp"
    android:background="@drawable/bgnav"
    android:layout_marginBottom="0dp"
    android:id="@+id/FrameLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/drawer_header_app_name"
        android:id="@+id/txt__view"
        android:layout_gravity="center"
        android:textSize="25dp"
        android:layout_marginBottom="10dp"
        android:textColor="#080000"
        android:textStyle="bold" />

</FrameLayout>

<LinearLayout
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:clickable="false"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal">

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="درباره ما"
        android:id="@+id/about_us_btn"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:shadowColor="#0b0a0a"
        android:textSize="25dp"
        android:background="#ee7a36"
        android:layout_marginBottom="15dp"
        android:onClick="onAboutMeClick" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="خروج"
        android:id="@+id/exit_btn_nav"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"

        android:textSize="25dp"
        android:background="#ee7a36"
        android:layout_marginBottom="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="دسته بندی"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/category_lv"
        android:layout_gravity="center_horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:clickable="false"
        android:focusable="false"
        />

</LinearLayout>

catrgory_list_drawer.XML:

<?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="wrap_content">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:background="#da4444"
    android:clickable="false"
    android:contextClickable="false">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/category_drawer_list_img"
        android:src="@drawable/advertisment"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:contentDescription="@string/drawer_list_img" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cat_name"
        android:layout_marginTop="20dp"
        android:layout_marginRight="5dp"
        android:textSize="20dp"
        android:editable="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cat_count"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="5dp"
        android:editable="true" />

</LinearLayout>
</LinearLayout>

`

如果有人帮助我,我将非常感激!:)

0 个答案:

没有答案