我有兩個gridviews。 (名字是我給)Baseadapter的getView()顯示不相關的信息
- 熱門的GridView:有的像 「123456123456」
- 底部的GridView號:含有數字0 - 9
最初在GridView的所有項目將是空的(像不可見的東西)。根據底部網格視圖中的項目點擊,將顯示頂部網格視圖中的項目。例如:
例如:假設top gridview中的項是「123456123456」。最初都是看不見的。 一旦我點擊底部網格視圖中的項目,我需要在頂部網格視圖中顯示適當的數字。 可以說,我點擊了1.網格視圖項目,我有「1」應該是可見的。
我試過這個邏輯,但出了點問題,最後我得到了一些奇怪的結果。
查看輸出:
如果您看到輸入上面的圖像「123456123456」,當被點擊1全1都變成可見的。當我點擊2,2的可見,甚至額外的1被分配給網格。 我不知道爲什麼會發生這種情況。
這是我試過的代碼。
主要活動:
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;
}
}
我的底部網格視圖適配器:
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文件:
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>
任何人都可以建議我哪裏出了錯。