我想根据SharedPrefarance数据和位置更改GridView图像更改。 我有一个LinkActivty。在此活动中,我根据gridview位置
在sharedPrefarance中保存了一些数据public class LinkActivity extends Activity {
TextView textView;
TextView textView2;
String[] text = { "robin", "robin", "pavel", "robin", "pavel", "robin",
"pavel", "robin", "pavel", "robin", "pavel", "robin", };
private Button button;
public static final String[] Name = { "name", "pavel", "rimon", "robin",
"class", "osdsd", "sadajhjkhsd", "sadd", "sakdods", "psadkds", "sajdka",
"asdjkjsdk" };
public static final String[] list = { "na", "p", "r", "rs", "c", "o",
"asdasd", "sadadsad", "paa", "ron", "reod", "rodela" };
private EditText editText;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_link);
Bundle bundle = getIntent().getExtras();
final int link = bundle.getInt("link");
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
button = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView.setText(text[link]);
if (sharedPreferences.contains(Name[link])) {
textView2.setText(sharedPreferences.getString(Name[link], ""));
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = editText.getText().toString();
if (name.equals(text[link])) {
Toast.makeText(getBaseContext(), "Right", 200).show();
name(Name[link], list[link]);
} else {
Toast.makeText(getBaseContext(), "Wrong", 200).show();
}
}
});
}
public void name(String name, String tr) {
Editor editor = sharedPreferences.edit();
editor.putString(name, tr);
editor.commit();
}
}
我的CustomGrideView
public class CustomGridView extends BaseAdapter {
private Context context;
private final int[] imageid;
private final String[] web;
private int[] clickImage;
public CustomGridView(Context context, int[] imageid, String[] web) {
super();
this.context = context;
this.imageid = imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(context);
grid = inflater.inflate(R.layout.list_item, null);
TextView textView = (TextView) grid.findViewById(R.id.textView1);
ImageView imageView = (ImageView) grid
.findViewById(R.id.imageView1);
//ImageView imageView2 = (ImageView) grid
// .findViewById(R.id.imageView2);
textView.setText(web[position]);
imageView.setImageResource(imageid[position]);
//imageView2.setImageResource(clickImage[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
在我的MainActivity中,我想更改我的共享位置数据将保存的那个位置的gridview图像。
public class MainActivity extends Activity {
private GridView gridView;
int link;
String clickid;
SharedPreferences sharedPreferences;
public String[] web = { "google", "google", "google", "google", "google",
"google", "google", "google", "google", "google", "google",
"google"
};
public static final String[] Name = { "name", "pavel", "rimon", "robin",
"class", "osdsd", "sadajhjkhsd", "sadd", "sakdods", "psadkds",
"sajdka", "asdjkjsdk" };
public static final String[] list = { "na", "p", "r", "rs", "c", "o",
"asdasd", "sadadsad", "paa", "ron", "reod", "rodela" };
String[] text = { "robin", "robin", "pavel", "robin", "pavel", "robin",
"pavel", "robin", "pavel", "robin", "pavel", "robin", };
int[] imageId = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, };
int[] signImage = { R.drawable.correct, R.drawable.correct,
R.drawable.correct, R.drawable.correct, R.drawable.correct,
R.drawable.correct, R.drawable.correct, R.drawable.correct,
R.drawable.correct, R.drawable.correct, R.drawable.correct,
R.drawable.correct };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGridView adapterCustomGridView = new CustomGridView(
MainActivity.this, imageId, web);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
// for (int i = 0; i < 11; i++) {
// clickid = sharedPreferences.getString(Name[i], list[link]);
// }
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(adapterCustomGridView);
for (int i = 0; i < 11; i++) {
String cliclString = sharedPreferences.getString(Name[i], list[i]);
if (cliclString == text[i]) {
adapterCustomGridView = new CustomGridView(MainActivity.this,
signImage, web);
}
}
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
LinkActivity.class);
intent.putExtra("link", position);
startActivity(intent);
}
});
}
}
当我运行我的项目时,它会将clickImage显示在所有gridview图像中。 我想在特定位置显示数据保存位置的点击图像。 我认为MainActivity中的主要问题是共享性。我使用了一些共享预测的条件,我没有完全阅读和判断数据。 怎么做。
答案 0 :(得分:0)
首先,不要在getView中使用View'网格'。使用convertView:
convertview = inflater.inflate(R.layout.list_item, null);
这就是你应该在if语句中做的所有事情。所以后面的所有内容,例如获取textview和imageview,以及添加文本,都在if语句之外执行。删除'其他'。现在我想你每个位置都有1个String和1个图像资源。您不必每次都在for循环中调用适配器的构造函数。只需执行一次,使用正确的String数组和int数组。您的int数组(带有资源)将与String数组具有相同的大小。您仍然可以在Activity中使用for循环。用它来填充一个新的int []:你要传递给你的适配器的那个。如果为true,则添加signImage项,否则添加imageId项。