ListView在垂直滚动时隐藏图片

时间:2015-11-08 07:39:12

标签: android arrays json listview

我是第一个实施Android应用程序,我的应用程序与汽车有关,在公司选择活动中我有ListView的问题。 在第一个是这样的: at the first that is like this :

但滚动时显示我的默认图标而不是徽标 enter image description here

我怎么能解决这个问题? 在这段代码中,我从本地json数组中获取数据并发送到自定义列表以进行表格显示。以下代码是我的viewallcompnay活动:

package ir.database;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

public class ViewAllCompany extends Activity {
String url = "http://192.168.1.206/androhp/companylist.php";

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

    setContentView(R.layout.view_all_company);
    LoadAllCompany lap = new LoadAllCompany();
    lap.execute(url);
}

private class LoadAllCompany extends AsyncTask<String, String, String> {
    private ArrayList<String> urlData;
    private ListView list;

    private InputStream getStreamFromURL(String urlString, String method) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection huc = (HttpURLConnection) url
                    .openConnection();
            huc.setRequestMethod(method);

            huc.connect();

            return huc.getInputStream();
        } catch (Exception e) {
            return null;
        }

    }

    private String streamToString(InputStream is) {
        String result = "";
        String line = null;

        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                result = line + "\n";
            }
        } catch (Exception e) {
        }

        return result;
    }

    protected String doInBackground(String... args) {
        InputStream jsonStream = getStreamFromURL(args[0], "GET");
        String jsonString = streamToString(jsonStream);
        urlData = parseJSON(jsonString);
        return null;
    }

    private ArrayList<String> parseJSON(String JSONString) {
        try {
            ArrayList<String> result = new ArrayList<String>();
            JSONObject jo = new JSONObject(JSONString);
            JSONArray allpersons = jo.getJSONArray("companylist");
            for (int i = 0; i < allpersons.length(); i++) {
                JSONObject object = allpersons.getJSONObject(i);
                String objString = "";
                objString = object.getString("name") + ","
                        + object.getString("picture") + ","
                        + object.getInt("id");
                result.add(objString);
            }
            return result;

        } catch (JSONException e) {
            return null;
        }
    }

    protected void onPostExecute(String file_url) {
        String[] companyname = new String[urlData.size()];
        String[] imageUrl = new String[urlData.size()];
        String[] id = new String[urlData.size()];
        int i = 0;
        for (String s : urlData) {
            String[] split = s.split(",");
            id[i] = split[2];
            companyname[i] = split[0];
            imageUrl[i] = "http://lgapps.karnotech.ir/takhtegaz/images/companylogo/"
                    + split[2] + "/" + split[1];
            i++;
        }

        list = (ListView) findViewById(R.id.list);
        CustomList adapter = new CustomList(ViewAllCompany.this,
                companyname, imageUrl, id);
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}
}

viewallcompany xml layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".View_all_company" >

<ListView
    android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:scrollingCache="true" >

</ListView>

,此代码是我的自定义列表:

package ir.database;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] companyname;
private final String[] imageUrl;
private final String[] id;
private ImageLoader imgLoader;

public CustomList(Activity context, String[] companyname,
        String[] imageUrl, String[] id) {
    super(context, R.layout.single_list, companyname);
    this.context = context;
    this.companyname = companyname;
    this.id = id;
    this.imageUrl = imageUrl;

}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.single_list, null, true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

    txtTitle.setId(Integer.parseInt(this.id[position]));

    txtTitle.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(context, Cars.class);
            intent.putExtra("carId", v.getId());
            context.startActivity(intent);
        }
    });

    txtTitle.setText(companyname[position]);

    ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

    imgLoader = new ImageLoader(context);
    imgLoader.DisplayImage(imageUrl[position], imageView);

    return rowView;
}

}

customlist xml layout:

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow android:gravity="right">

<ImageView
    android:id="@+id/img"
    android:layout_width="30dp"
    android:layout_height="30dp" />
<TextView
    android:id="@+id/txt"
    android:layout_width="90dp"
    android:layout_height="30dp"
    android:clickable="true"
    android:onClick="onClick"
     />
</TableRow>

1 个答案:

答案 0 :(得分:0)

我认为getView方法的实现可以通过一些变化来优化; 您必须使用setTaggetTag来避免重复查看视图。例如:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder mHolder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_menu, null);
        mHolder = new ViewHolder();

        mHolder.mText=(TextView) convertView.findViewById(R.id.appName);
        mHolder.mImage=(ImageView) convertView.findViewById(R.id.appIcon);
        mHolder.mCheckBox=(CheckBox) convertView.findViewById(R.id.mCheckbox);

        convertView.setTag(mHolder);

    } else {
        mHolder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

private class ViewHolder {
    private TextView mText;
    private ImageView mImage;
    private CheckBox mCheckBox;

}

当然我建议你也看看这个答案。

https://stackoverflow.com/a/15415784/2101822

https://stackoverflow.com/a/19019905/2101822