gpt4 book ai didi

java - 我无法在 Android Fragment 中使用 textview 填充 gridview

转载 作者:行者123 更新时间:2023-12-01 13:00:40 25 4
gpt4 key购买 nike

我无法使用 fragment 填充 gridview 内的 imageview TextView 。

它显示空白而不是我的项目中填充的 gridview 可以请任何人看到下面的代码并让我知道我必须更改什么

图像和文本的填充是动态地从 mysql 数据库

public class StoreHomeFragment extends Fragment {
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.store_home, container, false);
final GridView gridView1 = (GridView)rootView.findViewById(R.id.store_home_gridview);
gridView1.setAdapter(new ImageAdapter(rootView.getContext(), MyArrList));
return rootView;
}

//Activity 已创建

@Override

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

String url = "http://192.168.1.132/Android/App/good.php"; //url where i am using select query and retrieving it from database

try {
JSONArray data = new JSONArray(getJSONUrl(url));

HashMap<String, String> map;

for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);//retrieving from db
map = new HashMap<String, String>();
map.put("name", c.getString("name"));
map.put("artist", c.getString("artist"));
map.put("price", c.getString("price"));
map.put("image", c.getString("image"));


MyArrList.add(map);
}

//gridView1.setAdapter(new ImageAdapter(this,MyArrList));

} catch (JSONException e) {

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

e.printStackTrace();
}

}

//我已经使用了imageAdapter

class ImageAdapter extends BaseAdapter 

{
private Context context;
public ImageView imageView;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();

public ImageAdapter(Context c,ArrayList<HashMap<String, String>> list)
{
context = c;

MyArr = list;
}

public int getCount() {

return MyArr.size();
}

public Object getItem(int position) {

return position;
}

public long getItemId(int position) {

return position;
}
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
convertView = inflater.inflate(R.layout.store_home_gridview_row, null);
}

TextView tv_title = (TextView) convertView.findViewById(R.id.textview_name);
tv_title.setText("Title:"+MyArr.get(position).get("name"));

TextView tv_artist = (TextView) convertView.findViewById(R.id.textview_artist);
tv_artist.setText("Artist:"+MyArr.get(position).get("artist"));

TextView tv_duration = (TextView) convertView.findViewById(R.id.textview_price);
tv_duration.setText("Price:"+MyArr.get(position).get("price"));

String abc = (MyArr.get(position).get("image"));
String abcd = "http://192.168.1.132/images/products/"+abc;

imageView = (ImageView) convertView.findViewById(R.id.imageView1);
try
{
URL url3 = null;
try {
url3 = new URL(abcd);
} catch (Exception e) {

e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(url3.openConnection().getInputStream()); //image is populated
imageView.setImageBitmap(bmp);
}
catch(Exception par)
{
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
return convertView;
}

}



/*** Get JSON Code from URL ***/
public String getJSONUrl(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println ( "status Code : " + statusCode );
if (statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
str.append(line);
}
}
else
{
Log.e("Log", "Failed to download file..");
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println ( "str : " + str.toString() );
return str.toString();
}


}

最佳答案

按照步骤操作希望这对您有帮助

1) 从 onCreateView() 方法中删除这些行

final GridView gridView1 = (GridView)rootView.findViewById(R.id.store_home_gridview); 
gridView1.setAdapter(new ImageAdapter(rootView.getContext(), MyArrList));

2)修改onActivityCreated()如下

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new AsyncTask<Void, Void, Void>() {

@Override
protected Void doInBackground(Void... params) {
String url = "http://192.168.1.132/Android/App/good.php";

try {
JSONArray data = new JSONArray(getJSONUrl(url));

HashMap<String, String> map;

for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);

map = new HashMap<String, String>();
map.put("name", c.getString("name"));
map.put("artist", c.getString("artist"));
map.put("price", c.getString("price"));
map.put("image", c.getString("image"));

MyArrList.add(map);
}

} catch (JSONException e) {

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

e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
final GridView gridView1 = (GridView) rootView.findViewById(R.id.store_home_gridview);
gridView1.setAdapter(new ImageAdapter(getActivity(), MyArrList));

}
}.execute();
}

关于java - 我无法在 Android Fragment 中使用 textview 填充 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23536554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com