简体   繁体   English

Android - LazyAdapter重叠视图

[英]Android - LazyAdapter overlapping views

I am doing lazyloading to display images and text. 我正在进行延迟加载以显示图像和文本。 I have got everything up and now, trying to add a textview in the view. 我现在已经掌握了一切,尝试在视图中添加textview。 However, my adapter is overlapping my view. 但是,我的适配器与我的视图重叠。 Simpler terms is that, the listview is overlapping the textview. 更简单的术语是,listview与textview重叠。 I'm not sure where I'm doing it wrong because the original example is able to display a button just below the listview but I'm not. 我不确定我在哪里做错了,因为原始示例能够在列表视图下方显示一个按钮,但我不是。 I'm not getting any errors. 我没有收到任何错误。 My listview just covers the whole screen. 我的列表视图只涵盖了整个屏幕。 Any idea what might be the problem? 知道可能是什么问题吗?

main.java main.java

public class main extends Activity {

private static final String targetURL ="http://www.google.com/images";
ListView list;

ProgressDialog dialog;
private String[] mStrings = {};
private String[] dStrings = {};
//TextView tv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);         
    new TheTask().execute();
    list=(ListView)findViewById(R.id.list);
    TextView tv = (TextView)findViewById(R.id.tv);

    //tv.setText("Text View is displayed");
}   


protected class TheTask extends AsyncTask<Void, Void, MyResultClass >{

    protected void onPreExecute() {
        dialog = ProgressDialog.show(Cats.this, "Retrieving Information", "Please wait for few seconds...", true, false);
        dialog.setCancelable(true);
    }

    protected MyResultClass doInBackground(Void... params) {
        searchContent();
        MyResultClass result = new MyResultClass();
        result.mStrings = mStrings;
        result.dStrings = dStrings;
        return result;
    }   

    protected void onPostExecute(MyResultClass result) {            
        dStrings = result.dStrings;
        mStrings = result.mStrings;         
        LazyAdapter adapter = new LazyAdapter(Cats.this, mStrings, dStrings);
        list.setAdapter(adapter);
        dialog.dismiss();
    }       
}

    class MyResultClass
    { 
        public String[] mStrings; 
        public String[] dStrings; 
    }

public void searchContent()
{
    String imageC = "";
    String textC = "";

    try {

        URL url = new URL(targetURL);

        // Make the connection
        URLConnection conn = url.openConnection();
        BufferedReader reader = new BufferedReader(
         new InputStreamReader(conn.getInputStream()));

        String line = reader.readLine();
        Pattern sChar = Pattern.compile("&.*?;");
        line.replaceAll("\\<.*?\\>", "");
        Matcher msChar = sChar.matcher(line);
        while (msChar.find()) line = msChar.replaceAll("");

        while (line != null) {

            if(line.contains("../../"))
            {

                int startIndex = line.indexOf("../../") + 6;
                int endIndex = line.indexOf(">", startIndex + 1);
                String abc = "http://www.google.com/";
                String imageSrc = line.substring(startIndex,endIndex);
                //complete full url
                String xyz = abc +imageSrc;
                xyz = xyz.substring(0,xyz.indexOf('"'));
                xyz = xyz +";";
                xyz = xyz.replaceAll(" ", "%20");
                imageC += xyz;                  
                mStrings = imageC.split(";");
                line = reader.readLine();
            }

            if(line.contains("../../") == false)
            {
                line = reader.readLine();
            }

            if (line.contains("Gnametag"))
            {
                int startIndex = line.indexOf("Gnametag") + 10;
                int endIndex = line.indexOf("<", startIndex + 1);
                String gname = line.substring(startIndex,endIndex);
                textC = textC.replaceAll("</span>", "");
                textC += "Name: "+gname+ "\n";
            }

                if (line.contains("Age"))
                {
                    textC += "Age: "+reader.readLine() + "\n" + ";";
                    textC = textC.replaceAll("                  ", "");
                    dStrings = textC.split(";");
                }

            if (line.contains("Last Update"))
            {
                reader.close();
            }                               
        }           

        // Close the reader
        reader.close();

    } catch (Exception ex) {
        ex.printStackTrace();           
    }     

}
}

Adapter.java Adapter.java

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);;
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText(text[position]);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;
}

item.xml (used by Adapter.java) item.xml(由Adapter.java使用)

<?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="#FFFFFF">
<ImageView
  android:id="@+id/image"
  android:layout_width="90dip"
  android:layout_height="90dip" 
  android:src="@drawable/stub" 
  android:scaleType="centerCrop"
  android:layout_gravity="left|center_vertical" 
  />

<TextView
  android:id="@+id/text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:textSize="15dip"/>

</LinearLayout>

main.xml main.xml中

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">

<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing 123"
/> 

</LinearLayout>

<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"    
android:layout_below="@+id/linear1"
android:background="#FFFFFF">

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"/>
</LinearLayout>

</RelativeLayout>

Just remove the + symbol from this line 只需从此行中删除+符号即可

android:layout_below="@+id/linear1"

in second linear layout stanza. 在第二个线性布局节。

Add android:layout_alignParentTop="true" to your top LinearLayout ( linear1 ) android:layout_alignParentTop="true"到您的顶部LinearLayoutlinear1

<LinearLayout
    android:id="@+id/main_xml_linearlayout_one"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    >

    <TextView
        android:id="@+id/main_xml_textview_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

<LinearLayout
    android:id="@+id/main_xml_linearlayout_two"
    android:layout_below="@id/main_xml_linearlayout_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" >

    <ListView
        android:id="@+id/main_xml_listview_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"/>

</LinearLayout>

When you say the ListView covers the whole screen, do you refer to the result on an actual phone or eg the preview in Eclipse? 当你说ListView覆盖整个屏幕时,你是在实际手机上引用结果还是在Eclipse中预览? If it shows up correctly in the latter, then likely the issue is somewhere in your Java code. 如果它在后者中正确显示,则可能是Java代码中的某个问题。

Anyways, I don't seem to be able to reproduce your problem on either one. 无论如何,我似乎无法在任何一个上重现你的问题。 However, there are two things I would try: 但是,我会尝试两件事:

First, change the order of calls in the onCreate() method of your Activity. 首先,更改Activity的onCreate()方法中的调用顺序。 Specifically, don't execute your AsyncTask until after you've initialized the ListView, since you're using it in the onPostExecute() . 具体来说,在初始化ListView之后不要执行AsyncTask,因为您在onPostExecute()使用它。 Although this probably won't make a difference, you'd better avoid any chances on a race condition. 虽然这可能不会有所作为,但你最好避免任何竞争条件下的机会。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);         

    list=(ListView)findViewById(R.id.list);
    TextView tv = (TextView)findViewById(R.id.tv);
    // start task after 'list' is initialized.
    new TheTask().execute();
}   

Secondly, the layout file containing your ListView is unnecessarily complicated. 其次,包含ListView的布局文件不必要地复杂化。 Preferably you want to keep layouts as simple as possible and limit the nesting for a more flat hierarchy. 最好您希望保持布局尽可能简单,并限制嵌套以获得更平坦的层次结构。 You could simply get rid of the absolete LinearLayouts, and, if you prefer working with them, change the parent to one. 您可以简单地删除过时的LinearLayouts,如果您更喜欢使用它们,请将父级更改为1。 Also, android:orientation="vertical" is not a valid attribute for a RelativeLayout, so make sure you remove that. 此外, android:orientation="vertical"不是RelativeLayout的有效属性,因此请确保删除它。

Same RelativeLayout, but without the LinearLayouts: 相同的RelativeLayout,但没有LinearLayouts:

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Testing 123" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv"
        android:layout_marginBottom="30dp" />

</RelativeLayout>

Same behaviour, but with a LinearLayout as root element: 相同的行为,但使用LinearLayout作为根元素:

<?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="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Testing 123" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="30dp" />

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM