Baseadapter's getView() showing irrelevant inf

2019-08-20 18:43发布

问题:

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

  • Top gridview : some number like "123456123456"
  • bottom gridview : contains numbers from 0 - 9

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.

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.

Look at the output :

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.

This is the code that I tried.

Main Activity :

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;

}
}

My Top Grid Adapter :

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;
}
}

The constants file :

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

Next My xml files :

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

<?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

<?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.

回答1:

Managed to change visibility of topgrid items when i need.

Instead of this

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

Used the below 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