listadapter上此行的多个标记

时间:2013-07-13 02:14:13

标签: android listview

它说多行标记在这一行。我确实在数据适配器端出错了如何解决这个问题.. 它在这一行说多个标记。我确实在数据适配器端出错了如何解决这个问题。

DataAdapter.java

package com.phpserver;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class send1 extends ListActivity {
int ct_id;
String[] ct_name = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.main);
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2:8086/city.php");
        // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line = "0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    // paring data
    JSONArray jArray;
    try {
        jArray = new JSONArray(result);
        JSONObject json_data = null;
        ct_name = new String[jArray.length()];
        for (int i = 0; i < jArray.length(); i++) {
            json_data = jArray.getJSONObject(i);
            ct_id = json_data.getInt("CITY_ID");
            ct_name[i] = json_data.getString("CITY_NAME");
        }
    } catch (JSONException e1) {
        Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG)
                .show();
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    setListAdapter(new ArrayAdapter<string>(this,
            android.R.layout.simple_list_item_1, ct_name));
    ListView lv;
    lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView                 int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
                    ct_name[position] + " wasClicked", Toast.LENGTH_SHORT)
                    .show();
    }


    });
}
}</string>

1 个答案:

答案 0 :(得分:0)

好的。抱歉,我的工具会自动将您的代码重新格式化为我的风格,但这是我认为您需要的:

public class send1 extends ListActivity
{
    int      ct_id;
    String[] ct_name = null;

    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        String result = null;
        InputStream is = null;
        StringBuilder sb = null;

        // http post
        try
        {
            final HttpClient httpclient = new DefaultHttpClient();
            final HttpPost httppost = new HttpPost("http://10.0.2.2:8086/city.php");
            // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            final HttpResponse response = httpclient.execute(httppost);
            final HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        catch (final Exception e)
        {
            Log.e("log_tag", "Error in http connection" + e.toString());
        }

        // convert response to string
        try
        {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        }
        catch (final Exception e)
        {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        // paring data
        JSONArray jArray;
        try
        {
            jArray = new JSONArray(result);
            JSONObject json_data = null;
            this.ct_name = new String[jArray.length()];
            for (int i = 0; i < jArray.length(); i++)
            {
                json_data = jArray.getJSONObject(i);
                this.ct_id = json_data.getInt("CITY_ID");
                this.ct_name[i] = json_data.getString("CITY_NAME");
            }
        }
        catch (final JSONException e1)
        {
            Toast.makeText(this.getBaseContext(), "No City Found", Toast.LENGTH_LONG).show();
        }
        catch (final ParseException e1)
        {
            e1.printStackTrace();
        }
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.ct_name));
        ListView lv;
        lv = this.getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(final AdapterView<?> adapter, final View view, final int position, final long id)
            {
                // When clicked, show a toast with the TextView text
                Toast.makeText(send1.this.getApplicationContext(), send1.this.ct_name[position] + " wasClicked",
                                Toast.LENGTH_SHORT).show();
            }
        });
    }
}