2015-09-04 146 views
0

我試圖在按下搜索按鈕時將結果填充到列表視圖中。但是,當我嘗試這樣做時,listview上沒有顯示任何內容。使用android列表視圖填充列表

我正在使用asynctask從數據庫獲取數據並將其作爲數組傳遞到listview。

感謝提前的幫助!

下面是代碼:

public class FindFriends extends Activity implements View.OnClickListener { 

EditText handphone; 
Button searchbtn; 
String name,hp; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.findfriends); 
    handphone = (EditText) findViewById(R.id.enterfn); 

    searchbtn = (Button) findViewById(R.id.searchfor); 
    searchbtn.setOnClickListener(this); 

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

    String[] values = new String[]{name}; 

    ArrayAdapter<String> codeLearnArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values); 

    listview.setAdapter(codeLearnArrayAdapter); 

} 
@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.searchfor: 
      hp = handphone.getText().toString(); 
      new AttemptLogin().execute(hp); 
      break; 
    } 

} 


class AttemptLogin extends AsyncTask<String, String, JSONObject> { 

    private ProgressDialog pDialog; 
    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 
    private static final String LOGIN_URL = "address_url"; 

    @Override 
    protected JSONObject doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     // here Check for success tag 
     try { 
      HashMap<String, String> params = new HashMap<>(); 
      params.put("hp", args[0]); 
      JSONObject json = jsonParser.makeHttpRequest(
        LOGIN_URL, "POST", params); 

      if (json != null) { 
       Log.d("JSON result", json.toString()); 

       return json; 
      } 

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

     return null; 
    } 

    protected void onPostExecute(JSONObject json) { 

     if (json != null) { 
      Toast.makeText(FindFriends.this, json.toString(), 
        Toast.LENGTH_LONG).show(); 

      try { 
       name = json.getString("name"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

findfriends.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:background="@drawable/findfriends" 
    android:layout_height="match_parent"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:id="@+id/enterfn" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enter handphone number/username to start searching" 
     android:id="@+id/textView9" 
     android:textSize="20dp" 
     android:layout_above="@+id/enterfn" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Search" 
     android:id="@+id/searchfor" 
     android:layout_below="@+id/enterfn" 
     android:layout_centerHorizontal="true" /> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" 
     android:layout_below="@+id/searchfor" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

回答

0

我的Android是新。

無論如何,我看不到你更新你的數組,在AsyncTask中你下載的數據,但你不更新值。所以,你shuld更新傳遞給適配器的數組,然後調用它的方法notifyDataSetChanged()

0

只需經過下面的代碼,並與您的replcae它,

public class FindFriends extends Activity implements View.OnClickListener { 

EditText handphone; 
Button searchbtn; 
String name = "name", hp = "hp"; 


ListView listview; 
ArrayAdapter<String> codeLearnArrayAdapter; 
ArrayList<String> values; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.findfriends); 
    handphone = (EditText) findViewById(R.id.enterfn); 

    searchbtn = (Button) findViewById(R.id.searchfor); 
    searchbtn.setOnClickListener(this); 
    listview = (ListView) findViewById(R.id.listView); 

    // adding value to arrayList 
    values.add(name); 

    codeLearnArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values); 

    listview.setAdapter(codeLearnArrayAdapter); 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.searchfor: 
      hp = handphone.getText().toString(); 
      new AttemptLogin(new MyHandler()).execute(hp); 
      break; 
    } 

} 

class AttemptLogin extends AsyncTask<String, String, JSONObject> { 

    private ProgressDialog pDialog; 
    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 
    private static final String LOGIN_URL = "address_url"; 
    MyHandler myHandler; 

    public AttemptLogin(MyHandler myHandler) { 
     this.myHandler = myHandler; 
    } 

    @Override 
    protected JSONObject doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     // here Check for success tag 
     try { 
      HashMap<String, String> params = new HashMap<>(); 
      params.put("hp", args[0]); 
      JSONObject json = jsonParser.makeHttpRequest(
        LOGIN_URL, "POST", params); 

      if (json != null) { 
       Log.d("JSON result", json.toString()); 

       return json; 
      } 

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

     return null; 
    } 

    protected void onPostExecute(JSONObject json) { 

     if (json != null) { 
      Toast.makeText(FindFriends.this, json.toString(), 
        Toast.LENGTH_LONG).show(); 

      try { 
       name = json.getString("name"); 
       Message message = new Message(); 
       message.obj = name; 
       myHandler.sendMessage(message); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

private class MyHandler extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     String response = String.valueOf(msg); 
     values.add(response); 
     codeLearnArrayAdapter.notifyDataSetChanged(); 
    } 
} 
} 

讓我知道,如果它的工作原理。 ..

+0

沒有。我的整個應用程序崩潰,當我試圖進入頁面 –

0

你錯過了adapter.notifyDataSetChanged();

使用此方法的數據改變

後UPD

試試這個代碼:

protected void onPostExecute(JSONObject json) { 

    if (json != null) { 
     Toast.makeText(FindFriends.this, json.toString(), 
       Toast.LENGTH_LONG).show(); 

     try { 
      name = json.getString("name"); 
      values[0]=name; 
      codeLearnArrayAdapter.notifyDataSetChanged(); 

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

希望工程。否則讓我知道它。

P.S.你也應該使用List而不是數組。

+0

name = json.getString(「name」); codeLearnArrayAdapter.notifyDataSetChanged();試圖放在這裏,但它不起作用。有任何建議嗎? –

+0

您的列表視圖顯示存儲在'values'數組中的數據。當數據改變時,你應該將它保存到'values'中並調用'codeLearnArrayAdapter.notifyDataSetChanged();讓適配器瞭解數據更改。 – hornet2319

+0

@QingYong回答更新 – hornet2319