- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
int globalPosition ;
..............
buttonAllData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new UploadBulkData(globalPosition).execute();
}
});
........
class UploadBulkData extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
int dataPosition;
public UploadBulkData(int position){
this.dataPosition = position;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(UploadActivity.this);
pDialog.setMessage("Uploading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
.......
String url = "http://web/uploadBulk.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("UserData", st));
String resultServer = getHttpPost(url,params);
Log.d("Entire string::", " " + resultServer);
/*** Default Value ***/
strStatusID = "0";
strError = "";
JSONObject jsonObject;
try {
jsonObject = new JSONObject(resultServer);
strStatusID = jsonObject.getString("StatusID");
strError = jsonObject.getString("Message");
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
fileNameDB=ImageList.get(globalPosition).toString().substring
(strPath.lastIndexOf('/')+1, strPath.length());
if(strStatusID.equals("1"))
{
Toast.makeText(UploadActivity.this, "Data Uploaded Successfully", Toast.LENGTH_SHORT).show();
long saveImge = myDbbv.InsertData(fileNameDB);
Log.d("fileNameDB:UP", String.valueOf(saveImge));
}
else
{
Toast.makeText(UploadActivity.this, "Unable to upload Data", Toast.LENGTH_SHORT).show();
}
if (file_url != null){
Toast.makeText(UploadActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
在 getView 中我使用的是这样的:
public View getView(final int position, View convertView, ViewGroup parent) {
holder.dataImageView.setImageResource(R.drawable.bullet_button);
try {
// check data exist or not
boolean strDataExistU = myDbbv.Exists(fileNameDB);
if(strDataExistU)
{
holder.dataImageView.setImageResource(R.drawable.online);
}
else
{
boolean strDataExist = myDb.Exists(fileNameDB);
if(strDataExist)
{
holder.dataImageView.setImageResource(R.drawable.local);
}
else
{
holder.dataImageView.setImageResource(R.drawable.default);
}
}
} catch (Exception e) {
}
}
正如您在 getView(...) 方法中看到的,我使用了三种不同类型的可绘制对象(即:在线、本地、默认)
在线节目数据已上传到在线服务器,本地节目已添加到本地数据库并默认..(既不上传到服务器也不存储到本地数据库)
问题:
每当我进行批量上传时,仅对列表中的最后一行项目进行在线绘制,而我已将整个列表项数据上传到服务器
我只想显示所有列表项的在线可绘制对象,即我已上传到服务器的那些,否则我的代码可以正常工作...
几乎完整的代码:
public class UploadActivity extends Activity {
int globalPosition ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
ImageButton buttonAllData = (ImageButton) findViewById(R.id.btnMenus);
buttonAllData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new UploadBulkData(globalPosition).execute();
}
});
/*** Get Images from SDCard ***/
ImageList = getSD();
// ListView and imageAdapter
lstView = (ListView) findViewById(R.id.listView1);
lstView.setAdapter(new ImageAdapter(this));
totalItems = ""+ lstView.getAdapter().getCount();
}
public static List <String> getSD()
{
List <String> it = new ArrayList <String>();
String string = "/mnt/sdcard/Pictures/Joseph/";
f = new File (string+ CameraLauncherActivity.folder+ "/");
files = f.listFiles ();
/***
* to show last taken image on top using lastModified
* to sort data
* to refresh data after (remove or update)
*/
Arrays.sort(files, new Comparator<Object>()
{
public int compare(Object o1, Object o2) {
if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
// <<<<<<<<< END >>>>>>>>>>>
for (int i = 0; i < files.length; i++)
{
file = files[i];
Log.d("Count",file.getPath());
it.add (file.getPath());
}
return it;
}
static class ViewHolder {
public ViewHolder(View convertView) {
// TODO Auto-generated constructor stub
}
TextView imageNameTextView;
ImageView sdCardImageView, statusImageView, dataImageView;
ProgressBar uploadProgressBar;
ImageButton uploadImageButton, dataImageButton;
boolean isUploading = false;
}
public class ImageAdapter extends BaseAdapter
{
public ImageAdapter(Context c)
{
}
public int getCount() {
// TODO Auto-generated method stub
return ImageList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Avoid unneccessary calls to findViewById() on each row, which is expensive!
holder = null;
// If this item is to be synced
if(flags.get(position)) {
startUpload(position);
// Mark as synced
flags.put(position, false);
}
/*
* If convertView is not null, we can reuse it directly, no inflation required!
* We only inflate a new View when the convertView is null.
*/
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_upload, null);
holder = new ViewHolder(convertView);
// Create a ViewHolder and store references to the children views
holder.imageNameTextView = (TextView) convertView.findViewById(R.id.ColImgName);
holder.sdCardImageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
holder.statusImageView = (ImageView) convertView.findViewById(R.id.ColStatus);
holder.uploadProgressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
holder.uploadImageButton = (ImageButton) convertView.findViewById(R.id.btnUpload);
holder.dataImageButton = (ImageButton) convertView.findViewById(R.id.btnData);
holder.dataImageView = (ImageView) convertView.findViewById(R.id.dataExist);
// The tag can be any Object, this just happens to be the ViewHolder
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
strPath = ImageList.get(position).toString();
// Get File Name
fileName = strPath.substring( strPath.lastIndexOf('_')+1, strPath.length() );
file = new File(strPath);
@SuppressWarnings("unused")
long length = file.length();
holder.imageNameTextView.setText(fileName);
fileName=ImageList.get(position).toString().substring
(strPath.lastIndexOf('_')+1, strPath.length());
fileNameDB=ImageList.get(position).toString().substring
(strPath.lastIndexOf('/')+1, strPath.length());
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bm = BitmapFactory.decodeFile(strPath,options);
holder.sdCardImageView.setImageBitmap(bm);
if(holder.isUploading) {
holder.uploadProgressBar.setVisibility(View.VISIBLE);
} else {
holder.uploadProgressBar.setVisibility(View.GONE);
}
holder.dataImageView.setImageResource(R.drawable.bullet_button);
try {
// check data exist or not
boolean strDataExistU = myDbbv.Exists(fileNameDB);
if(strDataExistU)
{
holder.dataImageView.setImageResource(R.drawable.online);
}
else
{
// check data exist or not
boolean strDataExist = myDb.Exists(fileNameDB);
if(strDataExist)
{
holder.dataImageView.setImageResource(R.drawable.database);
}
else
{
holder.dataImageView.setImageResource(R.drawable.default);
}
}
} catch (Exception e) {
// TODO: handle exception
}
fileName = ImageList.get(position).toString().substring
(strPath.lastIndexOf('/')+1, strPath.length());
try {
boolean strExist = myDbb.Exists(fileName);
if(strExist)
{
holder.statusImageView.setImageResource(R.drawable.onl);
}
else
{
holder.statusImageView.setImageResource(R.drawable.bullet_button);
}
} catch (Exception e) {
// TODO: handle exception
}
// btnData
holder.dataImageButton.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
// Print
globalPosition = position;
fileName=ImageList.get(position).toString().substring
(strPath.lastIndexOf('_')+1, strPath.length());
fileNameDB=ImageList.get(position).toString().substring
(strPath.lastIndexOf('/')+1, strPath.length());
showDialog(DIALOG_LOGIN);
}
});
return convertView;
}
}
class UploadData extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(UploadActivity.this);
pDialog.setMessage("Uploading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
String url = "http://web/uploadData.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sImageName", fileNameDB));
Log.d("sImageName::", fileNameDB);
String resultServer = getHttpPost(url,params);
Log.d("Entire string::", " " + resultServer);
/*** Default Value ***/
strStatusID = "0";
strError = "";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
try {
fileName=ImageList.get(globalPosition).toString().substring
(strPath.lastIndexOf('_')+1, strPath.length());
fileNameDB=ImageList.get(globalPosition).toString().substring
(strPath.lastIndexOf('/')+1, strPath.length());
// prepare save data
if(strStatusID.equals("0"))
{
Toast.makeText(getApplicationContext(), "Unable to upload Data",
Toast.LENGTH_LONG).show();
}
else if (strStatusID.equals("1"))
{
Toast.makeText(getApplicationContext(), "Data Uploaded Successfully!",
Toast.LENGTH_SHORT).show();
// Save Data
long saveImge = myDbbv.InsertData(fileNameDB);
Log.d("fileNameDB:UP", String.valueOf(saveImge));
} else {
Toast.makeText(getApplicationContext(), "Unable to upload Data",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// TODO: handle exception
}
if (file_url != null){
Toast.makeText(UploadActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
});
cancelButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
closeButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
}
}
class UploadBulkData extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
int dataPosition;
//constructor to pass position of row, on which button was clicked to class
public UploadBulkData(int position){
this.dataPosition = position;
}
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(UploadActivity.this);
pDialog.setMessage("Uploading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
String url = "http://web/uploadBulk.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("EventData", st));
String resultServer = getHttpPost(url,params);
Log.d("Entire string::", " " + resultServer);
/*** Default Value ***/
strStatusID = "0";
strError = "";
JSONObject jsonObject;
try {
jsonObject = new JSONObject(resultServer);
strStatusID = jsonObject.getString("StatusID");
strError = jsonObject.getString("Message");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
// Prepare Save Data
if(strStatusID.equals("1"))
{
Toast.makeText(UploadActivity.this, "Data Uploaded Successfully", Toast.LENGTH_SHORT).show();
fileNameDB=ImageList.get(dataPosition).toString().substring
(strPath.lastIndexOf('/')+1, strPath.length());
// Save Data
long saveImge = myDbbv.InsertData(fileNameDB);
Log.d("fileNameDB:UP", String.valueOf(saveImge));
}
else
{
Toast.makeText(UploadActivity.this, "Unable to upload Data", Toast.LENGTH_SHORT).show();
}
if (file_url != null){
Toast.makeText(UploadActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
最佳答案
问题出在 holder.dataImageButton.setOnClickListener 方法 block 中,您分配给 globalPosition (globalPosition = position;
) 的位置值是传递给 getView 方法的值(每次回收 View 时都会调用 getView )。因此,您应该将 position 设置为 holder.dataImageButton 标记并在 setOnClickListener 方法 block 中从中检索。
所以在holder.dataImageButton标签中设置位置
holder.dataImageButton.setTag(position);
在你的代码行之后
holder.dataImageView.setImageResource(R.drawable.bullet_button);
并将您的 setOnClickListener 方法修改为
holder.dataImageButton.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
// Print
globalPosition = (Integer)v.getTag(); //modified
fileName=ImageList.get(position).toString().substring
(strPath.lastIndexOf('_')+1, strPath.length());
fileNameDB=ImageList.get(position).toString().substring
(strPath.lastIndexOf('/')+1, strPath.length());
showDialog(DIALOG_LOGIN);
}
});
关于android - 使用 ViewHolder 显示 ListView 中最后一个列表项的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29715351/
最近我使用 RecyclerView 并添加了一个自定义标题 View (另一种类型的项目 View )并尝试在数据发生更改时对其进行更新。奇怪的事情发生了。适配器创建一个新的 HeaderViewH
我的项目应该基于用户输入(用户发送文本、拍照或录制视频)创建多个 View ,类似于 WhatsApp 聊天 Activity ,结构几乎相同。适配器应该能够通过使用 getItemViewType(
在 ViewHolder pattern 中将 ViewHolder 设为静态对性能至关重要吗? ? A ViewHolder object stores each of the component
我只是想更好地理解我经常用来优化 ListView 的以下模式 我的阅读只指出静态内部类被视为顶级类的事实。与成员类(非静态)相比,这样的事情有什么好处? @Override public View
我有一个 RecyclerView 和一些 view/card(我们暂时称它为 View ),它们都包含相同的东西,包括我用作分隔栏的 View。 我希望能够在当前 View 上方的 view 中更改
我需要从 View Holder 访问 RecyclerView Adapter 的方法。我找不到任何解决方案。 或者是否可以从 ViewHolder 的 ViewModel 类(我已经在 MVVM
在我的 RecyclerView 适配器中只有一个 ViewHolder 类: public static class PlayerItemViewHolder extends RecyclerVie
我已经采用了一个示例代码来实现 RecyclerView,但试图将其转换为在我的应用程序的子 fragment 中工作。 代码在“创建列表 - 示例”下 Creating Lists and Card
我试图在我的主要 Activity 中而不是在我的自定义适配器中保存复选框的状态。我已检查共享首选项中是否存储了正确的数据,并且可以成功检索信息,但是当我尝试在打开应用程序时标记复选框时, View
我是 Kotlin 的新手,我正在制作一款货币兑换应用。在适配器中,我想将一些项目传递到新 Activity 中。 class AdapterC (val countryList: ArrayLis
我在我的代码中遇到了这个问题,当在 Viewholder 中的一个按钮上进行转换时,这是在 Onclicklistener 中完成的,多个转换发生在不同的行上即,如果我单击第 1 行中的按钮,则按钮向
public class ListViewAdapter extends BaseAdapter { private Context context; private LocationDetails[
我刚刚生成了一个 Master/Detail Flow 项目,我发现了一些奇怪的东西:在 DriverListActivity.java 中,名为 ViewHolder 的子类具有 final 属性。
我正在尝试配置抽屉导航以显示 3 种不同类型的“行”,但我在使用 Holder 时遇到了问题,被告知该消息: FATAL EXCEPTION: main java.lang.ClassCastExce
我理解Viewholder pattern的思路和用法,但我仍然有一个问题: 假设我们在 viewholder 中有一个 TextView,并显示 10 个项目(“item0,item1 ....”)
我有一个包含 3 种不同类型行的 ListView 。我在网上收到 ClassCastException holder = (RowViewHolder) row.getTag(); 我注意到 row
我正在尝试学习 Android 编程。我找不到这个算法的解释: public View getView(int r, View convertView, ViewGroup parent) { V
我正在创建一个 Android 应用程序,其中包含一个带有嵌套 CardView 的 RecyclerView。我需要将所有其他卡片替换为不同的颜色。我正在使用 @Override 来覆盖 onBin
我在 ListView 中使用分段时遇到问题。我需要使用 viewholder 使 listview 滚动平滑,但我不知道如何实现两个 viewholders,因为有两个单独的 View ,一个是部分
我在 ListView 上遇到了 View 持有者的充气问题。实现 Viewholder 后,充气机会出错,因此消息显示错误。 (在图 10 中,消息不是来 self ,而且每当我滚动时,错误的消息都
我是一名优秀的程序员,十分优秀!