单击ListView中的选择项时,没有任何事情发生,但LogCat中没有错误

时间:2017-05-07 08:14:33

标签: java android listview mobile-application

我的A上有问题选择项目。当我点击时没有任何事情发生,我无法进入下一个活动,因为当前B无法点击。 我尝试逐行调试代码,但似乎没有进入ListView部分。为什么会这样?

这是我的代码,

listView

这是我的xml文件

public void onItemClick

这是我的List_item.xml布局

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

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我的问题解决了.. 实际上我复制了我的list_item.xml中的scrolview布局..在我删除了滚动视图并仅离开了gridlayout之后,选择列表项工作..