如何在asynctask中从mysql制作可点击的Listview数据

时间:2014-05-13 07:21:52

标签: android listview android-listview android-asynctask

我正在开发一个简单的应用程序,它通过AsyncTask从MySQL数据库中检索数据。

我已经完成了在listview中检索数据,但仍无法使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

conn包含一个带有URL的方法。我通过将其对象命名为listView来调用它。

我有两个布局,第一个包含list,ID为textviews,另一个包含tvName,其中我将使用Id {{1}放置数据}和tvLocation

以下是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>

你能不能给我一些代码样本或只是如何解决它的伪代码。 感谢。

1 个答案:

答案 0 :(得分:0)

在onPostExecute方法中创建listview不是最好的方法,但如果你真的需要这种行为,请尝试runOnUI()线程在适当的应用程序线程中添加事件监听器。问候