- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已将以下代码用于带有自定义适配器的 ListView。代码对我来说工作正常,
在 ListView 中,每个 ListView 行中都有一个复选框、TextView、ImageView 和 Button。数据将通过 HttpPost 方法获取并分配到 ListView 的每一行中,没有问题。
我想获取在 ListView 中选中的所有复选框,单击主 Activity 的按钮。
我已经阅读了很多文章和例子,但无法得到正确的答案。
代码:MainActivity.java 文件,单击哪个按钮并将 Toast 设置为“单击按钮”。
package com.example.listviewimageloadingexample;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "rank";
static String COUNTRY = "country";
static String POPULATION = "population";
static String FLAG = "flag";
Button processedAllBtn;
CheckBox processAll;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
processedAllBtn=(Button) findViewById(R.id.btnProcessedAllAbove);
processedAllBtn.setOnClickListener(this);
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
Object obj=(ListViewAdapter)getLastNonConfigurationInstance();
if(obj==null)
{
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
else
{
adapter=(ListViewAdapter)obj;
setListView();
}
}
@Override
public void onClick(View arg0) {
switch(arg0.getId())
{
case R.id.btnProcessedAllAbove:
//Toast.makeText(getApplicationContext(), "button is clicked "+arg0.getId(), Toast.LENGTH_SHORT).show();
break;
}
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions.getJSONfromURL("WEBSERVICE_URL");
try {
// Locate the array name in JSON
//jsonarray = jsonobject.getJSONArray("worldpopulation");
jsonarray = jsonobject.getJSONArray("students");
String flag="";
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonobject = jsonarray.getJSONObject(i);
JSONObject student_object = jsonobject.getJSONObject("students");
// Retrive JSON Objects
map.put("rank", student_object.getString("id"));
map.put("country", student_object.getString("name"));
map.put("population", student_object.getString("roll_number"));
flag = student_object.getString("student_photo");
flag = flag.replace(" ", "%20");
map.put("flag", "WEBSERVICE_URL"+flag);
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
setListView();
// Close the progressdialog
mProgressDialog.dismiss();
}
}
@Override
public Object onRetainNonConfigurationInstance() {
return adapter;
}
public void setListView()
{
if(adapter==null)
{
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
}
// Set the adapter to the ListView
listview.setAdapter(adapter);
}
}
代码:ListAdapter.java 文件
package com.example.listviewimageloadingexample;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
boolean[] itemChecked;
public ListViewAdapter(Context context, ArrayList<HashMap<String, String>> arraylist)
{
super();
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
itemChecked = new boolean[data.size()];
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder
{
TextView rank;
TextView country;
TextView population;
Button processedBtn;
ImageView flag;
CheckBox processedCheckBox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view=convertView;
final ViewHolder viewHolder;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view == null)
{
view = inflater.inflate(R.layout.listview_item, null);
viewHolder= new ViewHolder();
// Locate the TextViews in listview_item.xml
viewHolder.rank = (TextView) view.findViewById(R.id.rank);
viewHolder.country = (TextView) view.findViewById(R.id.country);
viewHolder.population = (TextView) view.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
viewHolder.flag = (ImageView) view.findViewById(R.id.flag);
viewHolder.processedBtn = (Button) view.findViewById(R.id.btnProcessed);
viewHolder.processedCheckBox = (CheckBox)view.findViewById(R.id.processedCheckBox);
view.setTag(viewHolder);
}
else
{
viewHolder=(ViewHolder)view.getTag();
}
// Get the position
resultp = data.get(position);
// Capture position and set results to the TextViews
viewHolder.rank.setText(resultp.get(MainActivity.RANK));
viewHolder.country.setText(resultp.get(MainActivity.COUNTRY));
viewHolder.population.setText(resultp.get(MainActivity.POPULATION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), viewHolder.flag);
viewHolder.processedBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("rank", resultp.get(MainActivity.RANK));
// Pass all data country
intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
// Pass all data population
intent.putExtra("population",resultp.get(MainActivity.POPULATION));
// Pass all data flag
intent.putExtra("flag", resultp.get(MainActivity.FLAG));
// Start SingleItemView Class
context.startActivity(intent);
}
});
viewHolder.processedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
itemChecked[position]=(!itemChecked[position]);
viewHolder.processedCheckBox.setChecked(itemChecked[position]);
}
});
return view;
}
}
代码:listitem.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/processedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:saveEnabled="false" />
<ImageView
android:id="@+id/flag"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_vertical|center_horizontal"
android:padding="5dp"/>
<TextView
android:id="@+id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="id"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/country"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="@+id/population"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Roll_No"
android:textStyle="bold"
android:visibility="gone" />
<Button
android:id="@+id/btnProcessed"
android:layout_width="90dp"
android:layout_height="30dp"
android:textSize="15sp"
android:layout_gravity="center_vertical|center_horizontal"
android:background="@drawable/buttonshape"
android:text="Processed"
android:textColor="#FFFFFF"
android:layout_marginLeft="2dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>
</LinearLayout>
代码:listview_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkAllAbove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select All" />
<Button
android:id="@+id/btnProcessedAllAbove"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_marginLeft="119dp"
android:background="@drawable/buttonshape"
android:text="Processed"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="320dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>
</ScrollView>
最佳答案
有一个复选框 getTag() 方法,将它与一些逻辑一起使用,你应该没问题
参见:http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html
关于java - 安卓 : Determine which checkbox is checked in listview from the Main activity button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24631952/
我对 Android 很陌生,如果问题重复,请避免并发送链接。有三个 Activity A、B 和 C。 Activity A 获取一个用户名,我想在 Activity C 中显示该用户名,但我想先运
我正在尝试制作记事本应用程序,因此每次打开新笔记时,布局都会相同。另外, Activity 的数量(新注释)不应定义得尽可能多 最佳答案 如果 Activity 始终相同,您可能应该创建一个适配器,允
我有 3 个 Activity 。 主窗口 5 个按钮 在按钮的主窗口中按下此窗口打开(将其称为父窗口) 在父窗口按钮上按下此窗口打开调用它作为结束子窗口。 现在从子窗口我从父窗口获取值如下:
我遇到了一个 Activity backstack 问题。假设我的后台有 5 个 Activity :比如 Activity A、 Activity B、 Activity C、 Activity D
我正在寻找必须具有以下附加特征的 JMS 提供程序: 采用多代理,所有代理都必须处于事件状态(无单点故障) 仅在两台机器上进行扩展就足以满足我们的需求 能够保证订购(如果 1 个生产者 + 1 个消费
假设,我有一个由 TabHost 组成的选项卡 Activity 。 TabHost 包含 2 个选项卡,每两个选项卡都有一个 Activity 组。每个 Activity 组包含一项 Activit
我正在开发一个应用程序,我需要根据某些操作导航到特定 Activity 。这是一张图片 我的第一个 Activity 是 ReadingActivity。基于某些操作,用户将被带到 NewProjec
我创建了一个与服务器异步通信的应用程序。当应用程序发出服务器请求时,将创建一个带有“正在加载”通知的新对话框( Activity )。主要 Activity 实现了处理服务器响应的方法,我想在主要 A
我想在我的所有应用程序 Activity 中显示相同的选项菜单。我创建了一个实现菜单的通用 Activity ,并且我所有的进一步 Activity 都扩展了它。 问题:当我需要扩展其他特定 Acti
我有四个 Activity ,即 java 文件 - Activity1.java、activity2.java、activity3.java、activity4.java 和 xml 文件 - Ac
我有两个 Activity 。我想将数据从第二个 Activity 发送到上一个 Activity 。第一个 Activity 有自定义 ListView 和 bean 类。当我点击第二个 Activ
根 Activity 是堆栈中当前的第一个 Activity 还是 list 中指定为启动 Activity 的 Activity ? 支持应用程序 P 在启动时启动 Activity A。然后 A
你好 我想知道您在绘制 Activity 图选择“Activity ”时考虑了哪些关键点? 您如何从要建模的问题中选择 Activity ? 谢谢 最佳答案 Activity 图用于对正在开发的系统和
如何从主 Activity 启动 Activity 并在子 Activity 返回主 Activity 中退出操作后返回主 Activity ? 我已将子 Activity 作为启动器 Intent
我的工作流程如下: 登录 Activity -> ActivityB -> ActivityC -> ActivityD 我想将数据从LoginActivity传递到ActivityD,但不直接传递到
我之前曾尝试获得此问题的答案,但找不到可以解决我的问题的答案。我正在制作保存圆盘高尔夫球分数的应用程序。我的 MainActivity 有 4 个按钮。新比赛、恢复比赛、类(class)和球员。 At
我有一个 tts 非 UI 类和 Activity 类。现在在 Activity 类中,我有一个按钮,用户可以从中选择男声或女声,具体取决于我想要将字符串传递给 tts 类的选择,然后一次tts 类根
问题有点复杂,首先, Activity A 和 Activity B 的 list 中都有 android:noHistory = true 。我有一个自定义 serialized 类,假设 MyCl
在我的应用程序中,我有两个 Activity (AuthenticationActivity 和 MainActivity),每个 Activity 都有一个导航图和大量 fragment 。我创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How can i use compose email activity in tabView? 我想在选项
我是一名优秀的程序员,十分优秀!