2017-05-07 3 views
1

Ich habe Probleme, wählen Sie Artikel auf meinem ListView. Als ich geklickt habe passiert nichts und ich konnte nicht zur nächsten Aktivität gehen, da die aktuelle listView nicht anklickbar ist. Ich habe versucht Code Zeile für Zeile zu debuggen, aber es scheint, als ob es nicht in den public void onItemClick Teil eingeben. Warum wurde es so?Wenn angeklickt wählen Sie Element in ListView nichts passiert, aber kein Fehler in LogCat

Hier ist mein Code,

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

} 

Dies ist meine XML-Datei

<?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> 

Dieses Layout mein List_item.xml ist

<?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

Können Sie Ihr Zeilenelementlayout von listview aus teilen? – Opiatefuchs

+0

Opiatefuchs, ich teile schon mein Layout. –

+0

Ich meine dein 'R.layout.list_item' Layout .... – Opiatefuchs

Antwort

0

Try this:

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

Bitte erklären Sie, warum das funktionieren sollte .... das Veröffentlichen nur des ganzen Codes lässt den Fragesteller nicht wissen, was schief gelaufen ist. – Opiatefuchs

+0

Ferdous Ahamed, ich versuche, meinen Code basierend auf Ihrem Vorschlag Code zu ändern, aber immer noch nicht auf die lsitview.Wenn ich auf einen der Artikel immer noch nichts passiert .. –

0

Wenn Sie eine aktive Ansicht/fokussierbare Ansicht in Ihrer Listenansicht haben, wird Ihr onItemClickListener deaktiviert.Sie ​​können versuchen, ihn unfokussierbar zu machen, indem Sie hinzufügen: android: focusableInTouchMode = "false" zu jeder Ansicht, die normalerweise fokussierbar ist.
Ich war vor dem gleichen Problem und Einstellung android: focusableInTouchMode = „false“ mein Problem lösen

+0

Sandeep Dhiman, Sie bedeutet, dass Android setzen: focusableInTouchMode = "false" in alle meine Sicht und Knopf? –

+0

@SarahRose nein setzen Sie es Hauptlayout Ihres Listenansichtsartikelentwurfs –

+0

von meinem xml über, dann sollte ich in meine ScrolView oder GridLayout setzen? –

0

Mein Problem .. gelöst Eigentlich Duplizieren ich das scrolview Layout in meinem list_item.xml .. nachdem ich die scrolview entfernt und links nur das gridlayout, das select list item working ..

Verwandte Themen