当点击ListView中的选择项时,LogCat中没有发生任何错误但没有错误

问题描述:

我在我的ListView上有问题选择项。当我点击什么都没有发生,我不能去下一个活动,因为当前listView不可点击。 我尝试逐行调试代码,但它似乎不进入public void onItemClick部分。为什么它变成这样?当点击ListView中的选择项时,LogCat中没有发生任何错误但没有错误

这里是我的代码,

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.Button; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 

public class ViewAllBills extends AppCompatActivity implements ListView.OnItemClickListener { 

    private ListView listView; 

    private Button mydebtsBtn; 

    private String JSON_STRING; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view_all_bills); 
     listView = (ListView) findViewById(R.id.listView); 
     listView.setOnItemClickListener(this); 
     getJSON(); 
     myDebt(); 
    } 


    //for new bill 
    public void myDebt() { 
     mydebtsBtn = (Button) findViewById(R.id.buttonMyDebt); 
     mydebtsBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       Intent page1 = new Intent(ViewAllBills.this,myDebt.class); 
       startActivity(page1); 
      } 
     });} 


    private void showBills(){ 
     JSONObject jsonObject = null; 
     ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>(); 
     try { 
      jsonObject = new JSONObject(JSON_STRING); 
      JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

      for(int i = 0; i<result.length(); i++){ 
       JSONObject jo = result.getJSONObject(i); 
       String id = jo.getString(Config.TAG_ID); 
       String desc = jo.getString(Config.TAG_DESCRIPTION); 
       String amo = jo.getString(Config.TAG_AMOUNT); 

       HashMap<String,String> bills = new HashMap<>(); 
       bills.put(Config.TAG_ID,id); 
       bills.put(Config.TAG_DESCRIPTION,desc); 
       bills.put(Config.TAG_AMOUNT,amo); 
       list.add(bills); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     ListAdapter adapter = new SimpleAdapter(
       ViewAllBills.this, list, R.layout.list_item, 
       new String[]{Config.TAG_ID,Config.TAG_DESCRIPTION,Config.TAG_AMOUNT}, 
       new int[]{R.id.id, R.id.description,R.id.amountBill}); 

     listView.setAdapter(adapter); 
    } 

    private void getJSON(){ 
     class GetJSON extends AsyncTask<Void,Void,String>{ 

      ProgressDialog loading; 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loading = ProgressDialog.show(ViewAllBills.this,"Fetching Data","Wait...",false,false); 
      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       loading.dismiss(); 
       JSON_STRING = s; 
       showBills(); 
      } 

      @Override 
      protected String doInBackground(Void... params) { 
       RequestHandler rh = new RequestHandler(); 
       String s = rh.sendGetRequest(Config.URL_GET_ALL); 
       return s; 
      } 
     } 
     GetJSON gj = new GetJSON(); 
     gj.execute(); 

    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Intent intent = new Intent(ViewAllBills.this, ViewBills.class); 
     HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position); 
     String billId = map.get(Config.TAG_ID).toString(); 
     intent.putExtra(Config.BILL_ID,billId); 
     startActivity(intent); 
    } 

} 

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    > 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:background="@color/skyBlue"> 

     <Button android:id="@+id/buttonMyDebt" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_marginTop="10dip" 
      android:background="@color/colorAccent" 
      android:textColor="@color/black" 
      android:textStyle="bold" 
      android:focusable="false" 
      android:text="My Debt" 
      android:onClick="myDebt"/> 
     <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:columnCount="3" 
      android:rowCount="3" 

      android:background="@color/softGrey" 
      android:layout_marginTop="5dp"> 
      <TextView 
       android:id="@+id/ids" 
       android:layout_width="40dp" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_marginLeft="10dp" 
       android:textStyle="bold" 
       android:layout_gravity="center" 
       android:text="ID" 
       /> 

      <TextView 
       android:id="@+id/descriptions" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:layout_marginTop="10dp" 
       android:text="Bill Description" 
       android:layout_gravity="center" 
       /> 

      <TextView 
       android:id="@+id/amounts" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:layout_marginTop="10dp" 
       android:text="Amount (RM)" 
       android:layout_gravity="right" 
       /> 
     </GridLayout> 


     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="5dp" 
      android:id="@+id/listView" /> 

    </LinearLayout> 
</ScrollView> 

这是我List_item.xml布局

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    android:background="@color/skyBlue" 
    > 

    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:columnCount="3" 
     android:rowCount="3" 
     android:background="@drawable/border_style" 
     android:layout_marginTop="5dp"> 
     <TextView 
      android:id="@+id/id" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      /> 

     <TextView 
      android:id="@+id/amountBill" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:layout_gravity="right" 
      /> 
     </GridLayout> 
</ScrollView> 
+0

你能分享你的行项目布局从列表视图? – Opiatefuchs

+0

Opiatefuchs,我已经分享了我的布局。 –

+0

我的意思是你的'R.layout.list_item'布局.... – Opiatefuchs

试试这个:

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.Button; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 

public class ViewAllBills extends AppCompatActivity implements ListView.OnItemClickListener { 

    private Button mydebtsBtn; 
    private String JSON_STRING; 

    private ListView listView; 

    ArrayList<HashMap<String,String>> list; 

    ListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view_all_bills); 

     listView = (ListView) findViewById(R.id.listView); 

     list = new ArrayList<HashMap<String, String>>(); 

     getJSON(); 
     myDebt(); 
    } 


    //for new bill 
    public void myDebt() { 
     mydebtsBtn = (Button) findViewById(R.id.buttonMyDebt); 
     mydebtsBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       Intent page1 = new Intent(ViewAllBills.this,myDebt.class); 
       startActivity(page1); 
      } 
     });} 


    private void showBills(){ 
     JSONObject jsonObject = null; 
     try { 
      jsonObject = new JSONObject(JSON_STRING); 
      JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

      for(int i = 0; i<result.length(); i++){ 
       JSONObject jo = result.getJSONObject(i); 
       String id = jo.getString(Config.TAG_ID); 
       String desc = jo.getString(Config.TAG_DESCRIPTION); 
       String amo = jo.getString(Config.TAG_AMOUNT); 

       HashMap<String,String> bills = new HashMap<>(); 
       bills.put(Config.TAG_ID,id); 
       bills.put(Config.TAG_DESCRIPTION,desc); 
       bills.put(Config.TAG_AMOUNT,amo); 
       list.add(bills); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     adapter = new SimpleAdapter(
       ViewAllBills.this, list, R.layout.list_item, 
       new String[]{Config.TAG_ID,Config.TAG_DESCRIPTION,Config.TAG_AMOUNT}, 
       new int[]{R.id.id, R.id.description,R.id.amountBill}); 

     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(this); 
    } 

    private void getJSON(){ 
     class GetJSON extends AsyncTask<Void,Void,String>{ 

      ProgressDialog loading; 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loading = ProgressDialog.show(ViewAllBills.this,"Fetching Data","Wait...",false,false); 
      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       loading.dismiss(); 
       JSON_STRING = s; 
       showBills(); 
      } 

      @Override 
      protected String doInBackground(Void... params) { 
       RequestHandler rh = new RequestHandler(); 
       String s = rh.sendGetRequest(Config.URL_GET_ALL); 
       return s; 
      } 
     } 
     GetJSON gj = new GetJSON(); 
     gj.execute(); 

    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Intent intent = new Intent(ViewAllBills.this, ViewBills.class); 
     HashMap<String,String> map = list.get(position); 
     String billId = map.get(Config.TAG_ID).toString(); 
     intent.putExtra(Config.BILL_ID,billId); 
     startActivity(intent); 
    } 

} 
+1

请解释为什么这应该工作....发布只有整个代码不会让问题的人知道出了什么问题。 – Opiatefuchs

+0

Ferdous艾哈迈德,我尝试根据您的建议代码修改我的代码,但仍然无法点击lsitview.when我点击其中一个项目仍然没有发生.. –

如果你在你的列表视图中有一个活动的视图/可聚焦视图,那么它将禁用你的onItemClickListener.you可以尝试通过添加以下内容来使其不可聚焦:android:focusableInTouchMode =“false”到任何通常可以聚焦的视角。
我面临同样的问题,设置机器人:focusableInTouchMode =“假”解决我的问题

+0

Sandeep dhiman,你意味着把android:focusableInTouchMode =“false”进入我的所有视图和按钮? –

+0

@SarahRose没有把你的listview项目布局 –

+0

的主布局放在我上面的xml中,那么我应该把我的ScrolView或GridLayout? –

我的问题解决了.. 其实我复制scrolview布局在我list_item.xml ..之后我删除了scrolview离开只有gridlayout,选择列表项目工作..