2014-05-13 38 views
0

我正在開發一個簡單的應用程序,它通過AsyncTask從MySQL數據庫中檢索數據。如何在asynctask中使用mysql中的可點擊列表視圖數據

我已經完成了在列表視圖中檢索數據,但仍然無法使listview可點擊。我是新使用AsyncTask,使我不知道如何使listview可點擊。我試圖給OnItemClickListener()ListView裏面的onPostExecute()方法,但仍然沒有結果。 我在這裏搜索了一個相關的主題,但沒有找到任何。

在這裏,我表明我已經寫代碼:

public class MyClass extends ListActivity { 

protected TextView title; 
protected ImageView icon; 

static String the_name = "name"; 
static String the_location = "location"; 
JSONArray str_json = null; 
public String url; 

ListView listData; 

private ProgressDialog pDialog; 
ArrayList<HashMap<String, String>> data_map = new ArrayList<HashMap<String, String>>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.alldata); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
      R.layout.window_header); 
    title = (TextView) findViewById(R.id.title); 
    icon = (ImageView) findViewById(R.id.icon); 

    this.title.setText("All Data"); 
    this.icon.setImageResource(R.drawable.alldata); 

    Connection conn = new Connection(); 
    url = conn.connectData(); 
    new GetMyData().execute(); 

} 

class GetMyData extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MyData.this); 
     pDialog.setMessage("Loading Data" + "\r\n" + "Please Wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    protected String doInBackground(String... args) { 

     String link_url = url + "serverandroid2.php"; 
     JSONParser jParser = new JSONParser(); 
     JSONObject json = jParser.getJson(link_url); 

     try { 
      str_json = json.getJSONArray("mydata"); 

      for (int i = 0; i < str_json.length(); i++) { 
       JSONObject ar = str_json.getJSONObject(i); 

       HashMap<String, String> map = new HashMap<String, String>(); 

       String name= ar.getString("name"); 
       String location= ar.getString("region") + " | " 
         + ar.getString("city"); 

       map.put(the_name, name); 
       map.put(the_location, location); 

       data_map.add(map); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     }   
     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     pDialog.dismiss();   
     runOnUiThread(new Runnable() { 
      public void run() { 
       ListAdapter adapter = new SimpleAdapter(MyClass.this, data_map, R.layout.list_row, new String[] { 
           the_name, the_location }, new int[] { 
           R.id.tvname, 
           R.id.tvlocation }); 
       setListAdapter(adapter); 
      } 

     }); 
     listData = (ListView)findViewById(R.id.list); 
     listData.setClickable(true); 

     listData.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, 
        View viewRow, int position, long id) { 

       try{ 
        Intent viewDetail = null; 
        JSONObject jsonChildExtra = str_json.getJSONObject(position); 
        String nameEx = jsonChildExtra 
          .optString("name"); 
        String locationEx = jsonChildExtra 
          .optString("location"); 
        String regionEx = jsonChildExtra 
          .optString("region"); 

        viewDetail = new Intent(MyClass.this, 
          NextClass.class); 
        viewDetail.putExtra("name", 
          nameEx); 
        viewDetail.putExtra("location", 
          locationEx); 
        viewDetail.putExtra("region", regionEx); 
        startActivity(viewDetail); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 


      } 
     }); 


    } 
} 
} 

Connection那裏包含它裏面的URL的方法。我把它叫做conn

我有兩個佈局在那裏,第一個包含了listView ID爲list,而另一個包含textviews,我就會把數據與編號tvNametvLocation

下面是ListView我的佈局XML代碼:

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

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" > 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="15dip" 
      android:layout_marginRight="15dip" 
      android:layout_marginTop="15dip" 
      android:background="@drawable/labeldetail_background" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:paddingBottom="10dip" 
       android:paddingTop="10dip" 
       android:text="All Data" 
       android:textColor="#000000" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="15dip" 
      android:layout_marginRight="15dip" 
      android:layout_marginBottom="15dip" 
      android:layout_marginTop="10dip" > 

      <ListView 
       android:id="@android:id/list" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@drawable/labeldetail_background" > 
      </ListView> 
     </TableRow> 
    </TableLayout> 
</ScrollView> 

,這裏是爲TextView XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/bg_list" 
android:orientation="horizontal"> 
<LinearLayout   
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/tvName" 
     android:text="Data Not Found" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#000" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:paddingBottom="2dip" /> 

    <TextView 
     android:id="@+id/tvLocation" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textColor="#000" 
     android:textSize="10sp" /> 
</LinearLayout> 

可否請你遞給我的代碼示例或者只是如何解決它的僞代碼。 謝謝。

+0

這不是一個好主意,因爲它們都是可滾動的在滾動視圖中有一個ListView ...不確定是否是這種情況,但它可能是你的問題之一,因爲觸摸可能不會到組件你要。 – Mikel

+0

您也可以在onCreate的MyClass中使用onPostExecute方法中的所有代碼。在onPostExecute中,您只需將數據設置爲列表適配器並調用listAdapter.notifyDataSetChanged()。 – Mikel

+0

@Mikel O是的,我剛剛注意到它。滾動視圖在我的xml文件中標記爲黃色。但我不認爲這是這種情況下的問題。 我試過了,我把這個代碼(onItemClickListener和下面)放在onCreate方法裏面,但是也沒有顯示任何東西。我會再試一次。謝謝 – FathulAzis

回答

0

在onPostExecute方法內部創建listviews並不是實現它的最好方法,但是如果您真的需要這種行爲,請嘗試使用runOnUI()線程在正確的應用程序線程中添加事件偵聽器。問候

+0

我會盡力去探索它。無論如何,有沒有其他的方式來調用並給listview一個onClickItemListener()旁邊呢?如果有的話,我很樂意看到一個建議 – FathulAzis

相關問題