Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按内容设置listview行的可见性_Android_Json_Listview_Separator_Simpleadapter - Fatal编程技术网

Android 按内容设置listview行的可见性

Android 按内容设置listview行的可见性,android,json,listview,separator,simpleadapter,Android,Json,Listview,Separator,Simpleadapter,我是一个业余的Andriod程序员,我的应用程序在listview中显示一个JSON字符串。这是JSON对象: {"datos":[{"TITULO":"PRUEBA1","NOMBRELINEA":"CORTE CABALLERO","MONTO":"12.84"}, {"TITULO":"PRUEBA1","NOMBRELINEA":"SECADO","MONTO":"8.56"}, {"TITULO":"PRUEBA1","NOMBRELINEA":"PEINADO

我是一个业余的Andriod程序员,我的应用程序在listview中显示一个JSON字符串。这是JSON对象:

    {"datos":[{"TITULO":"PRUEBA1","NOMBRELINEA":"CORTE CABALLERO","MONTO":"12.84"},
    {"TITULO":"PRUEBA1","NOMBRELINEA":"SECADO","MONTO":"8.56"},
    {"TITULO":"PRUEBA1","NOMBRELINEA":"PEINADO CABALLERO","MONTO":"0"},
    {"TITULO":"PRUEBA2","NOMBRELINEA":"LAVADO KERAS","MONTO":"7.49"},
    {"TITULO":"PRUEBA2","NOMBRELINEA":"MASCARA KERAS","MONTO":"10.70"},
    {"TITULO":"PRUEBA3","NOMBRELINEA":"MECHAS CORTO","MONTO":"6.42"},
    {"TITULO":"PRUEBA3","NOMBRELINEA":"MECHAS LARGO","MONTO":"10"}]}
我使用一个简单的适配器和一个JSON解析器将JSON放入listview,结果可以在下图中看到:

但是,我希望它看起来像这样:

我喜欢Cyril Mottier关于使用View类的visibility属性并设置重复时消失的行的建议,但他建议的代码用于静态数据:

以下是JSON解析器的代码:

    public class JSONParser {
    /** Recibe un JSONobject y retorna una lista */
    public List<HashMap<String,String>> parse(JSONObject jObject){

    JSONArray jDatos = null;
    try {
        /** Recupera todos los elementos en el arreglo 'datos' */
        jDatos = jObject.getJSONArray("datos");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    /** Invoking getCountries with the array of json object
    * where each json object represent a country
    */
    return getDatos(jDatos);
    }

    private List<HashMap<String, String>> getDatos(JSONArray jDatos){
    int datoCount = jDatos.length();
    List<HashMap<String, String>> datoList = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> dato = null;

    /** Taking each country, parses and adds to list object */
    for(int i=0; i<datoCount;i++){
        try {
            /** Call getCountry with country JSON object to parse the country */
            dato = getDato((JSONObject)jDatos.get(i));
            datoList.add(dato);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return datoList;
    }

    /** Parsing the JSON object */
    private HashMap<String, String> getDato(JSONObject jDato){

    HashMap<String, String> dato = new HashMap<String, String>();
    String nombre = "";
    String precio="";
    String tipo="";

    try {
        tipo = jDato.getString("TITULO");
        nombre = jDato.getString("NOMBRELINEA");
        precio = jDato.getString("MONTO");

        dato.put("TITULO", tipo);
        dato.put("NOMBRELINEA", nombre);
        dato.put("MONTO", precio);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return dato;
    }
    }
公共类JSONParser{
/**接收目标并重新发送列表*/
公共列表解析(JSONObject jObject){
JSONArray jDatos=null;
试一试{
/**在“达托斯”的阿雷格洛岛上重现*/
jDatos=jObject.getJSONArray(“datos”);
}捕获(JSONException e){
e、 printStackTrace();
}
/**使用json对象数组调用getCountries
*其中每个json对象代表一个国家
*/
返回getDatos(jDatos);
}
私有列表getDatos(JSONArray jDatos){
int datoCount=jDatos.length();
List datoList=new ArrayList();
HashMap dato=null;
/**获取每个国家,解析并添加到列表对象*/
对于(int i=0;i 9){
StrictMode.ThreadPolicy策略=新建
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
}
}
私有字符串xhttpost(字符串targetURL、字符串变量、字符串值){
字符串url参数=”;
网址;
HttpURLConnection=null;
试一试{
//邮电参数
urlParameters=Variable+“=”+URLEncoder.encode(Valor,“UTF-8”);
//Crear conexion
url=新url(targetURL);
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
connection.setRequestProperty(“Content Length”,“Integer.toString(urlParameters.getBytes().Length));
connection.setRequestProperty(“内容语言”、“en-US”);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(真);
//发送请求
DataOutputStream wr=新的DataOutputStream(connection.getOutputStream());
writeBytes(urlParameters);
wr.flush();
wr.close();
//得到回应
BufferedReader rd=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
StringBuilder sb=新的StringBuilder();
弦线;
而((line=rd.readLine())!=null){
某人附加(行);
}
rd.close();
连接断开();
使某人返回字符串();
}捕获(格式错误){
//TODO自动生成的捕捉块
返回e.toString();
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
返回e.toString();
}捕获(协议例外e){
//TODO自动生成的捕捉块
返回e.toString();
}捕获(IOE异常){
//TODO自动生成的捕捉块
返回e.toString();
}
}
public void onStart(){
super.onStart();
String Consulta=“选择lt.nombre作为titulo,选择LC.nombrecombo作为NombreLinea,(选择Sum(Precioventa1)作为productos中的Precio,其中Codigo in(从listacombodetalle LCD中选择IdProducto,其中LCD.idcombo=LC.Id))作为LISTATITULO lt中的Monto,LISTACOMBO LC其中lt.Id=LC.idtitulo”;
字符串Z=xhttpost(“http://192.168.1.2/android/api/ConsultarFbLP","查询","咨询",;
字符串Q=“{”+“\”datos\:“+Z+”}”;
Toast.makeText(this,Q,Toast.LENGTH_LONG).show();
ListViewLoaderTask ListViewLoaderTask=新建ListViewLoaderTask();
listViewLoaderTask.execute(Q);
}
公共类ListViewLoaderTask扩展异步任务{
JSONObject jObject;
/**在非ui线程中解析xml数据*/
@凌驾
受保护的SimpleAdapter doInBackground(字符串…Q){
试一试{
jObject=新的JSONObject(Q[0]);
JSONParser JSONParser=新的JSONParser();
parse(jObject);
}捕获(例外e){
d(“JSON例外1”,例如toString());
}
JSONParser JSONParser=新的JSONParser();
列表datos=null;
试一试{
/**将解析后的数据作为列表构造获取*/
datos=jsonParser.parse(jObject);
}捕获(例外e){
Log.d(“异常”,例如toString());
}
/**Hashmap中使用的键*/
字符串[]from={“TITULO”、“NOMBRELINEA”、“MONTO”};
/**listview\u布局中的视图ID*/
int[]to={R.id.textViewT,R.id.textViewR,R.id.textViewP};
/**实例化适配器以存储每个项
*R.layout.listview\u布局定义每个项目的布局
*/
simpledapter adapter=新的simpledapter(getBaseContext(),datos,R.layout.lv_项,from,to);
返回适配器;
}
/**由Android系统在“doInBackground”上调用,完全执行*/
/**这将在ui线程中执行*/
@凌驾
受保护的void onPostExecute(SimpleAdapter适配器){
/**获取对main.xml布局文件的listview的引用*/
ListView ListView=(ListView)findViewById(R.id.lv_datos);
/**将包含国家/地区列表的适配器设置为listview*/
setAdapter(适配器);
}
}
}
这是填充列表视图的XML

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

    <TextView
    android:id="@+id/textViewT"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_weight=".6"
    android:background="#f00"
    android:gravity="left"
    android:text="Tipo de servicio"
    android:textColor="#fff"
    android:textSize="60sp" 
    android:visibility="visible"/>

    <TextView
    android:id="@+id/textViewR"
    android:layout_width="500dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textViewT"
    android:text="Renglon"
    android:textColor="#000"
    android:textSize="40sp" />

    <TextView
    android:id="@+id/textViewP"
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/textViewR"
    android:layout_alignBottom="@+id/textViewR"
    android:layout_toRightOf="@+id/textViewR"
    android:background="#BABABF"
    android:gravity="center"
    android:text="Precio"
    android:textColor="#000"
    android:textSize="40sp" />


    </RelativeLayout>

有没有关于如何隐藏重复行的建议?对不起,我是新来的


提前谢谢

您应该创建自己的自定义适配器,从BaseAdapter或ArrayAdapter继承,甚至可能很简单
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
    android:id="@+id/textViewT"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_weight=".6"
    android:background="#f00"
    android:gravity="left"
    android:text="Tipo de servicio"
    android:textColor="#fff"
    android:textSize="60sp" 
    android:visibility="visible"/>

    <TextView
    android:id="@+id/textViewR"
    android:layout_width="500dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textViewT"
    android:text="Renglon"
    android:textColor="#000"
    android:textSize="40sp" />

    <TextView
    android:id="@+id/textViewP"
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/textViewR"
    android:layout_alignBottom="@+id/textViewR"
    android:layout_toRightOf="@+id/textViewR"
    android:background="#BABABF"
    android:gravity="center"
    android:text="Precio"
    android:textColor="#000"
    android:textSize="40sp" />


    </RelativeLayout>