我在Android中有点新手并且真的想学习它但却陷入了一个教程。本教程是关于使用一些文本在gridview
下载和显示图像。到目前为止,我得到的图像,文字和一切。当我想从该gridview打开项目并显示大图像和文本时,会出现问题。我能够显示图像,但我无法弄清楚如何通过意图传递文本。
这是活动1
public class Main extends Activity {
public static final int DIALOG_DOWNLOAD_JSON_PROGRESS = 0;
private ProgressDialog mProgressDialog;
public String resultServer = "";
public String text;
ArrayList<HashMap<String, Object>> MyArrList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Download JSON File
new DownloadJSONFileAsync().execute();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_JSON_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading.....");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
// Show All Content
public void ShowAllContent()
{
// gridView1
final GridView gridV = (GridView)findViewById(R.id.gridView1);
gridV.setAdapter(new ImageAdapter(Restaurants.this,MyArrList));
// OnClick
gridV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String Position = String.valueOf(position);
Intent newActivity = new Intent(Main.this,MainTwo.class);
newActivity.putExtra("Position", Position);
newActivity.putExtra("resultServer", resultServer);
newActivity.putExtra("text", resultServer);
startActivity(newActivity);
}
});
}
我已将此行添加到Intent newActivity
- &gt; newActivity.putExtra("text", resultServer);
这是活动2 -
public class MainTwo extends Activity {
public static final int DIALOG_DOWNLOAD_JSON_PROGRESS = 0;
private ProgressDialog mProgressDialog;
public int curPosition = 0;
public String resultServer;
public ArrayList<HashMap<String, Object>> MyArrList;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_two);
Intent intent= getIntent();
curPosition = Integer.parseInt(intent.getStringExtra("Position"));
resultServer = String.valueOf(intent.getStringExtra("resultServer"));
resultServer = String.valueOf(intent.getStringExtra("text"));
try {
MyArrList = ConvertJSONtoArrayList(resultServer);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("ArrayList Size",String.valueOf(MyArrList.size()));
// Show Image Full
new DownloadFullPhotoFileAsync().execute();
// Button Home
final Button home = (Button) findViewById(R.id.btnHome);
// Perform action on click
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(MainTwo.this, Main.class);
startActivity(newActivity);
}
});
}
// Show Image Full
public void ShowImageFull(String imageName, Bitmap imgFull)
{
ImageView image = (ImageView) findViewById(R.id.fullimage);
try
{
image.setImageBitmap(imgFull);
} catch (Exception e) {
// When Error
image.setImageResource(android.R.drawable.ic_menu_report_image);
}
// Show Toast
Toast.makeText(MainTwo.this,imageName,Toast.LENGTH_LONG).show();
}
public ArrayList<HashMap<String, Object>> ConvertJSONtoArrayList(String json) throws JSONException
{
JSONArray data = new JSONArray(resultServer);
ArrayList<HashMap<String, Object>> arr = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, Object>();
map.put("ImageID", (String)c.getString("id"));
map.put("ImageName", (String)c.getString("name"));
map.put("ImagePathThum", (String)c.getString("image"));
map.put("ImagePathFull", (String)c.getString("rest_img_big"));
map.put("text", (String)c.getString("text"));
arr.add(map);
}
return arr;
}
这里我添加了for
块 - &gt; map.put(&#34; text&#34;,(String)c.getString(&#34; text&#34;));`
并获取意图部分resultServer = String.valueOf(intent.getStringExtra("text"));
这是main_two.xml
我看到Home
按钮,空格应该是text
和大图片。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1">
<ImageView
android:id="@+id/fullimage"
android:layout_width="match_parent"
android:layout_height="380dp" />
</LinearLayout>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</TableLayout>
如果您需要更多来源,我会发布。 可能是非常蹩脚的问题,但我无法理解。请帮忙。 更新
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
String url = "URL to download data";
JSONArray data;
try {
resultServer = getJSONUrl(url);
data = new JSONArray(resultServer);
MyArrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, Object>();
map.put("id", (String)c.getString("id"));
map.put("name", (String)c.getString("name"));
map.put("text", (String)c.getString("text"));
// Thumbnail Get ImageBitmap To Bitmap
map.put("ImagePathThum", (String)c.getString("image"));
map.put("ImageThumBitmap", (Bitmap)loadBitmap(c.getString("image")));
// Full (for View Full)
map.put("ImagePathFull", (String)c.getString("rest_img_big"));
MyArrList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
尝试仅传递特定项目的完整图像路径和文本而不是json数组字符串:
如何从第一项活动传递:
gridV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
Intent newActivity = new Intent(Main.this,MainTwo.class);
newActivity.putExtra("image", MyArrList.get(position).get("imagePathFull").toString());
newActivity.putExtra("text", MyArrList.get(position).get("text").toString());
startActivity(newActivity);
}
});
如何开展第二项活动:
String fullImagePath;
String text;
fullImagePath = getIntent().getStringExtra("imagePathFull");
text = getIntent().getStringExtra("text");