【问题标题】:When clicked on select item in ListView nothing happen but no error in LogCat单击 ListView 中的选择项时,没有任何反应,但 LogCat 中没有错误
【发布时间】:2017-05-07 08:14:32
【问题描述】:

我的 ListView 上的选择项目有问题。当我单击时,什么也没有发生,我无法进入下一个活动,因为当前的 listView 不可点击。 我尝试逐行调试代码,但似乎没有进入public void onItemClick 部分。为什么会变成这样?

这是我的代码,

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>

【问题讨论】:

  • 你能从列表视图分享你的行项目布局吗?
  • Opiatefuchs ,我已经分享了我的布局。
  • 我的意思是你的R.layout.list_item 布局......

标签: java android listview mobile-application


【解决方案1】:

试试这个:

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);
    }

}

【讨论】:

  • 请解释为什么这应该起作用....仅发布整个代码不会让提问者知道出了什么问题。
  • Ferdous Ahamed,我尝试根据您的建议代码修改我的代码,但仍然无法点击 lsitview。当我点击其中一项时,仍然没有任何反应..
【解决方案2】:

如果您的列表视图中有一个活动视图/可聚焦视图,那么它将禁用您的 onItemClickListener。您可以尝试通过添加:android:focusable="false" ,android:focusableInTouchMode="false" 使其无法聚焦通常可聚焦的视图。
我遇到了同样的问题,设置 android:focusableInTouchMode="false" 解决了我的问题

【讨论】:

  • Sandeep dhiman,你的意思是把 android:focusableInTouchMode="false" 放到我所有的视图和按钮中?
  • @SarahRose 没有把它放在你的列表视图项目布局的主要布局
  • 从我上面的 xml 中,那么我应该放入我的 ScrolView 或 GridLayout 吗?
  • @SarahRose 把它放在你的适配器布局中,我认为是 R.layout.list_item
  • 我已经添加了 android:focusableInTouchMode="false" ,但仍然无法点击列表项
【解决方案3】:

我的问题解决了.. 实际上我在我的 list_item.xml 中复制了 scrollview 布局。在我删除了 scrollview 并只留下了 gridlayout 之后,选择列表项正在工作..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    相关资源
    最近更新 更多