简而言之, 我只想显示警告对话框,如果上传活动在列表中没有数据,则启动UploadActivity ...
btnCheckUpload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder alertdialog = new AlertDialog.Builder(LoginActivity.this);
alertdialog.setTitle(getResources().getString(R.string.app_name));
alertdialog.setMessage("No Images found in UploadActivity");
alertdialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertdialog.show();
}
}
});
UploadActivity.java: -
public class UploadActivity extends Activity {
final private static int DIALOG_LOGIN = 1;
EditText editPersonName, editPersonaEmail, editPersonTelephone, parental_email ;
TextView editImageName ;
String fileName;
static ListView lstView;
private Handler handler = new Handler();;
static List <String> ImageList;
String strPath;
CheckBox chkOption3;
TextView tv1, tv2;
CheckBox chkOption1, chkOption2 ;
int position ;
static File f1;
static String folder = null ;
Intent i;
Intent intent ;
TextView textHeading1;
static File[] files;
static File file ;
static List <String> it ;
static String textHeading = null;
// new Class DB
final myDBClasss myDb = new myDBClasss(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
textHeading1 = (TextView) findViewById(R.id.txtEventNameDate1);
textHeading1.setText(CameraLauncherActivity.folder);
/*** Get Images from SDCard ***/
ImageList = getSD();
// ListView and imageAdapter
lstView = (ListView) findViewById(R.id.listView1);
lstView.setAdapter(new ImageAdapter(this));
}
public static List <String> getSD()
{
it = new ArrayList <String>();
String string = "/mnt/sdcard/Pictures/Awesome/";
f1 = new File (string+ CameraLauncherActivity.folder+ "/");
files = f1.listFiles ();
for (int i = 0; i < files.length; i++)
{
file = files[i];
Log.d("Count",file.getPath());
it.add (file.getPath());
}
return it;
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c)
{
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_upload, null);
}
// ColImgName
TextView txtName = (TextView) convertView.findViewById(R.id.ColImgName);
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();
txtName.setText(fileName);
// Image Resource
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bm = BitmapFactory.decodeFile(strPath,options);
imageView.setImageBitmap(bm);
// ColStatus
final ImageView txtStatus = (ImageView) convertView.findViewById(R.id.ColStatus);
txtStatus.setImageResource(R.drawable.bullet_button);
// progressBar
final ProgressBar progress = (ProgressBar) convertView.findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);
//btnUpload
final ImageButton btnUpload = (ImageButton) convertView.findViewById(R.id.btnUpload);
btnUpload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Upload
btnUpload.setEnabled(false);
startUpload(position);
}
});
return convertView;
}
}
答案 0 :(得分:3)
lstView.getChildCount()
将返回组中的子项数。您可以查看。还存储ImageAdapter
的引用
然后使用adapter.getcount();
您也可以检查ImageList
尺寸。
而不是
if (UploadActivity.lstView.getChildCount() > 0)
使用
if (imageList.size) > 0)
或
if (adapter.getcount() > 0)
答案 1 :(得分:0)
ListView是一个View,调用listView.getChildCount()表示:
<ListView>
<TextView></TextView>
</ListView>
没有意义,因为ListView没有孩子。您需要做的是调用Adapter.getCount();
如果你很好地创建了适配器,它应该可以正常工作。
答案 2 :(得分:0)
将您的getSD()
设为公共静态,然后尝试在LogincActivity
中访问它,如下所示:
btnCheckUpload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
List<String> dt=new ArrayList<String>();
dt=UploadActivity.getSD();
if (dt.size()> 0) {
Intent intentUpload = new Intent(LoginActivity.this, UploadActivity.class);
startActivity(intentUpload);
} else {
AlertDialog.Builder alertdialog = new AlertDialog.Builder(LoginActivity.this);
alertdialog.setTitle(getResources().getString(R.string.app_name));
alertdialog.setMessage("No Images found in UploadActivity");
alertdialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertdialog.show();
}
}
});
<强>编辑:强>
另一种方法是仅检查UploadActivity
中的条件,如果没有找到数据,则只需显示提醒并点击提醒完成活动,如下所示:
ImageAdapter adapter=new ImageAdapter(this);
if(adapter.getCount()>0)
{
// ListView and imageAdapter
lstView = (ListView) findViewById(R.id.listView1);
lstView.setAdapter(new ImageAdapter(this));
else
{
AlertDialog.Builder alertdialog = new AlertDialog.Builder(LoginActivity.this);
alertdialog.setTitle(getResources().getString(R.string.app_name));
alertdialog.setMessage("No Images found in UploadActivity");
alertdialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
finish(); //Finish your UploadActivity here.
}
});
alertdialog.show();
}