Baseadapter的getView(),显示无关的信息 [英] Baseadapter's getView() showing irrelevant information

查看:174
本文介绍了Baseadapter的getView(),显示无关的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个GridView的。 (我给的名字)

I am having two gridviews. (names that i gave)


  • 顶的GridView:一些数字,如123456123456

  • 底部的GridView:含有数字0 - 9

最初在GridView的所有项目将是空的(像无形的)。根据在底部的GridView项单击顶部网格视图中的项目将被显示。

Initially all the items in the gridview will be empty(something like invisible). Based on click of item in bottom gridview the items in the top grid view will be shown.

例如:假设在上面的GridView的项目是123456123456。最初,所有都看不见。
有一次,我在底部网格视图中点击一个项目,我需要做出相应的号码顶格视图中可见。
比方说,我点击1.网格视图项目在那里我有1应该是可见的。

Ex: suppose the items in top gridview be "123456123456". Initially all are invisible. Once I click on an item in the bottom grid view, i need to make the appropriate numbers visible in the top grid view. Lets say, i clicked 1. The grid view items where I am having "1" should be visible.

我试过这个逻辑,但出事了,我结束了一些奇怪的结果。

I tried this logic but something went wrong and i am ended up with some wierd results.

查看输出:

如果您看到输入上图中123456123456时,点击所有1的是转向可见。当我点击2,2的得到可见,甚至额外1的被分配给电网。
我唐诺为什么会这样。

If you see the above image for input "123456123456" when 1 is clicked all 1's are turned to visible. when i clicked 2, 2's got visible and even extra 1's are assigned to the grid. I donno why this happened.

这是code,我试图

主要活动:

public class MainActivity extends Activity {
GridView grid_topNumbers;
GridView gridview_belownumbers;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gridview_belownumbers = (GridView)findViewById(R.id.gridview_belownumbers);
    grid_topNumbers = (GridView)findViewById(R.id.gridview_topnumbers);

    TopGrid topGridAdapter = new TopGrid(this);

    Constants.topNumbers = "123456123456";


    List<TopNumbersObject> topNumList = new ArrayList<TopNumbersObject>();
    TopNumbersObject topnumObj = null;

    for(int i=0;i< Constants.topNumbers.length();i++){
        topnumObj = new TopNumbersObject();
    if(i<Constants.topNumbers.length()-1)
    {
        topnumObj.number = (Constants.topNumbers.substring(i, i+1))+"";
    }
    else
    {
        topnumObj.number = (Constants.topNumbers.substring(i))+"";
    }
    topnumObj.isVisited =false;
    topNumList.add(topnumObj);
    }

    Constants.TopNumbersList = topNumList;

    grid_topNumbers.setAdapter(topGridAdapter);
    gridview_belownumbers.setAdapter(new CustomGridViewAdapter(this,topGridAdapter));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}
}

我的屁股网格视图适配器:

My Bottom Grid view adapter :

public class CustomGridViewAdapter extends BaseAdapter {

Context cont;
LinearLayout.LayoutParams params = null;
TopGrid topAdapter = null;

public CustomGridViewAdapter(Context c,TopGrid topAdapter) {
    params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    cont = c;
    this.topAdapter = topAdapter;

    // type = Typeface.createFromAsset(cont.getAssets(),
    // "fonts/letters_fonttype.ttf");
}

@Override
public int getCount() {

    return 10;
}

@Override
public Object getItem(int arg0) {

    return arg0;
}

@Override
public long getItemId(int arg0) {

    return arg0;
}

@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {

    View v;
    TextView tv;

     // if it’s not recycled, initialize some
                                // attributes
        LayoutInflater li = (LayoutInflater) cont
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.bottom_grid, null);

        tv = (TextView) v.findViewById(R.id.bottom_grid_item_textView1);
        tv.setText(arg0+"");

        tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                TextView clickedTextView = (TextView)arg0;
                String letter = clickedTextView.getText().toString();

                for(int i=0;i<Constants.topNumbers.length();i++)
                {
                    TopNumbersObject letterObject = Constants.TopNumbersList.get(i);
                     if(letterObject.number.equalsIgnoreCase(letter))
                    {
                         letterObject.isVisited = true;
                        if(Constants.topNumbers.toLowerCase().lastIndexOf(letter.toLowerCase()) == i)
                        {
                            topAdapter.notifyDataSetChanged();
                            break;
                        }
                    }


                }

                clickedTextView.setClickable(false);
                clickedTextView.setOnClickListener(null);
            }

        });


    return v;

}
}

我顶格适配器:

public class TopGrid extends BaseAdapter {

Context cont;
LinearLayout.LayoutParams params = null;

public TopGrid(Context c) {
    params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    cont = c;

}

@Override
public int getCount() {

    return Constants.TopNumbersList.size();
}

@Override
public Object getItem(int arg0) {

    return arg0;
}

@Override
public long getItemId(int arg0) {

    return arg0;
}

@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {

    View v =convertView;
    TextView tv;
    TopNumbersObject topnoObject = (Constants.TopNumbersList.get(arg0));

    Holder h;
    if (v == null) // if it’s not recycled, initialize some  attributes
    { 
        h=new Holder();
        LayoutInflater li = (LayoutInflater) cont
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.top_numbers_item, null);

        h.tv = (TextView) v.findViewById(R.id.top_grid_item_textView1);
        v.setTag(h);
    } 
    else 
    {
        h=(Holder)v.getTag();
    }
    if(topnoObject.isVisited)
    {
        h.tv.setText(topnoObject.number);
    }
    Log.e("letters", topnoObject.number+ "  "+topnoObject.isVisited+"  "+arg0);
    return v;

}
private static class Holder
{
    TextView tv;
}
}

常量文件:

public class Constants {
public static boolean ISGRE;
public static String topNumbers = "1234567890";
public static List<TopNumbersObject> TopNumbersList = null;
public static int chances = 8;
}

接下来我的XML文件:

Next My xml files :

activity_main:

activity_main :

<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:background="#000000" >

<GridView
    android:id="@+id/gridview_topnumbers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:numColumns="12"
    android:stretchMode="columnWidth" >
</GridView>



<GridView
    android:id="@+id/gridview_belownumbers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/gridview_topnumbers"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:numColumns="7"
    android:stretchMode="columnWidth" >
</GridView>

</RelativeLayout>

top_numbers_item.xml

top_numbers_item.xml

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

<TextView
    android:id="@+id/top_grid_item_textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textColor="#FF00FF" />

</LinearLayout>

bottom_grid.xml

bottom_grid.xml

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

<TextView
    android:id="@+id/bottom_grid_item_textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textColor="#FF0011"
    android:text="A" />
</LinearLayout>

任何人都可以建议我哪里出了问题。

Can anyone suggest me where i went wrong.

推荐答案

设法改变topgrid项目的知名度,当我需要的。

Managed to change visibility of topgrid items when i need.

取而代之的是

if(topnoObject.isVisited)
{
     h.tv.setText(topnoObject.number);
}

用于以下code。

if(topnoObject.isVisited)
{
    h.tv.setVisibility(View.VISIBLE);
}
else
{
    h.tv.setVisibility(View.INVISIBLE); 
}
h.tv.setText(topnoObject.number);

但仍在等待更准确的答案

But still waiting for a more accurate answer

这篇关于Baseadapter的getView(),显示无关的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆